• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA to save word doc in directory.

MikeTE

New Member
hello,

I'm struggling with changing my excel VBA code to word (don't hate me i'm a beginner :) )

that's my code it works in excel but not in word.
can anybody please help me to fix it? it's tripping on filename=textbox1.text

original excel code:
Code:
Private Sub CommandButton1_click()  
Dim path as string   
Dim filename as string   
path = "C:kjsdfhdfghld"   
filename = range("d2").text  
Application.displayAlerts = false  
ActiveWorkbook.saveAs Filename:=path & filename & ".xls", fileFormat:=xlopenXMLWorkbook  
Application.displayAlerts = true
  ActiveWorkbook.close

End Sub
my word test
Code:
Sub test()
  Dim path As String
    Dim filename As String
    path = "G:\Management\"
   
    filename = TextBox1.Text
    Application.DisplayAlerts = False
    ActiveDocument.SaveAs filename:=path & filename & ".doc", fileFormat:=docDocument
    Application.DisplayAlerts = True
    ActiveDocument.Close
End Sub
 
Last edited by a moderator:

Stevie

Active Member
Hiya Mike,
according to the Microsoft docs site here, you need to use something like:
Code:
ActiveDocument.Shapes(1).TextFrame.TextRange.Text = "My Text"
So, depending what the shape index is (or name) in your document, your code should look more like:
Code:
filename = Shapes(1).TextFrame.TextRange.Text
Where '1' is replaced with the index of the textbox in question.

You can also use the name of the shape as follows:
Code:
filename = Shapes("Text Box 1").TextFrame.TextRange.Text
Where 'Text Box 1' is the name of the shape.

I hope this helps, if so, please click 'Like' ! on the bottom right.

Stevie
^.^
 

macropod

Member
You really ought to use the VBA Help file. There is no such Word FileFormat constant as docDocument. Querying the SaveAs (or, better still, SaveAs2) method in the VBA Help file would tell you what the valid arguments and constants are.
 
Top