Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Wherein my mistake?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
avkokin - 27 May 2008 20:09 GMT
Hello.
I need to insert field into footer. Field of number pages in active
document and minus 1. That is field: {={numpages}-1}
I try it insert with next code:
sub insPN ()
Dim myRange As Range
Set myRange =
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
Selection.Fields.Add Range:=myRange, Type:=wdFieldEmpty, Text:="=",
PreserveFormatting:=False
Selection.Fields.Add Range:=myRange, Type:=wdFieldEmpty,
Text:="numpages", PreserveFormatting:=False
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:="-1"
Selection.Fields.Update
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
end sub
I get the mistake.
Wherein my mistake?
Thank you very much for help.
Jean-Guy Marcil - 27 May 2008 20:59 GMT
> Hello.
> I need to insert field into footer. Field of number pages in active
[quoted text clipped - 15 lines]
> I get the mistake.
> Wherein my mistake?

Quite a few...
For example, you set myRange, then you add a field using the myRange object,
and then, immediatley after that, you add another field, using the same range
object, thereby overwriting the first field..
Then, you use a Selection objet, assuming it is connected to your Range
object (because of "Selection.Fields.Add Range:=myRange..."), but is in fact
related to whatever happens to be selected in the document when the code runs.

Nested fields with VBA can be quite tricky... for a complete exlpanation and
different solutions, see
  http://word.mvps.org/faqs/macrosvba/NestedFieldsWithVBA.htm
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.