How to execute macro for First Page ONLY

E

ES

I have a macro that I want to execute for the first page only. How would I
wrap code around something that will only execute on PageNumber = 1?

Thanks,
Enoch
 
J

Jay Freedman

Hi, Enoch,

That depends very much on what the macro is doing and how it does it.

For example, if it's doing Find/Replace, you would assign a Range object to
the first page and use that object's .Find method, with the .Wrap parameter
set to wdFindStop so it doesn't continue past the end of the range. (Note
that this is a change in the basic macro and probably can't be done by
"wrapping" an existing macro.)

In some other cases you might want to have the wrapping macro check that the
Selection (the insertion point or selected text) is on page 1, and
immediately exit if it isn't. You can do that check by looking at the value
of Selection.Information(wdActiveEndPageNumber) or
Selection.Information(wdActiveEndAdjustedPageNumber).

There may be other methods, depending on what you're doing.
 
E

ES

I have some code working perfectly that loops through each "Range in
ActiveDocument.StoryRanges", each "Shape in ActiveDocuments.Shapes", and
each "HeaderFooter in Section.Header" to find the count of MailMergeFields
in a document. The problem is, I now want to limit this search to find
fields on the first page only and not the entire docuement (if multiple
pages). Therefore, I simply need to wrap this code around code that only
searches if the active page = first page. I'm not sure how to determine the
first page and only search for the fields on the first page.

Thanks,
Enoch
 
E

ES

Thanks for helping. Below is the code that I'm using that works very
well. The problem is that I need to only execute the AddMergeFieldToArray()
method if the field appears on the first page. I tried using some
Selection.Information(wdActiveEndPageNumber) code, but that assumes that the
mouse pointer is located on the first page (not good). Basically all I want
is to wrap code around the commented section below, such as "If
ActiveDocument.PageNumber = 1 Then" (see comment section below). Either
this is extremely easy or not
obvious. Please help!!

Thanks,
ES

'Finds all MailMergeFields except Textbox
m_strErrMsg = " Finding all MailMergeFields in Main Story, except Textboxes
"
For Each rRange In wrdDocument.StoryRanges
If rRange.StoryType <> 5 Then 'wdTextFrameStory Then
For Each fField In rRange.Fields
If fField.Type = 59 Then 'wdFieldMergeField Then
lngTotalMergeFieldCount = lngTotalMergeFieldCount + 1

strMergeField = CStr(fField.code)

'Cleanup MERGEFIELD Code
strMergeField = CleanupMergeFields(strMergeField)

'Check to see if field is in database
'other code here

'***I only want to add this to the array if the field is located on the
first page
'Add MergeField to the array
AddMergeFieldToArray (strMergeField)
'*****************************************************
End If
Next
End If
Next

'Finds all MergeFields in Textboxes of Active Document
' similar code as above

'Finds all MergeFields in Textboxes of HeadersFooters Section
'similar code as above
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top