0
votes

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
1
Try to save it at the root folder first, like: C:\MovieReport.docxxwhitelight
Shouldn't the full extension be "C:\Users\" & Environ("UserProfile") & "\Desktop\MovieReport.docx"?dwirony
@xwhitelight after attempting that, I received Run-time error '6294' Method 'SaveAs2' of object'_Document' failedCullerWhale
@dwirony I received the same 5152 error when I tried that using .ActiveDocument.SaveAs2 "C:\Users\" & Environ("UserProfile") & "\Desktop\MovieReport.docx"CullerWhale
What about .ActiveDocument.SaveAs Filename:="C:\Users\" & Environ("UserProfile") & "\Desktop\MovieReport.docx"?dwirony

1 Answers

0
votes

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