Alex Kitson
Alex Kitson

Reputation: 93

How to save an embedded Word document in an Excel workbook as a separate file using VBA

I am creating a workbook that will populate an embedded Word document template with pictures, and then save the document elsewhere, without editing the embedded template. However, when I try to save the document, I get a Run-Time Error 4605 telling me: "The SaveAs method or property is not available because this document is being edited in another applicator"

This is the sub to open the template:

Sub OpenWord()
'Opens the template when the main function first runs

    Set WDObj = Sheets("Template").OLEObjects("Template")

    WDObj.Activate
    WDObj.Object.Application.Visible = False

    Set WDApp = GetObject(, "Word.Application")

    Set WDDoc = WDApp.ActiveDocument                   

End Sub

After this a main sub runs which populates the template, then when I try to save the document using:

WDDoc.SaveAs "myDocument.doc", FileFormat:=wdFormatDocumentDefault

I get the error.

Please has anyone encountered this error before/ know to to fix it, I have done much Googling which still has not gotten me anywhere.

Upvotes: 1

Views: 761

Answers (1)

Paul Ogilvie
Paul Ogilvie

Reputation: 25266

I tested the following, which worked on my system/installation:

Sub OpenWord()
'Opens the template when the main function first runs

    Dim WDObj As Object
    Dim WDApp As Object

    Set WDApp = GetObject(, "Word.Application")
    Set WDObj = Sheets("Template").OLEObjects("Template")

    WDObj.Activate
    WDObj.Object.Application.Visible = False

    WDApp.ActiveDocument.SaveAs ("YourFilename.doc")
    Set WDObj = Nothing
    Set WDApp = Nothing

End Sub

Upvotes: 1

Related Questions