Hi all,
I am writing a VBA Word macro in Office 2007 that will reformat a long document that contains many exported emails about a certain topic. One problem is that attachments get exported as is, and are embedded in the document as objects.
I want to write a subroutine that will:
[ol 1]
[li]Scan the document for embedded objects (they will almost always be Word documents; very rarely Excel)[/li]
[li]Open each attachment[/li]
[li]Copy the contents[/li]
[li]Close the attachment[/li]
[li]Go back to the original document, and replace the object with the actual text[/li]
[/ol]
I'm entirely self-taught, and everything I have is based on recording-reading and online research. Here's what I have so far, followed by the present (perhaps last) sticking point:
Sub findEmbed()
Dim oSh As InlineShape
For Each oSh In ActiveDocument.InlineShapes
If oSh.Type = wdInlineShapeEmbeddedOLEObject Then
'This is here because, when activating the object, I don't know how to bypass the dialog box asking what to do with the file
MsgBox ("Please select ""Open"" in the following dialog box")
oSh.OLEFormat.Activate
'The issue arises here
ActiveWindow.Selection.WholeStory
Selection.Copy
Debug.Print (oSh.OLEFormat.IconPath)
ActiveWindow.Close
Windows("homeDocument").Activate
Selection.PasteAndFormat (wdPasteDefault)
The problem is that when I activate the object, it opens as a new instance of Word, and it does not become the active window. So it's actually the original document that ends up getting selected and copied.
I guess my main question is: How do I get the newly-opened object to be the active window so that I can select it?
Eager to hear your ingenious responses!
-Michael
I am writing a VBA Word macro in Office 2007 that will reformat a long document that contains many exported emails about a certain topic. One problem is that attachments get exported as is, and are embedded in the document as objects.
I want to write a subroutine that will:
[ol 1]
[li]Scan the document for embedded objects (they will almost always be Word documents; very rarely Excel)[/li]
[li]Open each attachment[/li]
[li]Copy the contents[/li]
[li]Close the attachment[/li]
[li]Go back to the original document, and replace the object with the actual text[/li]
[/ol]
I'm entirely self-taught, and everything I have is based on recording-reading and online research. Here's what I have so far, followed by the present (perhaps last) sticking point:
Sub findEmbed()
Dim oSh As InlineShape
For Each oSh In ActiveDocument.InlineShapes
If oSh.Type = wdInlineShapeEmbeddedOLEObject Then
'This is here because, when activating the object, I don't know how to bypass the dialog box asking what to do with the file
MsgBox ("Please select ""Open"" in the following dialog box")
oSh.OLEFormat.Activate
'The issue arises here
ActiveWindow.Selection.WholeStory
Selection.Copy
Debug.Print (oSh.OLEFormat.IconPath)
ActiveWindow.Close
Windows("homeDocument").Activate
Selection.PasteAndFormat (wdPasteDefault)
The problem is that when I activate the object, it opens as a new instance of Word, and it does not become the active window. So it's actually the original document that ends up getting selected and copied.
I guess my main question is: How do I get the newly-opened object to be the active window so that I can select it?
Eager to hear your ingenious responses!
-Michael