Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

changing text inside footer/header by vb code

Posted on 2008-06-10
3
Medium Priority
?
239 Views
Last Modified: 2008-06-24
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!
0
Comment
Question by:Or_A
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Expert Comment

by:Robin
ID: 21750382
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

0
 

Author Comment

by:Or_A
ID: 21750620
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!
0
 
LVL 16

Accepted Solution

by:
Robin earned 2000 total points
ID: 21750676
Yes, sure. Something like this:

    Dim lIdx As Long
    Dim sReplace As String
    
    sReplace = "Try"
    
    'header
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    For lIdx = 1 To Selection.Words.Count
        If Left$(Selection.Words(lIdx).Text, 3) = "FLD" Then
            Selection.Words(lIdx).Text = sReplace & " "
        End If
    Next lIdx
 
    'footer
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    For lIdx = 1 To Selection.Words.Count
        If Left$(Selection.Words(lIdx).Text, 3) = "FLD" Then
            Selection.Words(lIdx).Text = sReplace & " "
        End If
    Next lIdx
 
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Open in new window

0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question