Force wdPrintDocumentWithMarkup

C

cklester

I'm showing the wdDialogFilePrint, but I would like to force
wdPrintDocumentWithMarkup. I tried something like:

Set didPrint = Dialogs(wdDialogFilePrint)
didPrint.Display
didPrint.Item = wdPrintDocumentWithMarkup
didPrint.Execute

but that doesn't work. :/

All the help I've seen suggests that I can only use
wdPrintDocumentWithMarkup with PrintOut. If true, will PrintOut use
the settings I've captured in the didPrint Dialog object?
 
J

Jay Freedman

I'm showing the wdDialogFilePrint, but I would like to force
wdPrintDocumentWithMarkup. I tried something like:

Set didPrint = Dialogs(wdDialogFilePrint)
didPrint.Display
didPrint.Item = wdPrintDocumentWithMarkup
didPrint.Execute

but that doesn't work. :/

All the help I've seen suggests that I can only use
wdPrintDocumentWithMarkup with PrintOut. If true, will PrintOut use
the settings I've captured in the didPrint Dialog object?

If you need to use wdPrintDocumentWithMarkup, you're stuck with using
the PrintOut method. The reason is a little complicated, and more than
a little stupid...

The parameters of the built-in dialogs in Word 97 and later are all
identical to the ones used in WordBasic in Word 95 and earlier. The
parameter of the wdDialogFilePrint dialog object that corresponds to
the Print What box in the dialog is named "Type". You can find this in
the FilePrint topic of the WordBasic help file, which is still
available for download from
http://www.microsoft.com/downloads/...FamilyID=1A24B2A7-31AE-4B7C-A377-45A8E2C70AB2.

The trouble with this is that in VBA, the Dialog object itself has a
Type property. The value of Dialogs(wdDialogFilePrint).Type is 88,
which is the value of the constant wdDialogFilePrint -- in other
words, it returns the answer to "what type of dialog is this?" This is
a read-only property, so code that _should_ work

Dim dlg As Dialog
Set dlg = Dialogs(wdDialogFilePrint)
With dlg
.Type = wdPrintDocumentWithMarkup
.Execute
End With

fails with a compile error "Can't assign to a read-only property".

What you can do instead is to use the .Display method of the dialog to
let the user set the desired controls, and then transfer the resulting
parameters to the PrintOut method, and add the
Item:=wdPrintDocumentWithMarkup parameter as you do that:

Dim dlg As Dialog
Set dlg = Dialogs(wdDialogFilePrint)
With dlg
If .Display = -1 Then
ActiveDocument.PrintOut _
Background:=True, _
Range:=.Range, _
from:=.from, to:=.to, _
Item:=wdPrintDocumentWithMarkup, _
Copies:=.numcopies, _
Pages:=.Pages
End If
End With

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
T

Tony Jollans

What you can do instead is to use the .Display method of the dialog to
let the user set the desired controls, and then transfer the resulting
parameters to the PrintOut method, and add the
Item:=wdPrintDocumentWithMarkup parameter as you do that:

Dim dlg As Dialog
Set dlg = Dialogs(wdDialogFilePrint)
With dlg
If .Display = -1 Then
ActiveDocument.PrintOut _
Background:=True, _
Range:=.Range, _
from:=.from, to:=.to, _
Item:=wdPrintDocumentWithMarkup, _
Copies:=.numcopies, _
Pages:=.Pages
End If
End With

Unfortunately this is not a perfect solution as error handling does not work
properly with the Display method. I have just checked and this is still the
case in Word 2007.

For example, if a user enters "abc" in the Pages box, the dialog recognises
the error and redisplays but without any error message. If the user manages
to correct the error the dialog will then show the error message it should
have shown earlier and return the error, rather than the correction, to the
VBA code.

So the method will work properly only if the user never makes a mistake :)
 
T

Tony Jollans

LOL!

Not you, Jay, of course not you! It's those pesky users - I think they do it
on purpose <g>
 
C

cklester

If you need to usewdPrintDocumentWithMarkup, you're stuck with using
the PrintOut method. The reason is a little complicated, and more than
a little stupid...

Thanks, Jay, for the explanation and solution. :)
 
C

cklester

If you need to usewdPrintDocumentWithMarkup, you're stuck with using
the PrintOut method. The reason is a little complicated, and more than
a little stupid...

Ummmm... trying to make this backward compatible and failing
miserably. Jay, can you revise it?

When used in, for example, Word 2000, wdPrintDocumentWithMarkup can
even be bypassed with On Error Resume Next. I figure I would assign it
to a variable and if it can't get assigned, that means it's < 2003. No
such luck.

Help?! :)
 
J

Jay Freedman

Ummmm... trying to make this backward compatible and failing
miserably. Jay, can you revise it?

When used in, for example, Word 2000, wdPrintDocumentWithMarkup can
even be bypassed with On Error Resume Next. I figure I would assign it
to a variable and if it can't get assigned, that means it's < 2003. No
such luck.

Help?! :)

Can you wait a few hours? I'm at work now, where I have only Word 2003 to
play with. When I get home I can try other versions (Virtual PC is good that
way!).

Meanwhile, you can check explicitly what version is running your code by
interrogating the value of Application.Version. It returns a string, such as
"11.0" for Word 2003 or "9.0" for Word 2000. Use that in an If statement to
execute one or the other copy of the PrintOut method.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Jay Freedman

Can you wait a few hours? I'm at work now, where I have only Word 2003 to
play with. When I get home I can try other versions (Virtual PC is good that
way!).

Meanwhile, you can check explicitly what version is running your code by
interrogating the value of Application.Version. It returns a string, such as
"11.0" for Word 2003 or "9.0" for Word 2000. Use that in an If statement to
execute one or the other copy of the PrintOut method.

OK, the light dawns...

In Word 2000, there is no way to toggle the visibility of tracked
changes; either they're visible, or they've been accepted/rejected.
Extending that to printing, there's no choice in the Print dialog to
print with or without markup; you get what's visible in the document.

In VBA, Word 2000 has no such constant as wdPrintDocumentWithMarkup,
as you can see by looking at the members of the WdPrintOutItem
collection in the object browser. In later versions that constant has
the value 7, while in Word 2000 the valid values are only 0 through 6.

A macro like this should work in either version, as I mentioned
before:

Sub foo()
Dim dlg As Dialog
Set dlg = Dialogs(wdDialogFilePrint)
With dlg
If .Display = -1 Then
If Val(Application.Version) > 9# Then
ActiveDocument.PrintOut _
Background:=False, _
Range:=.Range, _
from:=.from, to:=.to, _
Item:=wdPrintDocumentWithMarkup, _
Copies:=.numcopies, _
Pages:=.Pages
Else
' let the Item parameter default
' to wdPrintDocumentContent
ActiveDocument.PrintOut _
Background:=False, _
Range:=.Range, _
from:=.from, to:=.to, _
Copies:=.numcopies, _
Pages:=.Pages
End If
End If
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
C

cklester

A macro like this should work in either version, as I mentioned
before:

The reason that code won't work in Word 2000 (at least it doesn't work
for me) is because wdPrintDocumentWithMarkup looks like an undefined
variable, so I get the "Compile Error: Variable Not Defined" message.
You said that constant has a value of 7, so I just replaced the
variable name with the value 7 and it seems to work in 2000. I haven't
tested that in 2003 yet, but I will today.

Thanks for the help! :)
 
J

Jay Freedman

The reason that code won't work in Word 2000 (at least it doesn't work
for me) is because wdPrintDocumentWithMarkup looks like an undefined
variable, so I get the "Compile Error: Variable Not Defined" message.
You said that constant has a value of 7, so I just replaced the
variable name with the value 7 and it seems to work in 2000. I haven't
tested that in 2003 yet, but I will today.

Thanks for the help! :)

Ah, I know what happened.

You have Option Explicit at the top of your module to tell VBA to consider
undeclared variables as errors. That's highly recommended, and I normally
use it in Word 2003 and 2007, the programs I use all the time.

However, I have a copy of Word 2000 installed in a Virtual PC partition,
which I use maybe five times a year. I didn't notice while I was testing
this code that I don't have Option Explicit turned on there. (Curses to MS
for not having it turned on by default...) So VBA just assumed that
wdPrintDocumentWithMarkup was an undeclared variable, initialized it to 0,
and proceeded. Since the code didn't execute that line, it all looked good.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top