Saving a Word Document from Excel

Nick_De

New Member
Joined
Oct 4, 2019
Messages
1
Hello All,

I am hoping someone can help me with a problem I have been surfing the forums trying to find a solution to for a few hours now. I have an Excel macro file which will be stored in our company's SharePoint which will be used to track contracts being created and also serve a process for creating the actual contracts which are created in Word (Also stored in SharePoint).

I have managed to create the Excel file and code which opens the Word file (.docx) and populates all the relevant data but I am having an issue with saving the word file automatically with a dynamic name (SaveAs). My Code is below

Code:
    Dim ws As Worksheet, msWord As Object, itm As Range, fname As String
    fname = cName
    Set ws = ActiveSheet
    Set msWord = CreateObject("Word.Application")


    With msWord
        .Visible = True
        .Documents.Open "N:\Test\Test.docx"
        .Activate
        
        With .ActiveDocument.Content.Find
            .ClearFormatting
            .Replacement.ClearFormatting


            For Each itm In ws.UsedRange.Columns("A").Cells


                .Text = itm.Value2                          'Find all strings in col A


                .Replacement.Text = itm.Offset(, 1).Value2  'Replacements from col B


                .MatchCase = False
                .MatchWholeWord = False


                .Execute Replace:=2     'wdReplaceAll (WdReplace Enumeration)
            
            Next
        End With
        .Application.ActiveDocument.SaveAs Filename:="N:\Test\" & fname & ".docx"


    End With

This error I get is Run-time Error '4198' Command Failed

Any help would be greatly appreciated.

Nick
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi @★ Nick_De, Welcome to the forum!

The error is because the fname variable is empty, at the beginning of the code you have this:
fname = cName
But cName is empty, therefore, fname is empty.

Just fill in the fname variable with some data and try again.
eg.
fname = "new doc"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top