RonanC
RonanC

Reputation: 19

VBA save as Filepath

I'm attempting to use this code found here:

https://answers.microsoft.com/en-us/msoffice/forum/all/can-i-save-as-multiple-documents-at-one-time/eae10efb-1984-4131-b072-a96d45020ba9

Sub SaveAllOpenDocsAsDocx()

For Each aDoc In Application.Documents

    aDoc.SaveAs FileName:=aDoc.FullName & ".doc", FileFormat:=wdFormatDocument

    aDoc.Close

Next aDoc

End Sub

I'd like to save any open word documents to a specific folder path, how would I go about changing

FileName:=aDoc.FullName

to a specific locations e.g. C:\Users\joe.blog\Desktop\Backup

Upvotes: 0

Views: 489

Answers (1)

dbmitch
dbmitch

Reputation: 5386

Using FullName property includes the original path.

You need to pull out the Filename using the Name property and append that to your path

Something like this

Sub SaveAllOpenDocsAsDocx()
    Const MY_LOCATION = "C:\Users\joe.blog\Desktop\Backup\"
    Dim myFileLocation As String

    For Each aDoc In Application.Documents
        myFileLocation = MY_LOCATION & aDoc.Name & ".doc"
        aDoc.SaveAs FileName:=myFileLocation, FileFormat:=wdFormatDocument
        aDoc.Close
    Next aDoc

End Sub

Upvotes: 2

Related Questions