PDA

View Full Version : Run code when entering a header/footer



u_dakka
10-29-2010, 03:47 PM
As the title suggests I am clues as to how I can get my VBA code to run whenever someone enters a header or a footer in a word document.

Is there some background function that's always running that I can add an if statement to see if it's in the header/footer?

the version I will be deploying this on is 2003 (although I am personally coding in 2010).

Thanks in advance
Andy

fumei
10-29-2010, 04:22 PM
"Is there some background function that's always running "

Nope.

Why do you want to know this?

u_dakka
10-30-2010, 01:50 AM
The background function was just an idea of a way to solve it

What I want is to know when someone has entered the header/footer

fumei
11-01-2010, 09:24 AM
So I will ask again. Why?

u_dakka
11-01-2010, 09:50 AM
Where I work we have a specific header/footer that we have to use.

Not all the users are comfortable with using a button or keyboard shortcut. I'm not allowed to tie it into other functions like save or open, but I want users to check their header/footer and only use predefined responses to certain questions (which end up in the header/footer).

Therefore the easiest way that I could see this code running that would be the least obtrusive is if I used some code that was executed when the user entered the header/footer.

Does such an entry point exist?

Andy

fumei
11-01-2010, 11:36 AM
As answered, no.

Frankly, I still do not quite follow what the issue is.

If you have a specific header/footer, why not put that specific header/footer IN the header/footer?

If there - as it seems - DIFFERENT header/footer (since there appears to be "certain questions" that the user answers to determine predefined responses going into the header/footer), then how are those given to the user?

A userform? How are these questions asked of the user?

What I am getting at is a question as to WHY the user is even going into the header/footer. As it stands I see no reason why they need to go into the header/footer at all, ever.

WHY do they need to "check" their header/footer?

macropod
11-01-2010, 03:23 PM
I want users to check their header/footer and only use predefined responses to certain questions (which end up in the header/footer).If your using a Word form for the data capture, you can use field coding and/or a macro to automaically insert the correct information into the header/footer. Alternatively, if your document is suitably structured, you might be able to use a STYLEREF field.

You need to provide more information on what sort of document you're using and what you're trying to achieve.

rruckus
11-02-2010, 12:00 PM
You can certainly run your code when someone enters a header, footer and most other parts of the document actually. Create a new class module to register your events. At the top of the module put:


Private WithEvents m_oWordApp As Word.Application
Private WithEvents m_oWordDoc As Word.Document



This will allow you to get events from your word document. Use the selection change event and test if the selection is in a header or footer:


Private Sub m_oWordApp_WindowSelectionChange(ByVal Sel As Selection)
If Selection.Information(WdInformation.wdInHeaderFooter) = True Then
MsgBox "I'm in a header or footer!"
Else
MsgBox "I'm NOT in a header or footer!"
End If
End Sub



This should work in all versions since 2003 I believe, perhaps even earlier.