TRYING to use SaveAs2 to save a word doc that never has been saved before. I use FileName:= "F:\FolderOfWordDocs\FolderOfDocs\DocFileName.docm" seems should save and name the file. EVEN if that is not the default directory.

mark jackson 1 Reputation point
2022-06-11T21:16:36.62+00:00

I use CreateObject to start Word 2016. Then open one of several templates. All works to this point.

Then try to save the new word document using SaveAs2 I get a RunTime error 5152 bad file name.

I based on all the documentation I can find I could just use FileName:= "Doc1.docm" if I was in the default directory but I am not.

All the documentation seems to imply I could use like this. FileName:= "F:\externalHardDrive\FolderOfWordDocs\Doc1.docm"

I have been unable to tell if that is too long or not permitted to include the hard drive letter.

or I must change the default directory or move to that directory. NOT all of the word docs are to go to the "F" drive or would be simple to change the save directory in word's backstage.

Dim strSaveCompletePath As String   ' THIS TO HOLD BOTH FILENAME AND PATH TO STORE IT.  
  
  Dim WRD As Object  
    
  Set WRD = CreateObject("Word.Application")  
  Dim doc As Document  
    
    WRD.Visible = True  
  
WRD.Documents.Open "F:\SeaGateBlue\Stories\MyStories\WrdWriteTemplate.dotm"  
  
  
myCloseDoc = InputBox("1 TO QUIT WORD " & vbCrLf & "2 TO SAVE THE ACTIVE DOC" & vbCrLf & "3 TO SAVE AND QUIT WORD", "PICK ONE")  
  
Select Case myCloseDoc  
      
    Case 1  
            MsgBox "QUIT WORD"  
              
            WRD.Quit  
    Case 2  
            MsgBox "SAVE ACTIVE DOC"  
              
            Dim varName As Variant  
              
            varName = InputBox("ENTER THE NAME TO SAVE AS", "NAME THE STORY", "AAAstory")  
            varName = varName & ".docm"  
              
            Debug.Print "CASE 2   varName is name to save as = " & varName  
              
                          
                        varName = Me.cboStoryStorePath & varName  
                          
                  
            varName = Replace(varName, Chr(34), "", 5, 1, vbTextCompare)   ' SEEMS MUST HAVE TO GET RID OF THE  DBL QUOTES AT END OFF THE PATH TO SAVE.  
   
              
            ActiveDocument.SaveAs2 FileName:=varName, AddToRecentFiles:=True  
              
               
              
    Case 3  
            MsgBox "SAVE AND QUIT WORD"  
    Case Else  
            MsgBox "SOMETHING WRONG.   SHOULD NOT GET HERE"  
      
End Select  
  
  
Set WRD = Nothing  
  
  
Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
6,030 questions
Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
780 questions
Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
729 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Paul Edstein 166 Reputation points
    2022-06-11T22:45:52.423+00:00

    There are multiple flaws in your code. For starters, given that you're using atemplate, you should be using Documents.Add, not Documents.Open. Furthermore, you need to specify both the file type (should be docx) and file format. The path also needs to include the final '\' separator, if not already present. Thus:
    Dim Wrd As Object, Doc As Document, varName As String
    Set Wrd = CreateObject("Word.Application")
    With Wrd
    .Visible = True
    Set Doc = .Documents.Add("F:\SeaGateBlue\Stories\MyStories\WrdWriteTemplate.dotm")
    'Presumably some editing happens here, referencing 'Doc', not 'ActiveDocument', then:
    Select Case InputBox("1 TO QUIT WORD " & vbCrLf & "2 TO SAVE THE ACTIVE DOC" & vbCrLf & "3 TO SAVE AND QUIT WORD", "PICK ONE")
    Case 1
    MsgBox "QUIT WORD"
    Doc.Close False
    .Quit
    Case 2
    MsgBox "SAVE ACTIVE DOC"
    varName = Me.cboStoryStorePath & InputBox("ENTER THE NAME TO SAVE AS", "NAME THE STORY", "AAAstory") & ".docx"
    Doc.SaveAs2 FileName:=varName, FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=True
    Case 3
    MsgBox "SAVE AND QUIT WORD"
    If InStr(Doc.FullName, Me.cboStoryStorePath) = 0 Then
    varName = Me.cboStoryStorePath & "\" & InputBox("ENTER THE NAME TO SAVE AS", "NAME THE STORY", "AAAstory") & ".docx"
    Doc.SaveAs2 FileName:=varName, FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=True
    ElseIf .Saved = False Then
    .Save
    End If
    .Quit
    Case Else
    MsgBox "SOMETHING WRONG. SHOULD NOT GET HERE"
    End Select
    End With
    Set Doc = Nothing: Set Wrd = Nothing