Link to home
Create AccountLog in
Avatar of Or_A
Or_AFlag for Israel

asked on

changing text inside footer/header by vb code

hi,

is there a way to say in vb under word doc:
dim n as integer
n=0
for each word in this document's headers and footers (lets say there's only one duplicated header and footer for every page), n=n+1? (i know it will also count commas, spaces etc.. i dont care)

thanks!
Avatar of Robin Uijt
Robin Uijt
Flag of Netherlands image

In Word, in VBA, you could count the words as in the following code example.
If that's what you want.
'Header
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Debug.Print Selection.Words.Count
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
 
'Footer
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Debug.Print Selection.Words.Count
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Open in new window

Avatar of Or_A

ASKER

thanks,
but my real intension is not counting the words, but to replace words that begins with FLD with lets say the word Try, so i planned to put this code lines:

dim str as string
str="Try"
(for each word in the headers and footers)
for n=1 to (footer/header.words.count - not sure how to write this)
  if left$(trim$(words(n).text,3)="FLD" then (here too, im not sure if "words(n)" is correct to write)
  words(n).text=str
next n

can you please arrange together what you wrote and what i wrote to one complete code?
thanks!
ASKER CERTIFIED SOLUTION
Avatar of Robin Uijt
Robin Uijt
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer