PDA

View Full Version : Solved: Remove Header Images Before Print



cmorphew
04-08-2005, 12:14 PM
I'm having a problem getting the machines in my office to remove header images in a word document before printing. I've tried to use the following:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=104

The first few times I tried it I got the following error:
Run-time error '4605':
This method or property is not available because no text is selected.

Now when I run it nothing happens, I don't even get a message prompt. I'm running Office 2000 Pro on Windows 2000 Pro. I've also tried it on a XP Home with Office 2003 as well. Thanks!

fumei
04-08-2005, 12:44 PM
The following removes all images (as InlineShapes) from all headers, in all Sections.

NOTE # 1: this does not remove Shapes, but it could.
NOTE # 2: this does NOT remove TEXT from the headers. Any text in the headers is left alone.
Sub RemoveHeaderImages()
Dim ThisImage As InlineShape
Dim mySection As Section
Dim myHF As HeaderFooter
For Each mySection In ActiveDocument.Sections()
For Each myHF In mySection.Headers
If myHF.Range.InlineShapes.Count > 0 Then
' just in case there is more than one image
For Each ThisImage In myHF.Range.InlineShapes
ThisImage.Delete
Next
End If
Next
Next
End Sub

cmorphew
04-09-2005, 08:17 AM
Thanks, I'll try it. Before I get too far in this, will this work in all versions of Word? I just found out the office I'm doing this for has Office 97 through Office 2003!

MOS MASTER
04-09-2005, 12:36 PM
Thanks, I'll try it. Before I get too far in this, will this work in all versions of Word? I just found out the office I'm doing this for has Office 97 through Office 2003!Hi, :D

Don't have 97 at hand right now but the objects used by Fumei I think are used by all versions > 97 so it will properbly work..(A test always gives the final answer)

The code Fumei gave was very good but you should consider the folowing:
* Word has an InlineShape model but also have a Shape model
* The code provided by Fumei does not take care off those (Flowting) shapes.

So if you want all shapes and inlineshapes to be deleted from you're headers then you can use this (Code by Fumei) I've altered:
Sub RemoveHeaderImages()
Dim ThisImage As InlineShape
Dim mySection As Section
Dim myHF As HeaderFooter
For Each mySection In ActiveDocument.Sections()
For Each myHF In mySection.Headers
If myHF.Range.InlineShapes.Count > 0 Then
' just in case there is more than one image
For Each ThisImage In myHF.Range.InlineShapes
ThisImage.Delete
Next
End If
If myHF.Range.ShapeRange.Count > 0 Then
myHF.Range.ShapeRange.Delete
End If
Next
Next
End Sub
Enjoy! :thumb

MOS MASTER
04-10-2005, 01:27 AM
NOTE # 1: this does not remove Shapes, but it could.
NOTE # 2: this does NOT remove TEXT from the headers. Any text in the headers is left alone.Hi Fumei, :D

Sorry, I've missed you're Notes yesterday. :banghead: (off course you did think about the Shapes)

I?ll leave the code stand as is because most users don?t know them selves if they are using Shapes or InlineShapes. (I always check for the both)

@cmorphew,

I?ve tested the code in ?97, 2000, 2002, 2003 so it should work well for you. ;)

cmorphew
04-11-2005, 09:55 AM
That did delete the headers. What I'm really looking for is something more like DRJ wrote where it prompted the user, then based on their response, removed the headers, print, and then replaced the headers. It doesn't work at all on my machine (2000 pro, office pro) and from what I understand it won't work at all in Word 97 (I think the DocumentBeforePrint event was made available to VBA in Word 2000)

I am VERY green to this and I'm not sure what I'm looking for is even possible.

MOS MASTER
04-11-2005, 10:13 AM
Hi, :D

I wasn't aware we where discussing DocumentBeforePrint event over here?
I don't see that in Fumei's code or my addon to his version?

I've just seen the link you're refering to in you're first message. Missed that one..sorry.

I did a test on the code I last put in to this forum on as well as my memory works on 97 to 2003 so I don't know why this sub isn't working for you.

Can you please confirm that my last sub doesn't run for you. Then I will do a more extensive test in 2000. :thumb

Hi, :D

Instead of application Events to trap the printbutton you can use Word Buildin Macros to capture that event.

Like: (Untested)Option Explicit
Sub FilePrint() 'Catches File/Print command
On Error GoTo Oops
If MsgBox("Do you wish to delete all pictures in headers", _
vbQuestion + vbYesNo, "Delete Pictures") = vbYes Then
RemoveHeaderImages
Dialogs(wdDialogFilePrint).Show
End If
Oops:
End Sub
Sub FilePrintDefault() 'Catches Toolbar Print button
On Error GoTo Oops
If MsgBox("Do you wish to delete all pictures in headers", _
vbQuestion + vbYesNo, "Delete Pictures") = vbYes Then
RemoveHeaderImages
ActiveDocument.PrintOut
End If
Oops:
End Sub
Sub RemoveHeaderImages()
Dim ThisImage As InlineShape
Dim mySection As Section
Dim myHF As HeaderFooter
For Each mySection In ActiveDocument.Sections()
For Each myHF In mySection.Headers
If myHF.Range.InlineShapes.Count > 0 Then
' just in case there is more than one image
For Each ThisImage In myHF.Range.InlineShapes
ThisImage.Delete
Next
End If
If myHF.Range.ShapeRange.Count > 0 Then
myHF.Range.ShapeRange.Delete
End If
Next
Next
End Sub

Give it a go and send me the feedback!

Enjoy.:thumb

cmorphew
04-11-2005, 10:19 AM
Hi! Thanks for being so quick. I am very new to this and am not sure exactly how to use the code you gave me but here is what I tried.

The result was the file printed then the header image was deleted and then I get the following error:

Run-time error '5852':

Requested object is not available.

When I debug the following line is highlighted

If myHF.Range.ShapeRange.Count > 0 Then


Sub FilePrint()
'
' FilePrint Macro
' Prints the active document
'
Dialogs(wdDialogFilePrint).Show
Dim ThisImage As InlineShape
Dim mySection As Section
Dim myHF As HeaderFooter
For Each mySection In ActiveDocument.Sections()
For Each myHF In mySection.Headers
If myHF.Range.InlineShapes.Count > 0 Then
' just in case there is more than one image
For Each ThisImage In myHF.Range.InlineShapes
ThisImage.Delete
Next
End If
If myHF.Range.ShapeRange.Count > 0 Then
myHF.Range.ShapeRange.Delete
End If
Next
Next
End Sub

MOS MASTER
04-11-2005, 10:23 AM
Hi, :D

The code seams to lose reference to the range object of the header. Not shure why this is happening. He's deleting the pictures so it's properbly the last loop that's causing this!

In my test i didn't get this one but no machine is the same.

So let's test with an On error resume Next:
Sub FilePrint()
'
' FilePrint Macro
' Prints the active document
'
Dim ThisImage As InlineShape
Dim mySection As Section
Dim myHF As HeaderFooter

On Error Resume Next
For Each mySection In ActiveDocument.Sections()
For Each myHF In mySection.Headers
If myHF.Range.InlineShapes.Count > 0 Then
' just in case there is more than one image
For Each ThisImage In myHF.Range.InlineShapes
ThisImage.Delete
Next
End If
If myHF.Range.ShapeRange.Count > 0 Then
myHF.Range.ShapeRange.Delete
End If
Next
Next
Dialogs(wdDialogFilePrint).Show
End Sub

Enjoy! :thumb

cmorphew
04-11-2005, 10:30 AM
The On Error Resume Next worked the Header was deleted and then Printed!:clap: How do I get the image back after it prints?

MOS MASTER
04-11-2005, 11:28 AM
Hi, :D
You're Welcome! :beerchug:

Well the easy thing to do is put a save command in front off the bitt that deletes all the pictures and after printing just close the document without saving...

Enjoy!

C, are you there yet? Or...:whistle:

cmorphew
04-11-2005, 12:04 PM
I've been playing around with this for a bit and I thought I had it by recording a macro and trying to get it to work with some of the code you supplied. I think I'm close but I'm getting the following error:

Object variable or With block variable not set

Am I close? Or should I just give up and go get a drink!:banghead:

Option Explicit
Sub FilePrint()
If MsgBox("Do you wish to delete all pictures in headers", _
vbQuestion + vbYesNo, "Delete Pictures") = vbYes Then
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Shapes("Picture 8").Select
Selection.Cut
ActiveDocument.PrintOut
Selection.Paste
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

Else
ActiveDocument.PrintOut
End If
End Sub

Sub FilePrintDefault()
If MsgBox("Do you wish to delete all pictures in headers", _
vbQuestion + vbYesNo, "Delete Pictures") = vbYes Then

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Shapes("Picture 8").Select
Selection.Cut
ActiveDocument.PrintOut
Selection.Paste
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Else
ActiveDocument.PrintOut
End If
End Sub

MOS MASTER
04-11-2005, 12:23 PM
Hi, :D

Wow this is really bad coding (You recorded this I see) and Fumei did such a nice feature for you...:(

You're code is failing on: Selection.HeaderFooter.Shapes("Picture 8").Select
Because Picture 8 no longer exists...word numberings goes to the next..

But now i see what you want have a moment and I'll code it out for you with the old method! :thumb

Hi C, :D

Well did a quick test and this seams to work:
Option Explicit
Dim iCnt As Long
Sub FilePrint() 'Catches File/Print command
Dim iUndo As Long
On Error GoTo Oops
If MsgBox("Do you wish to delete all pictures in headers", _
vbQuestion + vbYesNo, "Delete Pictures") = vbYes Then
RemoveHeaderImages
Dialogs(wdDialogFilePrint).Show
For iUndo = 1 To iCnt
ActiveDocument.Undo
Next
Else
Dialogs(wdDialogFilePrint).Show
End If
Oops:
End Sub
Sub FilePrintDefault() 'Catches Toolbar Print button
Dim iUndo As Long
On Error GoTo Oops
If MsgBox("Do you wish to delete all pictures in headers", _
vbQuestion + vbYesNo, "Delete Pictures") = vbYes Then
RemoveHeaderImages
For iUndo = 1 To iCnt
ActiveDocument.Undo
Next
ActiveDocument.PrintOut
Else
ActiveDocument.PrintOut
End If
Oops:
End Sub
Function RemoveHeaderImages()
Dim ThisImage As InlineShape
Dim mySection As Section
Dim myHF As HeaderFooter

On Error Resume Next
iCnt = 0
For Each mySection In ActiveDocument.Sections()
For Each myHF In mySection.Headers
If myHF.Range.InlineShapes.Count > 0 Then
' just in case there is more than one image
For Each ThisImage In myHF.Range.InlineShapes
ThisImage.Delete
iCnt = iCnt + 1
Next
End If
If myHF.Range.ShapeRange.Count > 0 Then
myHF.Range.ShapeRange.Delete
iCnt = iCnt + 1
End If
Next
Next
End Function

Have Funn! :thumb

cmorphew
04-11-2005, 12:26 PM
:doh:Yeah, I told you I was new at this...

MOS MASTER
04-11-2005, 12:28 PM
:doh:Yeah, I told you I was new at this...Hi, :D
No problems over here ... but is the new code helping you yet? :rofl:

And don't forget keep on trying everything...and one off these day's you'll be the one giving the advice!...

Have Fun!

cmorphew
04-11-2005, 12:35 PM
M.O.S. Master you Rock! :bow: That did it. Thank you! Thanks to fumei as well.
:beerchug:

Cabe

MOS MASTER
04-11-2005, 12:38 PM
Hi Cabe, :D

:beerchug: You're very welcome!...

Till we meet again.....
(Ps..please don't forget to mark you're thread solved?)

fumei
04-13-2005, 09:17 AM
This is a good example of:

1. determination of needs

2. clear expression of those needs.

The post/code in the original link CLEARLY states the code works only with the specified template.

The original post here did NOT state that there was to be a user interface, something that requests a choice by the user. Otherwise I would have put one in.

In reference to #1 above, there is no NEED for all this code. if the need requirement is for:

1. a interface to return a user choice of what to remove from the header;
2. a print spool of the document WITHOUT those elements
3. no change to the original document.

THEN

1. build the interface - three choices, Remove Text, Remove Graphics, Remove Both. This is easy.

2. COPY the entire document into a new document. This leaves the original intact - thereby releasing the need to put any header elements BACK into the document.

3. run the appropriate removal. My tight stuff to remove graphics (adjusted for Shapes by a simple test - If range (of header/footer) shape count > 0 then.....); or removal of text (no problem).

3. print the thing - thereby releasing any need to recode the FilePrint command.

4. close the file without saving.

Done.

MOS MASTER
04-13-2005, 10:20 AM
Hi Fumei,

A good question in general would only need one answer! :rofl:

But that?s usually not the case. Most off the times people see the code in action and think: ?what about adding this and that..wouldn?t that be great..and so on!?

Yes if al they information was there in the beginning than perhaps I would have taken another approach.

My primary goal is to make it work. Fine tuning always happens after you worked with it for a while and you receive feedback from users?

Still love the way you can explain things?

fumei
04-13-2005, 11:12 AM
Quite agree. Fine tuning does, in fact, come after. This is why is it so important to get a solid determination of what is actually needed at the start.

You fine tune to make that, and only that, work properly. THEN, you go on to make improvements - which are, of course, always there to be done.

I was not complaining, just pointing out that this is where the vast majority of development does not make the cut. Most developers (even those of us who are really hacking around with Word macros) fail to do the up-front work. They want to code.

However, in my experience, and that goes back more than 30 years, development of well designed and stable code can be cut in half if the needs analysis and design part is fully done.

It rarely happens. I have had major development teams openly state they do "not have time for design", they are paid to code. True, except that 80% of your time coding is fixing badly designed code.

Software that requires 18 MAJOR patches in one year, is bad software.

I know we are, here, discussing Word VBA. This is not for huge corporation applications. However, the principle is the same. Actually working out what you need, step by step, and how you have to do that, step by step, makes a big difference.

There, I am done with my rant.

Zack Barresse
04-13-2005, 11:21 AM
Nice post Gerry! :)

Which reminds me (as it often happens at the time of somebody else's train of thought), I want to thank you for all your efforts! This board is so much better from the expert advice of people like you and Joost. Thank you both!!! Your invaluable help is soooo very much appreciated!! :yes


.. There, I am done with my rant.

:rotlaugh: I hear ya there!

MOS MASTER
04-13-2005, 11:26 AM
Hi Gerry, :D

With this story I totaly agree!

I also think the 80/20 rule should be implemented a lot more. Just like the Deming Circel (Plan Do Check Act) is a good handle on making you're project more efficient..

I haven't been arrount as long as you have in this stuff..(Actually you're experience is my exact age!) :rofl:

Personally I love to work with checklists up front. You can catogorize Word in to different areas and for al off those areas I have checklist that I can discuss with the client. (And they do help with don't forgetting stuff)

So that's the rough part..after that you build a lite sollution and have them test that...(and yes the questions and changes are rolling in right now...but you expect this)

After this fase you put on paper what the client exactly want's and double confirm on his agreements. (And agree on the fact that al afterward changes to the design will be coded in a totaly different project will start on after this one is accepted)

Now you're ready to normalize you're sollution and start coding..(The path to follow allready there in the code)

Then comes the testing, implementing, doing the helpdesk part..documentation...

And making shure the customer is happy with the sollution provided.

Propably forgetting a lot that is going on but that isn't the issue..

But yeah it is word thinking about stuff in front..that saves the most headaces and Time related issues whit the project..

Hope on seeing you arround! :thumb