View Full Version : Saving embedded documents in word.
Phuzzy
02-15-2012, 03:46 PM
I found a script on this site that prints all the embedded documents in a Word file. I modified it to cycle through all the embedded documents and save them. However, it gets to a certain file and opens it but instead saves and closes the file with the embedded documents and not the one that is embedded. After doing some troubleshooting/debugging, I found that the for the 'good' files the ActiveDocument.InlineShapes(x).OLEFormat.ProgID is set to Word.Document.8 whereas the 'Problem' embedded file is 'Outlook.FileAttach'. All files are type=1. This file opens in word, looks like a word document and acts like a word document. I'd like to know how to get this problem document to Activate, Save then close like all the other ones.
I am doing all this on Word 2003 and Windows XP. I have about 1 week of VBA Experience so dumb it down.
The original script is from here. It won't let me post links....
www . vbaexpress. com / forum / archive / index . php / t-19070. html
Here is my altered code.
Option Explicit
Public Sub Save_Attachments()
'
' Save_Attachments Macro -- to print attachments in a doc
' Comments: TYPE=1 is an embedded object
Dim x As Integer
Dim countx As Integer
Dim counte As Integer
Dim fileNameVar As Integer
Dim newFileName As String
Dim filePath As String
Dim mainFile As String
Dim textvarint As Integer
Dim testvar As String
filePath = ActiveDocument.Path
mainFile = ActiveDocument.Name
fileNameVar = 1
On Error Resume Next
For x = 1 To ActiveDocument.InlineShapes.Count
If ActiveDocument.InlineShapes(x).Type = 1 Then
If ActiveDocument.InlineShapes(x).OLEFormat.ProgID <> "Package" Then
textvarint = ActiveDocument.InlineShapes(x).Type ' Used for debugging
testvar = ActiveDocument.InlineShapes(x).OLEFormat.ProgID ' Used for debugging
ActiveDocument.InlineShapes(x).Activate
'Disable the next 4 lines and the script no longer saves this doc and closes it. It continues but the problem doc(s) do not get saved.
'With ActiveWindow.View
'.ShowRevisionsAndComments = False
'.RevisionsView = wdRevisionsViewFinal
'End With
newFileName = filePath + "\Attachment" + Str(fileNameVar) + ".doc"
If ActiveDocument.Name <> mainFile Then ActiveDocument.SaveAs (newFileName) ' Preventing my file from being rewritten
If ActiveDocument.Name <> mainFile Then ActiveDocument.Close ' Preventing my file from being closed
fileNameVar = fileNameVar + 1
countx = countx + 1
End If
End If
Next
MsgBox "This Document contains: " & countx & " embedded documents"
End Sub
fumei
02-15-2012, 05:27 PM
is 'Outlook.FileAttach'. All files are type=1. This file opens in word, looks like a word document and acts like a word document
except it is not a Word document. That is, it is not using the Word executable to display itself to you. It is using pseudo-ersatz Outlook sort-of Word.
Frosty
02-15-2012, 05:58 PM
Does the behavior go away if you check the options to use Word 2003 as your email editor?
In Outlook 2003, Tools > Options > Mail Format > check the two options that begin "Use Microsoft Office Word 2003 to..."
Frosty
02-15-2012, 06:05 PM
Couple additional notes-
1. Get rid of your On Error Resume Next -- that's a problem for debugging.
2. I'm impressed with your debugging skills with only a week in VBA-- guessing you have some other programming experience, so I will continue with #3
3. I think you need to separate out which files you want to skip. "Package" won't be enough, since you potentially have Chart Objects, Excel Spreadsheets, Powerpoint objects, etc. In addition, since you're only checking Inline objects, you'll be skipping any embedded objects which are "floating".
This is pretty dangerous code to rely on, the way it is structured.
Check out using the VBA tags for ease of reading (and indents), and then check out using a Select Case statement in the following manner:
Select Case ActiveDocument.InlineShapes(x).OLEFormat.ProgID
Case "Package", "Something else"
'Skip these
Case Else
'work on these
End If
That will let you add additional prog ids for embedded objects you want to skip, rather than your IF yada yada <> "Package" Then structure.
Phuzzy
02-16-2012, 08:23 AM
Does the behavior go away if you check the options to use Word 2003 as your email editor?
In Outlook 2003, Tools > Options > Mail Format > check the two options that begin "Use Microsoft Office Word 2003 to..."
I thought that this solution was too easy a fix to work. And I was right. I was hopeful it might have something to do with it when you mentioned it, but no.
Phuzzy
02-16-2012, 08:41 AM
Couple additional notes-
1. Get rid of your On Error Resume Next -- that's a problem for debugging.
2. I'm impressed with your debugging skills with only a week in VBA-- guessing you have some other programming experience, so I will continue with #3
3. I think you need to separate out which files you want to skip. "Package" won't be enough, since you potentially have Chart Objects, Excel Spreadsheets, Powerpoint objects, etc. In addition, since you're only checking Inline objects, you'll be skipping any embedded objects which are "floating".
This is pretty dangerous code to rely on, the way it is structured.
Check out using the VBA tags for ease of reading (and indents), and then check out using a Select Case statement in the following manner:
Select Case ActiveDocument.InlineShapes(x).OLEFormat.ProgID
Case "Package", "Something else"
'Skip these
Case Else
'work on these
End If
That will let you add additional prog ids for embedded objects you want to skip, rather than your IF yada yada <> "Package" Then structure.
Thanks for your help.
1. I'll get rid of the On error....
2. I programmed in Basic way back in the 8 bit days, some assembly and more recently a semester of Java at college.
3. I would really like to be able to separate the various values for ProgID, but I don't know what they all could be. I am finding that information on the Word Object Model is scarce to say the least and I won't even go into how I feel about the MSDN website.
Although the ProgID of this troublesome file is Outlook.FileAttach, is there a way, programmatically to force Word to open it or to make it behave like all the other files.
This baffles me (not hard to do) because it opens in (what appears to be) Word when it hits the .activate. Is it a different instance of Word? A Word look-alike? Can this problem document be detected then converted to a Word.Document.8 within my script, then saved?
Thanks again.
Phuzzy
02-16-2012, 09:08 AM
except it is not a Word document. That is, it is not using the Word executable to display itself to you. It is using pseudo-ersatz Outlook sort-of Word.
Okay, now that we know that it is not quite a word file is there anything that can be done so that it saves like a word file?
Phuzzy
02-16-2012, 10:46 AM
UPDATE. I modified the code to use the ActiveAs method, thusly:
With ActiveDocument.InlineShapes(x).OLEFormat
.ActivateAs ClassType:="Word.document"
.Activate
End With
It all works good until of course, the problem file. It throws an error 6069. See attached file...
After I get out of that dialog and go back to word. Word throws an error that says "Word cannot start the converter mswrd632.wpc." After I click okay to that a Convert File dialog appears. It gives me a list of formats I can convert to.
SO. I think I might be on the right track with the ActivateAs method but I think that I need to convert it to a format (RTF Rich Text Format) before I use the save method.
Thoughts??!?!?
Frosty
02-16-2012, 02:09 PM
My thought is that you need to prepare a dummy document with some embedded objects of the same types here, and then I can play with it. The problem with specifying Word is that you're simply telling it to convert something it can't convert. If it was that easy, it would have worked already.
Since it's trying to use the wordperfect converter, my guess is that the embedded document is some sort of xml format (maybe html email?). But you're not going to have a one-size-fits-all solution if this is an embedded email object of various formats (RTF, HTML, plain text).
In any event-- it's all a bunch of guesses to see if anything sticks. To really help, I need some kind of working document to test. Can you work one up that doesn't have any sensitive info in it?
Otherwise, I would simply code it to handle the items you can, and flag in some manner the ones you can't (bookmarks, etc). Then go back and deal with the flagged ones manually.
Check out Debug.Print in terms of getting prog ID... that will put stuff in the immediate window so you don't have to necessarily put a break point if you're dealing with a large number of embedded objects...
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.