CullerWhale
CullerWhale

Reputation: 31

Path error 5152 when saving Word document using Excel VBA

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

Upvotes: 0

Views: 1342

Answers (2)

Alex
Alex

Reputation: 1

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

Upvotes: 0

IcebergKF
IcebergKF

Reputation: 16

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
   Else
      .ActiveDocument.SaveAs2 SaveName
   End If
   
End With

Upvotes: 0

Related Questions