IF-THEN statement comparing "current page" variable to "last page" variable

  • Thread starter todd m via OfficeKB.com
  • Start date
T

todd m via OfficeKB.com

I know this sounds simple, but I just can't figure this out. I wish to
insert a first logo on the first page of my Word document (which I can do by
assigning my graphic as autotext and then inserting from the Insert>Autotext
toolbar), and then cycle through pages 2 to "end" to insert a second logo. I
can easily cycle through manually to insert the graphics, but I wish to
automate. Please review my comments to see where I wish to place the IF-THEN
statement:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 7/11/2005 by Todd E. Marlette
'
' GOTO TOP OF DOCUMENT -- THIS SECTION IS O.K.
Selection.HomeKey Unit:=wdStory
Application.DisplayAutoCompleteTips = True

' INSERT AUTOTEXT GRAPHIC CALLED LOGO -- THIS SECTION IS O.K.
NormalTemplate.AutoTextEntries("logo").Insert Where:=Selection.Range, _
RichText:=True

' IF "CURRENT PAGE" IS NOT THE "LAST PAGE" THEN GOTO TO NEXT PAGE AND
INSERT LOGO2
' HERE IS MY PROBLEM. HOW DO I WRITE THIS IF-THEN STATEMENT IN A WORD
MACRO?

' GOTO TOP OF NEXT PAGE -- THIS SECTION IS O.K.
Application.Browser.Next
Application.DisplayAutoCompleteTips = True

' INSERT AUTOTEXT GRAPHIC CALLED LOGO2 -- THIS SECTION IS O.K.
NormalTemplate.AutoTextEntries("logo2").Insert Where:=Selection.Range,
_
RichText:=True

End Sub

' THANK YOU !!!!!
 
J

Jean-Guy Marcil

todd m via OfficeKB.com was telling us:
todd m via OfficeKB.com nous racontait que :
I know this sounds simple, but I just can't figure this out. I wish
to insert a first logo on the first page of my Word document (which I
can do by assigning my graphic as autotext and then inserting from
the Insert>Autotext toolbar), and then cycle through pages 2 to "end"
to insert a second logo. I can easily cycle through manually to
insert the graphics, but I wish to automate. Please review my
comments to see where I wish to place the IF-THEN statement:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 7/11/2005 by Todd E. Marlette
'
' GOTO TOP OF DOCUMENT -- THIS SECTION IS O.K.
Selection.HomeKey Unit:=wdStory
Application.DisplayAutoCompleteTips = True

' INSERT AUTOTEXT GRAPHIC CALLED LOGO -- THIS SECTION IS O.K.
NormalTemplate.AutoTextEntries("logo").Insert
Where:=Selection.Range, _ RichText:=True

' IF "CURRENT PAGE" IS NOT THE "LAST PAGE" THEN GOTO TO NEXT PAGE
AND INSERT LOGO2
' HERE IS MY PROBLEM. HOW DO I WRITE THIS IF-THEN STATEMENT IN A
WORD MACRO?

'_______________________________________
If Selection.Information(wdNumberOfPagesInDocument) > 1 Then
'Insert logo
End If
'_______________________________________

By the way, you can remove
Application.DisplayAutoCompleteTips = True
from the macro.

The macro recorder records a lot of unnecessary stuff. You can always trim a
macro after having recorded it.

See
http://word.mvps.org/faqs/macrosvba/UsingRecorder.htm
for basic info, then see
http://word.mvps.org/faqs/macrosvba/ModifyRecordedMacro.htm
for further information on the topic.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
T

todd m via OfficeKB.com

THANK YOU !!! I got something that now works. FYI, this is what I came up
with.

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 7/11/2005 by Todd E. Marlette
'
' GOTO END OF DOCUMENT
Selection.EndKey Unit:=wdStory

' ASSIGN CURRENT PAGE NUMBER TO VARIABLE LASTPAGENO
LastPageNo = Selection.Information(wdActiveEndPageNumber)

' GOTO TOP OF DOCUMENT
Selection.HomeKey Unit:=wdStory

' INSERT AUTOTEXT GRAPHIC CALLED LOGO
NormalTemplate.AutoTextEntries("logo").Insert Where:=Selection.Range, _
RichText:=True

INNERLOOP:

' ASSIGN CURRENT PAGE NUMBER TO VARIABLE CURRENTPAGE
CurrentPageNo = Selection.Information(wdActiveEndPageNumber)

If CurrentPageNo <> LastPageNo Then

' GOTO TOP OF NEXT PAGE
Application.Browser.Next

' INSERT AUTOTEXT GRAPHIC CALLED LOGO2
NormalTemplate.AutoTextEntries("logo2").Insert Where:=Selection.Range, _
RichText:=True

GoTo INNERLOOP

End If


End Sub
 
T

todd m via OfficeKB.com

FYI: THIS ONE WORKS MUCH BETTER

Sub Macro3()
'
' Macro3 Macro
' Macro recorded 7/21/2005 by Todd E. Marlette
'
' GOTO TOP OF DOCUMENT
Selection.HomeKey Unit:=wdStory

' INSERT AUTOTEXT GRAPHIC CALLED logo
NormalTemplate.AutoTextEntries("logo").Insert Where:=Selection.Range, _
RichText:=True

' DETERMINE NUMBER OF PAGES IN DOCUMENT
MaxPages = Selection.Information(wdNumberOfPagesInDocument)

' LOOP THROUGH PAGES AFTER PAGE 1 TO INSERT SECOND LOGO
For i = 2 To MaxPages
Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=i, Name:=""

' INSERT AUTOTEXT GRAPHIC CALLED logo2
NormalTemplate.AutoTextEntries("logo2").Insert Where:=Selection.Range, _
RichText:=True

Next

' GOTO TOP OF DOCUMENT
Selection.HomeKey Unit:=wdStory

End Sub
 

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