Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Language version: Microsoft Word/Excel version 16.41
Operating system: Mac OS Mojave 10.14.6

I am trying to use Excel VBA to save a Word document on my desktop.

Result:

Run time error 5152. This is not a valid file name. Try one or more of the following:
-Check the path to make sure it was typed correctly
-Select a file from the list of files and folders

I am using Microsoft Excel version 2008 and Microsoft Word version 2008. I am using early binding and I have selected Microsoft Excel, Office, and Word 16.0 Object Library. (Does the 16.0 object library seem odd for version 2008?) (Windows 10 Pro version 2004. Microsoft Office 365 Subscription)

I am trying to follow this tutorial .

Line with error:

.ActiveDocument.SaveAs2 Environ("UserProfile") & "\Desktop\MovieReport.docx"

My code :

Option Explicit
Sub CreateBasicWordReportEarlyBinding()
    Dim wdApp As Word.Application
    Set wdApp = New Word.Application
    With wdApp
        .Visible = True
        .Activate
        .Documents.Add
        With .Selection
            .ParagraphFormat.Alignment = wdAlignParagraphCenter
            .BoldRun
            .Font.Size = 18
            .TypeText "Best Movies Ever"
            .BoldRun
            .Font.Size = 12
            .TypeText vbNewLine
            .ParagraphFormat.Alignment = wdAlignParagraphLeft
            .TypeParagraph
        End With
        Range("A2", Range("A2").End(xlDown).End(xlToRight)).Copy
        .Selection.Paste            
        .ActiveDocument.SaveAs2 Environ("UserProfile") & "\Desktop\MovieReport.docx"
        .ActiveDocument.Close
        .Quit
    End With
   'Set wdApp = Nothing
End Sub
                Shouldn't the full extension be "C:\Users\" & Environ("UserProfile") & "\Desktop\MovieReport.docx"?
– dwirony
                Sep 25, 2020 at 15:31
                @xwhitelight after attempting that, I received Run-time error '6294' Method 'SaveAs2' of object'_Document' failed
– CullerWhale
                Sep 25, 2020 at 15:35
                @dwirony I received the same 5152 error when I tried that using .ActiveDocument.SaveAs2 "C:\Users\" & Environ("UserProfile") & "\Desktop\MovieReport.docx"
– CullerWhale
                Sep 25, 2020 at 15:36
                What about .ActiveDocument.SaveAs Filename:="C:\Users\" & Environ("UserProfile") & "\Desktop\MovieReport.docx"?
– dwirony
                Sep 25, 2020 at 15:50

You must use "ActiveDocument.SaveAs", not "ActiveDocument.SaveAs2". You cannot use "ActiveDocument.SaveAs2" method if you use Office 2010 or an older version. Use the following code and I hope your problem will be solved.

With wdApp  
   'Your codes...
   SaveName = Environ("UserProfile") & "\Desktop\MovieReport.docx"
   If .Version <= 12 Then
      .ActiveDocument.SaveAs SaveName
      .ActiveDocument.SaveAs2 SaveName
   End If
End With

The best way to do this (in any version) is to find the proper syntax for saving to a Mac OS folder:

  • open a file
  • record a macro
  • save the file (choose your folder)
  • stop the recording
  • edit the macro
  • see how Word translated it in VisualBasic
  • example with Word 16 (2019)

    ActiveDocument.SaveAs2 FileName:= _ "/Users/myusername/anyfolder/nameofthefile" & DocNum & ".txt" _ , FileFormat:=wdFormatText, LockComments:=False, Password:="", _ AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _ EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _ :=False, SaveAsAOCELetter:=False, Encoding:=65001, InsertLineBreaks:= _ False, AllowSubstitutions:=False, LineEnding:=wdCROnly
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.