Save embedded documents

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
Can someone give me some advice on how I can save 2 embedded Word documents?

I have 2 Word templates embedded in my Excel workbook and I want to extract them and save them to the same path as the workbook - I don't want to open them, just save them.

Anyone?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this. It requires a reference to MS Word Object Library and assumes the two Word objects are named "Object 1" and "Object 2" on Sheet1.
Code:
Public Sub Save_Embedded_Word_Documents()

    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Dim WordFileName As String
    Dim embeddedWordDocument As Object
    
    On Error Resume Next
    Set WordApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then 'Word isn't already running
        Set WordApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    WordApp.Visible = False
    
    'Open and save Word document object "Object 1" on Sheet1
    
    Set embeddedWordDocument = ThisWorkbook.Sheets("Sheet1").OLEObjects("Object 1")
    embeddedWordDocument.Verb Verb:=xlOpen

    Set WordDoc = WordApp.ActiveDocument
    WordDoc.SaveAs ThisWorkbook.Path & "/Embedded_Document1_Saved.docx", FileFormat:=wdFormatXMLDocument
    WordDoc.Close True
    
    'Open and save Word document object "Object 2" on Sheet1
    
    Set embeddedWordDocument = ThisWorkbook.Sheets("Sheet1").OLEObjects("Object 2")
    embeddedWordDocument.Verb Verb:=xlOpen

    Set WordDoc = WordApp.ActiveDocument
    WordDoc.SaveAs ThisWorkbook.Path & "/Embedded_Document2_Saved.docx", FileFormat:=wdFormatXMLDocument
    WordDoc.Close True
    
    WordApp.Quit False
    
    Set WordDoc = Nothing
    Set WordApp = Nothing
    Set embeddedWordDocument = Nothing
  
End Sub
 
Upvote 0
Thanks John, this works great...

Can I ask one more thing though?

Is there a way I can hide the document whilst it is being opened? I have tried setting the screenupdating to false but that does nothing.

Thanks!
 
Upvote 0
It seems you can't hide the document on opening because the Verb:=xlOpen command briefly shows the document.

I have added WordApp.WindowState = wdWindowStateMinimize and moved WordApp.Visible = False after each open, but the documents are still visible briefly.
Code:
Public Sub Save_Embedded_Word_Documents()

    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Dim WordFileName As String
    Dim embeddedWordDocument As Object
    
    On Error Resume Next
    Set WordApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then 'Word isn't already running
        Set WordApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    WordApp.WindowState = wdWindowStateMinimize

    'Open and save Word document object "Object 1" on Sheet1
    
    Set embeddedWordDocument = ThisWorkbook.Sheets("Sheet1").OLEObjects("Object 1")
    embeddedWordDocument.Verb Verb:=xlOpen
    WordApp.Visible = False
    
    Set WordDoc = WordApp.ActiveDocument
    WordDoc.SaveAs ThisWorkbook.Path & "/Embedded_Document1_Saved.docx", FileFormat:=wdFormatXMLDocument
    WordDoc.Close False
    
    'Open and save Word document object "Object 2" on Sheet1
    
    Set embeddedWordDocument = ThisWorkbook.Sheets("Sheet1").OLEObjects("Object 2")
    embeddedWordDocument.Verb Verb:=xlOpen
    WordApp.Visible = False

    Set WordDoc = WordApp.ActiveDocument
    WordDoc.SaveAs ThisWorkbook.Path & "/Embedded_Document2_Saved.docx", FileFormat:=wdFormatXMLDocument
    WordDoc.Close False
    
    WordApp.Quit False
    
    Set WordDoc = Nothing
    Set WordApp = Nothing
    Set embeddedWordDocument = Nothing
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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