Solved

changing text inside footer/header by vb code

Posted on 2008-06-10
3
230 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
  • 2
3 Comments
 
LVL 16

Expert Comment

by:robinu
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:
robinu earned 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

773 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