PDA

View Full Version : Automation of bookmark in Footer



JenJer
02-07-2011, 11:27 AM
I am using bokmarks in a document to insert specific parts such as date, user etc. I am also using bookmarks to insert our logo at the "header" (actually in the body of the doc) and also an image into the first page footer. The image has our address information for our four offices. I have created a form where the user selects an option button to pick the header and footer images. The code behind the form first lists the options for the header logo, then it puts the focus on the footer and inserts the image, then it puts the focus back on the main document for editing. This code for inserting the footer works in a 97-2003 .dot template, but will not work in 2007 with a .dotm template. It will however work in 2010. I have tried converting the older doc and created a new doc. Either way it will not insert the footer image. We are using 2007 with XP and Win 7, and it won't work on either. Any help would be great. I am a beginner at VB and don't know where to go next.

Here is the code:

Private Sub CmdOK_Click()
If OptionButton1 = True Then _
ActiveDocument.Bookmarks("Header").Range.InlineShapes.AddPicture FileName:= _
"\\pinkfloyd\Templates\FORMS\Logos\ConferenceENG.png", LinkToFile:=False, _
SaveWithDocument:=True

If OptionButton2 = True Then _
ActiveDocument.Bookmarks("Header").Range.InlineShapes.AddPicture FileName:= _
"\\pinkfloyd\Templates\FORMS\Logos\ConferenceNRG.png", LinkToFile:=False, _
SaveWithDocument:=True

If ActiveDocument.Bookmarks.Exists("Footer") = True Then _
ActiveDocument.Bookmarks("Footer").Select
If OptionButton3 = True Then _
ActiveDocument.Bookmarks("Footer").Range.InlineShapes.AddPicture FileName:= _
"\\pinkfloyd\Templates\FORMS\Logos\FooterLaCrosseEng.png", LinkToFile:=False, _
SaveWithDocument:=True
If OptionButton4 = True _
Then ActiveDocument.Bookmarks("Footer").Range.InlineShapes.AddPicture FileName:= _
"\\pinkfloyd\Templates\FORMS\Logos\FooterLaCrosseNRG.png", LinkToFile:=False, _
SaveWithDocument:=True

If OptionButton5 = True Then _
ActiveDocument.Bookmarks("Footer").Range.InlineShapes.AddPicture FileName:= _
"\\pinkfloyd\Templates\FORMS\Logos\FooterTwinCitiesENG.png", LinkToFile:=False, _
SaveWithDocument:=True

If OptionButton6 = True Then _
ActiveDocument.Bookmarks("Footer").Range.InlineShapes.AddPicture FileName:= _
"\\pinkfloyd\Templates\FORMS\Logos\FooterCedarRapidsNRG.png", LinkToFile:=False, _
SaveWithDocument:=True
If ActiveDocument.Bookmarks.Exists("Header") = True Then _
ActiveDocument.Bookmarks("Header").Select


ActiveWindow.View.Type = 3 'print layout
ActiveWindow.View.Zoom.Percentage = 100

Application.ScreenUpdating = True
Unload Me


End Sub

macropod
02-25-2011, 04:23 AM
Hi JenJer,

The following simplified version of your code seems to work fine in Word 2007:
Private Sub CmdOK_Click()
Application.ScreenUpdating = False
Const strPath As String = "\\pinkfloyd\Templates\FORMS\Logos\ (file://\\pinkfloyd\Templates\FORMS\Logos\)"
Dim strHdImg As String, strFtImg As String
With ActiveDocument
If OptionButton1 = True Then strHdImg = "ConferenceENG.png"
If OptionButton2 = True Then strHdImg = "ConferenceNRG.png"
If OptionButton3 = True Then strFtImg = "FooterLaCrosseEng.png"
If OptionButton4 = True Then strFtImg = "FooterLaCrosseNRG.png"
If OptionButton5 = True Then strFtImg = "FooterTwinCitiesENG.png"
If OptionButton6 = True Then strFtImg = "FooterCedarRapidsNRG.png"
If .Bookmarks.Exists("Header") = True Then _
.Bookmarks("Header").Range.InlineShapes.AddPicture FileName:= _
strPath & strHdImg, LinkToFile:=False, SaveWithDocument:=True
If .Bookmarks.Exists("Footer") = True Then _
.Bookmarks("Footer").Range.InlineShapes.AddPicture FileName:= _
strPath & strFtImg, LinkToFile:=False, SaveWithDocument:=True
End With
With ActiveWindow.View
.Type = 3 'print layout
.Zoom.Percentage = 100
End With
Application.ScreenUpdating = True
Unload Me
End Sub

JenJer
02-25-2011, 12:49 PM
I'm wondering if we have a problem with our Office installations. I could not get your revised code to insert the footer. (Still works in 2010) I tried installing a fresh copy of Office 2007 on a spare machine and it will not work there either. We use a custom .msp file to install Office. Maybe we have something configured in Word that is causing a problem? Will test on an external machine this weekend. In the meantime, are you aware of any settings that could cause a problem with inserting into the footer?

Frosty
02-25-2011, 01:10 PM
There are definitely settings that could cause a problem... most likely compatibility settings. My first guess would be that you have something as simple as First Page Different as your default setting on the Word 2007 machine, but not on the Word 2010 machine.

Are you trying to run the code on a blank new document? A document created in another version? Compatibility mode on in the document?

You've got a number of If Exists statements... maybe the bookmarks don't exist?

Rather than doing re-installs of Word 2007 or assuming it's some kind of installation issue, try finding out which line of code is breaking (do you see a debug window, or can you just not find the graphic at the end of the routine?)

There are a number of possibilities, but with so many unknowns... I'm just throwing stuff against a wall and seeing if it sticks.

Perhaps uploading a generic (no client info) version of the document the code doesn't work on would be a good start? Alternatively, if you have enough understanding to "step through" the code, identify which line isn't doing what you expect.

Highly doubt the above code wouldn't work in Word 2007. More likely it's the document you're trying to insert into.

That said, there are many ways to insert a graphic into the header... so if we can narrow down what's happening, we can suggest some alternative approaches to achieve the same result.

macropod
02-25-2011, 03:35 PM
Hi Jenjer,

I doubt the error has anything to do with Word's configuration (including compatability settings) though, as Frosty says, a 'different first page' document layout might be relevant - but only if the offending bookmark was on an as yet uncreated second page. The same consideration applies if you're using a 'different odd and even' page layout.

Try the following code, to which I've added some error checking and the ability to change the image (if you reload the form).
Private Sub CmdOK_Click()
Application.ScreenUpdating = False
Const strPath As String = "\\pinkfloyd\Templates\FORMS\Logos\ (file://\\pinkfloyd\Templates\FORMS\Logos\)"
Dim strHdImg As String, strFtImg As String
With ActiveDocument
If OptionButton1 = True Then strHdImg = "ConferenceENG.png"
If OptionButton2 = True Then strHdImg = "ConferenceNRG.png"
If OptionButton3 = True Then strFtImg = "FooterLaCrosseEng.png"
If OptionButton4 = True Then strFtImg = "FooterLaCrosseNRG.png"
If OptionButton5 = True Then strFtImg = "FooterTwinCitiesENG.png"
If OptionButton6 = True Then strFtImg = "FooterCedarRapidsNRG.png"
Call UpdateImage("Header", strPath & strHdImg)
Call UpdateImage("Footer", strPath & strFtImg)
End With
With ActiveWindow.View
.Type = 3 'print layout
.Zoom.Percentage = 100
End With
Application.ScreenUpdating = True
Unload Me
End Sub

Sub UpdateImage(strBNm As String, strImg As String)
Dim RngImg As Range
If Dir(strImg) = "" Then
MsgBox "Can't find the image file:" & vbCr & strImg, vbExclamation
Exit Sub
End If
With ActiveDocument
If .Bookmarks.Exists(strBNm) = True Then
Set RngImg = .Bookmarks(strBNm).Range
With RngImg
.Text = vbNullString
.InlineShapes.AddPicture FileName:=strImg, _
LinkToFile:=False, SaveWithDocument:=True
.End = .End + 1
End With
.Bookmarks.Add strBNm, RngImg
Else
MsgBox Chr(34) & strBNm & Chr(34) & " Bookmark not found.", vbExclamation
End If
End With
End Sub

JenJer
03-03-2011, 09:29 AM
I tried the code you posted with the error checking and it runs. It inserts the header image at the header bookmark, but it will not insert an image into the footer bookmark. No errors occur. Nothing happens with the footer.

Both bookmarks are on the first page. I don't have any settings changed in the header and footer i.e. different first page or different Odd & Even pages. I even tried making a blank document with just two bookmarks in it. I am attaching the most recent doc I am working with. I included the images for Option buttons 1 and 3.

macropod
03-03-2011, 03:05 PM
Hi JenJer,

There is definitely something odd about your template and I don't have time to explore that ATM.

For whatever reason, as constructed, the 'footer' bookmark becomes inaccessible via the UI once the macro has run. However, if you reverse order of the two call statements in the code I gave you, so that the footer is updated first, everything seems to work OK.

Also, FWIW, your 'header' bookmark is not in the page header - it's actually in the body of the document.

JenJer
03-03-2011, 03:39 PM
That's what I don't get. I open a blank document. Insert two bookmarks, and save as a .dotm. I have tried the header image in the body and in the header. (The reason it is in the main doc was because we needed to adjust the margins to fit the pic and we couldn't get it to work on just the first page margins. It changed the second page even though we set section breaks.)

If I reverse the order in the statement with the header image in the body, the footer doesn't insert. If the header image is in the header, then the header and footer images both insert at the header bookmark. If I comment out the header code the footer inserts at the header bookmark. So messed up!

I still think there has to be something with our Office install that is causing this. Nothing else makes sense. We even tried using fields to insert the images and that wouldn't work either.

Any suggestions?

Frosty
03-03-2011, 04:39 PM
I downloaded your zip file, but it's not going to work as delivered, because it's trying to reference some kind of Form Automation template as well.

I know you're not familiar with VB, but it's very tough to diagnose without doing all of the work for you. Here are a couple of pointers:

1. Always use Option Explict at the top of your documents-- this is probably the main issue between your versions. It's too long of a discussion to get into the "why" you should do this... but make sure Option Explicit is always at the top of any code module (including userforms).

2. When you name things, don't name them with existing object names, especially when you don't use Option Explicit. Your userform is named UserForm. I would rename it to "frmMyUserForm" or even "myUserForm" when in doubt, even if you don't use a variable naming convention, you need to come up with some kind of prefix just to be able to make readable.

I'm going to play around, change the methodology, and provide you something that works. But it's not going to look like anything you currently have, because there isn't any one thing wrong with your existing template... there are a number of things wrong.

Frosty
03-03-2011, 06:00 PM
Here you go. Unable to test on Windows 7... but this should at least be a workable demo.

Only functionality I changed was not closing the activedocument if you hit cancel.

There are a number of sort of "bad design" concepts I didn't address, because this is just a sample. But the one that makes the troubleshooting really difficult is not using Option explicit, but still using pretty advanced functions like trying to load addins as well as creating objects.

JenJer
03-08-2011, 03:47 PM
I realize there were a lot of issues with my code. It's because I used google to piece it all together. I have never worked with VBA before. Your code that you provided me (Frosty) works fine as long as I don't make any changes to the form or the location of the bookmarks. If I move the header bookmark it deletes it entirely. If I use the code on a multiple page document that needs first page different header and footer it deletes the footer bookmark. If I change the option buttons to reflect the needs of the document it stops inserting the footer. I'm about ready to give up on this and pay someone else to complete it!

Frosty
03-08-2011, 04:09 PM
That may be a good decision (hiring someone). It's impressive that you were able to get as far as you did when having zero experience with VBA. But you may need someone with a bit more experience to be able to translate what you want into something that works in multiple cases.

I mean this very kindly, so please don't take offense, but I think your biggest obstacle right now is not your lack of VBA understanding, but your lack of advanced functionality in Word.

The knowledge gap can exist in vba or in word, but not in both, and I think you've given yourself a pretty insurmountable task. It's a testament to something (your smarts, your refusal to quit, all of the above) that you've gotten as far as you have.

Just a couple of responses to help you along... but I tend to agree that you would benefit from professional (paid) help. Not that I'm soliciting for myself-- I am plenty busy.

1. Look up bookmarks in word help (online as well) so you can get a basic understanding of them. Bookmarks have to have unique names in a document. So if you select the first paragraph of your document, insert a bookmark with the name "bkParagraph"... and then you select a different paragraph and insert a bookmark with the name "bkParagraph"... your "original" bookmark will disappear. But it's not being "deleted"... it's being "redefined."

2. Look up Page Setup/Layout tutorials on the microsoft website. At its simplest form, a single section Word document will still have the following: first page header, first page footer, primary header, primary footer, even page header, even page footer. Those headerfooter objects exist in every single word document. Having "Different First Page" checked on, or "Odd/Even Pages" checked on simple shows those header footer objects... but they are there regardless. If you try to access them in code, they will be there, whether or not you (the user) has decided to show them.

3. Look up "Working with Ranges" in vba help. Bookmarks are simply an easy way for an end-user to define a range. But a programmer doesn't need to do that.. you can simply Dim myRange as Range in your procedure, and then use that throughout (set myRange = ActiveDocument.Sections(1).Header(wdPrimaryHeader).Range)

So I won't suggest you setting up multiple bookmarks to identify something word already identifies for you (First Page header, First Page Footer, Primary Header, Primary Footer), although you could do that.

Good luck.