[msword/vba] Problem with showing/hiding shapes

D

dwergkees

Got a strange problem:

A document has a corporate logo, which doesn't need to be printed on
the clients pc (as he has preprinted paper). The logo does need to be
printed other computers (when client for example mails a document).

My solution is as follows: intercept the print commands on clients pc,
hide the logo before printing, show it again when ready.

Code is on bottom of page. The strange problem is: When I open the
document and try to print, it doesn't work the first time, but it does
work the second. Both times the sub is called and processed with no
errors, as both times I get the messageBoxes(!). Also, it does work
directly when the VBA window is open.

Any ideas?

Code:

Sub FilePrint()
'To intercept File > Print and CTRL-P
toggleLogos

End Sub

Sub FilePrintDefault()
'To intercept the Standard toolbar button
toggleLogos True

End Sub

Sub toggleLogos(Optional toolbar As Boolean = False)

Dim logo As Shape, headerLogo As Shape

' fetch prenamed shapes
Set logo = Application.ActiveDocument.Shapes("Logo")
Set headerLogo =
Application.ActiveDocument.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Shapes("LogoHeader")

' Just to check if this sub is called properly
MsgBox logo.Name
MsgBox headerLogo.Name

' hide shapes
logo.Visible = msoFalse
headerLogo.Visible = msoFalse

' show print dialog or just spit it out
If toolbar Then
ActiveDocument.PrintOut
Else
Dialogs(wdDialogFilePrint).Show
End If

' show shapes
logo.Visible = msoTrue
headerLogo.Visible = msoTrue

End Sub
 
J

Jean-Guy Marcil

dwergkees was telling us:
dwergkees nous racontait que :
Got a strange problem:

A document has a corporate logo, which doesn't need to be printed on
the clients pc (as he has preprinted paper). The logo does need to be
printed other computers (when client for example mails a document).

My solution is as follows: intercept the print commands on clients pc,
hide the logo before printing, show it again when ready.

Code is on bottom of page. The strange problem is: When I open the
document and try to print, it doesn't work the first time, but it does
work the second. Both times the sub is called and processed with no
errors, as both times I get the messageBoxes(!). Also, it does work
directly when the VBA window is open.

So it does not work the first time you call the macro, but every time after
that it works, until you close the document. Right?

What if you change
logo.Visible = msoFalse
headerLogo.Visible = msoFalse
to
logo.Visible = False
headerLogo.Visible = False

and
logo.Visible = msoTrue
headerLogo.Visible = msoTrue
to
logo.Visible = True
headerLogo.Visible = True
???

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

dwergkees

What if you change
logo.Visible = msoFalse
headerLogo.Visible = msoFalse
to
logo.Visible = False
headerLogo.Visible = False

already tried and tested, just as error trapping on or off (on error
resume ...)
walking through the code doesn't help, as it always work when the vba
window is open :(
 
J

Jean-Guy Marcil

dwergkees was telling us:
dwergkees nous racontait que :
already tried and tested, just as error trapping on or off (on error
resume ...)
walking through the code doesn't help, as it always work when the vba
window is open :(

Well, I am not sure what to write at this point. The code you posted seemed
OK.
(
Except maybe that
Set headerLogo =
Application.ActiveDocument.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Shapes("LogoHeader")
was a little "overkill," this would have worked as well:
Set headerLogo =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("LogoHeader")
)

So I am not sure I can help, except if you do not mind me looking at your
document. Un-mangled my email address in my signature if you want me to take
a quick look at it.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

dwergkees

Well, I am not sure what to write at this point. The code you posted seemed

Yeah, that's my problem, it all * seems * OK, it's beginning to look
like a vba/word bug by now :)

Anyway, i mailed you a document containing both code and images (real
app has code in normal.dot) wich reproduces the exact problem. Just
press File > Print, then Cancel and then repeat the procedure. the
first time the images won't hide (but the msgboxes appear), the second
time the images will hide. (at least on the two pc's i tested on).

TIA!

Wilco Menge (aka Dwergkees)
 
J

Jean-Guy Marcil

dwergkees was telling us:
dwergkees nous racontait que :
Yeah, that's my problem, it all * seems * OK, it's beginning to look
like a vba/word bug by now :)

Anyway, i mailed you a document containing both code and images (real
app has code in normal.dot) wich reproduces the exact problem. Just
press File > Print, then Cancel and then repeat the procedure. the
first time the images won't hide (but the msgboxes appear), the second
time the images will hide. (at least on the two pc's i tested on).

TIA!

Wilco Menge (aka Dwergkees)

Sorry, but...
Here is what I did:

1. I open the document you sent and saw the 2 logos
2. I hit CTRL-P
3. Right away I noticed the 2 logos become invisible
4. The print dialog appeared
5. I hit Cancel
6. The logos became visible again
7. The 2 message boxes appeared

Then, as you mentioned when you wrote to me directly, just in case, I
transferred the code to Normal.dot and tried again.
Same result.

So... what is going on at your end of things?

One thing I forgot to ask from the get go... What Office version are you
using?
That might make a difference.
I tested your document on Office 2003 SP2.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

dwergkees

at your side it works as expected, so it seems to be my setup. my
setup: winXP sp2, office 2003 sp2 (the same as yours it seems)

the thing that goes wrong at my end is point 3. the logo's wont go
invisible. when i repeat the steps it does. seems to me like some sort
of bug, i am not going to track anymore but blame it on ms instead :)

thanks for looking into it!

dwergkees
 

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