PDA

View Full Version : [SOLVED:] Export embedded Word document and set data



Cosmo
09-23-2013, 07:51 AM
I need to save a Word document that has been inserted into an Excel file, then be able to populate it with values entered into the Excel document. My experience with VBA in Word and Excel is very limited (most of my work has involved PowerPoint), and I haven't seen exactly how to save out the Word document.

Can anyone point me in the correct direction to save the document into a specific location, and reference it's objects? (I specifically need to fill data into various cells in a table)

Kenneth Hobs
09-23-2013, 11:19 AM
Sub OpenADoc()
Dim mpWord As Object
Dim mpDoc As Object

ActiveSheet.OLEObjects("Object 1").Verb xlPrimary
Do
Set mpWord = GetObject(, "Word.Application")
mpWord.Visible = True
Set mpDoc = mpWord.ActiveDocument
Loop Until Not mpDoc Is Nothing

With mpDoc
.Range(.Content.Start, .Content.End).Copy
End With

mpDoc.Close
mpWord.Quit
Set mpDoc = Nothing
Set mpWord = Nothing

Worksheets("Sheet2").Paste
End Sub

Cosmo
09-23-2013, 02:02 PM
Thanks! That looks like that should get me pretty well started.



Sub OpenADoc()
Dim mpWord As Object
Dim mpDoc As Object

ActiveSheet.OLEObjects("Object 1").Verb xlPrimary
Do
Set mpWord = GetObject(, "Word.Application")
mpWord.Visible = True
Set mpDoc = mpWord.ActiveDocument
Loop Until Not mpDoc Is Nothing

With mpDoc
.Range(.Content.Start, .Content.End).Copy
End With

mpDoc.Close
mpWord.Quit
Set mpDoc = Nothing
Set mpWord = Nothing

Worksheets("Sheet2").Paste
End Sub