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 / December 2007

Tip: Looking for answers? Try searching our database.

Add Method for the Fields Collection Issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kdh - 21 Dec 2007 00:05 GMT
In an attempt to automate the conversion of document headers, I have been
stumped by the Add method of the Fields collection continually overwriting
the first field rather than adding additional fields.

ActiveDocument.Fields.Add _
Range:=ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range, _
Type:=wdFieldDocProperty

Trying to add multiple fields just continually modifies Item 1 inside the
Fields collection. I can manipulate the one field it does create as desired
and I can manipulate multiple fields if they are first created manually in
Word first. I just can't seem to get more than one field created at any given
time. Any suggestions would be appreciated.
Jay Freedman - 21 Dec 2007 01:23 GMT
Without seeing your whole macro, I'll guess that the problem is the
Range parameter you're passing to the Add method. If you call the same
statement repeatedly, the Range for the second call is the whole
header, which includes the field inserted by the first call, and so
on.

Instead, declare a Range object and "move" it along the header,
collapsing it at the end each time before adding the next text or
field:

Sub demo()
   Dim oRg As Range
   Dim oFld As Field
   
   ' first field
   Set oRg = ActiveDocument.Sections(1).Headers( _
       wdHeaderFooterPrimary).Range
   oRg.Collapse wdCollapseEnd
   
   Set oFld = ActiveDocument.Fields.Add( _
       Range:=oRg, Type:=wdFieldEmpty)
   oFld.Code.Text = "DocProperty Author"
   oFld.Update
   
   ' second field
   Set oRg = ActiveDocument.Sections(1).Headers( _
       wdHeaderFooterPrimary).Range
   With oRg
       .Collapse wdCollapseEnd
       .Text = vbTab
       .Collapse wdCollapseEnd
   End With
   
   Set oFld = ActiveDocument.Fields.Add( _
       Range:=oRg, Type:=wdFieldEmpty)
   oFld.Code.Text = "Date \@yyyy-MM-dd"
   oFld.Update
   
   ' third field
   Set oRg = ActiveDocument.Sections(1).Headers( _
       wdHeaderFooterPrimary).Range
   With oRg
       .Collapse wdCollapseEnd
       .Text = vbTab
       .Collapse wdCollapseEnd
   End With
   
   Set oFld = ActiveDocument.Fields.Add( _
       Range:=oRg, Type:=wdFieldPage)
End Sub

>In an attempt to automate the conversion of document headers, I have been
>stumped by the Add method of the Fields collection continually overwriting
[quoted text clipped - 9 lines]
>Word first. I just can't seem to get more than one field created at any given
>time. Any suggestions would be appreciated.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
 
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.