How to define/modify a word document path before saveAS

1.9k views Asked by At

I am writing a VBScript that will preform some auto conversion of word documents from .doc to either .docx or .dotm depending if the .doc has any VBA script embedded into the document.

The only trouble im having is modifying the File.Path

I can append the "x" to the tail of the existing path which staifys my requirment for creating the docx, but trying to modify the path to change the extension from doc to dotm is proving to be quite hard to solve.

Is there anyway to define a new FileSystem Path object?

My current thoughts are to use the FileSystemObject Data available. (filename, parentfolder ect) and build a new path to pass into the the SaveAs method.

For Each oFile In oFldr.Files
    If LCase(oFSO.GetExtensionName(oFile.Name)) = "doc" Then

        Set oDoc = oWord.Documents.Open(oFile.path)

            Dim parentFolder = oFSO.GetParentFolderName(oFile)
            Dim baseFileName = oFSO.GetBaseName(oFile)


            If oDoc.HasVBProject Then
                oWord.ActiveDocument.SaveAs parentFolder & baseFileName & ".dotm", 15
            Else
                oWord.ActiveDocument.SaveAs oFile.path & "x", 12
            End If
        oDoc.Close
    End If
Next
1

There are 1 answers

1
Ansgar Wiechers On BEST ANSWER

Always use BuildPath for constructing paths. Also, if you're actually using VBScript (not VBA) you cannot define a variable and assign a value to it in the same step. Plus, documents with macros should be saved as .docm (type 13), not as .dotm (type 15), see WdSaveFormat Enumeration. The latter type is for document templates with macros.

This should work:

Dim parentFolder, baseFileName, extension, format, newPath

parentFolder = oFSO.GetParentFolderName(oFile)
baseFileName = oFSO.GetBaseName(oFile)

If oDoc.HasVBProject Then
    extension = ".docm"
    format    = 13 
Else
    extension = ".docx"
    format    = 12 
End If

newPath = oFSO.BuildPath(parentFolder, baseFileName & extension)

oWord.ActiveDocument.SaveAs newPath, format