PDA

View Full Version : Solved: Word 2003: Print Embedded Documents



matt_65
04-17-2008, 12:45 AM
Hello,
I have created VBA code that when ran will go through a document find any embedded documents, open them, print them, close the document.

The code functions well, however if the document had an embedded document and it was deleted with markup enabled Word throws an error when I run my script.

The error is the usual error popup window with the red cross, but it doesnt have any text, just an OK button.

My code is as follows:
Public Sub PRINT_Attachments()
'
' PRINT_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

For x = 1 To ActiveDocument.InlineShapes.Count
If ActiveDocument.InlineShapes(x).Type = 1 Then
If ActiveDocument.InlineShapes(x).OLEFormat.ProgID <> "Package" Then
ActiveDocument.InlineShapes(x).Activate
With ActiveWindow.View
.ShowRevisionsAndComments = False
.RevisionsView = wdRevisionsViewFinal
End With
ActiveDocument.PrintOut
ActiveDocument.Close

countx = countx + 1
End If
End If
Next
MsgBox "This Document contains: " & countx & " embedded documents"
End Sub

To replicate the issue I am experiencing:

Create a document and insert a few embedded documents (Insert > Object, tick the Icon box and click OK, put some text in the documents and close).
Enable document mark-up
Delete one of the documents
Run scriptI have attached a document which is what I have been using for testing, it contains two embedded documents one has been deleted with mark-up enabled, if you show markup you should see the second document that has been deleted.
The above process results in the error message I am experiencing everytime.

I would like to know if there is a way to skip any documents that have been deleted, or to add some kind of error support. As you can see from my code above, I have turned off document mark-up (so that it shows only the final version), but it still occurs.

Any assistance will be greatly appreciated.

Thanks,

Matt

fionabolt
04-17-2008, 02:48 AM
Matt,

You say :

As you can see from my code above, I have turned off document mark-up (so that it shows only the final version), but it still occurs.
However, you have set the revisions view on the embedded document you have just opened. You need to set this on the original source document, before you begin itterating through the shapes.
Option Explicit
Public Sub PRINT_Attachments()
'
' PRINT_Attachments Macro -- to print attachments in a doc
' Comments: TYPE=1 is an embedded object
Dim oDocument As Document
Dim x As Integer
Dim countx As Integer
Dim counte As Integer

oDocument = ActiveDocument

With Documents(oDocument).ActiveWindow.View
.ShowRevisionsAndComments = False
.RevisionsView = wdRevisionsViewFinal
End If

For x = 1 To Documents(oDocument).InlineShapes.Count
If Documents(oDocument).InlineShapes(x).Type = 1 Then
If Documents(oDocument).InlineShapes(x).OLEFormat.ProgID <> "Package" Then
Documents(oDocument).InlineShapes(x).Activate
'the activedocument is now the newly opened document
ActiveDocument.PrintOut
ActiveDocument.Close
countx = countx + 1
End If
End If
Next x
MsgBox "This Document contains: " & countx & " embedded documents"
End Sub

matt_65
04-17-2008, 09:10 PM
Fionabolt,
Thanks, for your help, unfourtanetly it still errors on the deleted document.

With your code it wouldnt work in my environment unless I did the following:
Set oDocument = ActiveDocument

I am not sure if this changes how your code was meant to function, without the Set, an error displays "Compile Error: Invalid use of property".

Your assistance is greatly appreciated.

Thanks,

Matt

lucas
04-17-2008, 11:07 PM
In cases like this, I'm not above adding an "On error resume next" and just let it bypass the error since we know what is causing it. You have to be careful though in other cases it's not a good idea to bypass errors.
Option Explicit
Public Sub PRINT_Attachments()
'
' PRINT_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
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
ActiveDocument.InlineShapes(x).Activate
With ActiveWindow.View
.ShowRevisionsAndComments = False
.RevisionsView = wdRevisionsViewFinal
End With
' ActiveDocument.PrintOut
ActiveDocument.PrintPreview
ActiveDocument.Close

countx = countx + 1
End If
End If
Next
MsgBox "This Document contains: " & countx & " embedded documents"
End Sub


I changed it to print preview for testing....you can comment that line and uncomment the prinout line to actually print.

matt_65
04-17-2008, 11:27 PM
Thanks Lucas, yes I too am not big on the idea of just skipping over errors as you never know when a new one may occur.

I have made some changes to the way that my code functions, I am now creating a copy of the document, accepting all revision changes (therfore removing the deleted document) and then iterating through the embedded documents and printing them out.

Option Explicit
Public Sub PRINT_Attachments()
'
' PRINT_Attachments Macro -- to print attachments in a doc
' Comments: TYPE=1 is an embedded object
Dim oDocument As Document
Dim x As Integer
Dim countx As Integer
Dim counte As Integer

Application.Documents.Add ActiveDocument.FullName
Set oDocument = ActiveDocument
With Documents(oDocument).ActiveWindow.View
WordBasic.AcceptAllChangesInDoc
End With

For x = 1 To Documents(oDocument).InlineShapes.Count
If Documents(oDocument).InlineShapes(x).Type = 1 Then
If Documents(oDocument).InlineShapes(x).OLEFormat.ProgID <> "Package" Then
Documents(oDocument).InlineShapes(x).Activate
' 'the activedocument is now the newly opened document
' ActiveDocument.PrintOut
ActiveDocument.Close
countx = countx + 1
End If
End If
Next x
ActiveDocument.Saved = True
ActiveDocument.Close
MsgBox "This Document contains: " & countx & " embedded documents"
End Sub

I have tested the code and it functions as I want it to.

I thank both Lucas and Fionabolt for your assistance, it has been very valuable.

Thanks,

Matt

lucas
04-17-2008, 11:31 PM
Hi Matt, thanks for posting your solution......don't feel like we helped much but we are good moral support.... :beerchug:

lescajul
06-09-2010, 09:45 AM
Hi. I have tried to use the last vestion of the code posted in this thread in a plain Word 2003 document with 2 inserted PDF objects (attached).

I need the code to open & print both atttachments, one after the other (this is the first part of my need, but I cannot get past it). I run into the following problem: the code will open the first attachment but will print the Contaning Word document instead (line #A#). The following line then closes both the open attachment and the Word document itself, thus finishing the code abnormally.

I am using Word 2003 with Windows XP SP2, both in Spanish.

Any idea what I could be doing wrong?

Thanks a lot. I have been reading posts from this site recently and found it awesome!

Julio