0

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.

1
  • 1
    What version of Word are you using? SaveAs2 only works in Word 2010 or later. Commented Sep 11, 2013 at 16:34

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

.docx isn't aadequate to wdFormatXMLDocument. Also check msdn.microsoft.com/en-us/library/ff839952.aspx !
@LS_dev Not sure what you're trying to point out. I've tested the above code on Word 2007, 2010, and 2013 without any issues.
Yes, because when Word starts reading file states that it is not wdFormatXMLDocument and try to guess correct format. If you want to create a true .docx, use wdFormatXMLDocument. Use wdFormatXMLDocument to create .xml documents.
You can also specify filename without extension, so Word automatic adds correct one. Or you can omit FileFormat and Word will guess correct format from extension.
@LS_dev, I don't think I buy into that explanation. An xml uses the FileFormat wdFormatFlatXML. You can test it out easily by creating a macro with a new document and save it as a XML.
|
0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.