Wendy Tittle
Wendy Tittle

Reputation: 1

Using a userform object to save a file

I need to have the VBA code save a file, that was created from a template, to a specific directory with a specific name. Example: \Partial path\ plus a subdirectory selected in the userform \ plus another bit of info from a different userform. I can get it to save to the partial path, but adding the subdirectory and the filename is where I'm stuck. This was the last thing I tried...

ActiveDocument.SaveAs2 FileName:="X:\Directory\" & strSubDirectory & strUserText ".docx"

Any help would be greatly appreciated.

Upvotes: 0

Views: 1550

Answers (2)

David Zemens
David Zemens

Reputation: 53623

Did you make sure that your strSubDirectory etc. contain the path separator? If not, you need to include this in your FileName string:

ActiveDocument.SaveAs2 _
    FileName:="X:\Directory\" & strSubDirectory & "\" & strUserText & ".docx"

Upvotes: 0

jmstoker
jmstoker

Reputation: 3475

You're missing a "\" and some &s in your statement. Also depending on your version of Word, you might not be able to use SaveAs2 as it was introduced with Word 2010. The code below works using SaveAs. Note: This code assumes the subdirectory already exists

Private Sub SaveDocument()
    Dim strSubDirectory As String
    Dim strUserText As String
    Dim myPath As String

    strSubDirectory = "SubTest"
    strUserText = "Test"
    myPath = "C:\Test\" & strSubDirectory & "\" & strUserText & ".docx"

    ActiveDocument.SaveAs FileName:=myPath, FileFormat:=wdFormatXMLDocument
End Sub

The different types of FileFormat can be found at http://msdn.microsoft.com/en-us/library/ff839952.aspx

Upvotes: 1

Related Questions