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 / July 2005

Tip: Looking for answers? Try searching our database.

Auto Update Headers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nic - 06 Jul 2005 23:38 GMT
Am self taught in VBA so please be patient with me!!

I have a code that prompts the user to insert fields into a new word doc
created from a template.  The code I used is:
_________________________________________
Sub AutoNew()

Dim DocNum As String
Dim Title As String
Dim Author As String
Dim RevNo As String
Dim oStory As Range

Title = InputBox("Enter the Document Title:", "Title", "New Document")
Author = InputBox("Enter the Document Author:", "Author", "Originator's Name")
DocNum = InputBox("Enter Doc number", "Doc Number", "XXX-XXXX")
RevNo = InputBox("Enter Rev Number", "Rev Number", "Rev Number")
ActiveDocument.Variables("DocNum").Value = DocNum
ActiveDocument.Variables("RevNo").Value = RevNo
ActiveDocument.BuiltInDocumentProperties("Title").Value = Title
ActiveDocument.BuiltInDocumentProperties("Author").Value = Author

For Each oStory In ActiveDocument.StoryRanges
   oStory.Fields.Update
   If oStory.StoryType < wdMainTextStory Then
     While Not (oStory.NextStoryRange Is Nothing)
       Set oStory = oStory.NextStoryRange
       oStory.Fields.Update
     Wend
   End If
Next oStory
Set oStory = Nothing
End Sub

Sub AutoOpen()

Dim Update As VbMsgBoxResult
Dim DocNum As String
Dim RevNo As String
Dim Title As String
Dim Author As String
Dim oStory As Range

On Error GoTo ExitSub
Update = MsgBox("Update Document Information?", vbYesNo)
If Update = vbYes Then
   Title = InputBox("Enter the Document Title:")
   Author = InputBox("Enter the Document Originator:")
   DocNum = InputBox("Enter Doc number", "Doc Number")
   RevNo = InputBox("Enter Rev Number", "Rev Number")
   ActiveDocument.Variables("DocNum").Value = DocNum
   ActiveDocument.Variables("RevNo").Value = RevNo
   ActiveDocument.BuiltInDocumentProperties("Title").Value = Title
   ActiveDocument.BuiltInDocumentProperties("Author").Value = Author

   For Each oStory In ActiveDocument.StoryRanges
   oStory.Fields.Update
   If oStory.StoryType < wdMainTextStory Then
     While Not (oStory.NextStoryRange Is Nothing)
       Set oStory = oStory.NextStoryRange
       oStory.Fields.Update
     Wend
   End If
Next oStory
Set oStory = Nothing
End If
ExitSub:
End Sub

_________________________________

My problem is it is not updating the document (in particular the header) after
section breaks.  I can manually go into the header and right click on the
field and select update field but would like this to run automatically.  Any
assistance on what I should add to the above code to make this happen is much
appreciated.

Nic
Anne Troy - 07 Jul 2005 00:48 GMT
Nic, you might just want to add some code at the end of your document that
updates all the fields for you:

Sub
     UpdateAllDocFields()

     Dimsry

     ForEach sry
     In ActiveDocument.StoryRanges

         sry.Fields.Update

     NextStr
     End
Sub
*******************
~Anne Troy

www.OfficeArticles.com
www.MyExpertsOnline.com

> Am self taught in VBA so please be patient with me!!
>
[quoted text clipped - 74 lines]
>
> Nic
Nic - 07 Jul 2005 01:15 GMT
Anne,

Apologies for my not understanding.  I only have very basic knowledge of
programming.  Your assistance is much appreciated.

I created a new module in the template and pasted your text in from below.  
The following error message was displayed:
Compile Error: Expected: line number or label or statement or end of
statement.

What am I doing wrong??

Thanks again for your assistance.
Nic

> Nic, you might just want to add some code at the end of your document that
> updates all the fields for you:
[quoted text clipped - 100 lines]
> >
> > Nic
Anne Troy - 07 Jul 2005 01:30 GMT
Hm. I copied it from MSDN, and never even looked. It looks to me like
NextStr should be NextSry
*******************
~Anne Troy

www.OfficeArticles.com
www.MyExpertsOnline.com

> Anne,
>
[quoted text clipped - 115 lines]
> > >
> > > Nic
Jean-Guy Marcil - 07 Jul 2005 02:08 GMT
Anne Troy was telling us:
Anne Troy nous racontait que :

> Nic, you might just want to add some code at the end of your document
> that updates all the fields for you:
[quoted text clipped - 12 lines]
>      End
> Sub

It should be:

'_______________________________________
Sub UpdateAllDocFields()

Dim sry As StoryRange

For Each sry In ActiveDocument.StoryRanges
   sry.Fields.Update
Next

End Sub
'_______________________________________

But even that will not work all the time. It will not work if you have
fields in more than one textbox, or if you have unlinked odd/even
headers/footers.

For a complete cure to those problems, see:

   http://www.word.mvps.org/faqs/customization/ReplaceAnywhere.htm

and replace the code about replacing by the code for updating (from above).
(i.e. replace
   SearchAndReplaceInStory rngStory, "cat", "dog"
by
   rngStory.Fields.Update
and don't use the SearchAndReplaceInStory  Sub.)

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

 
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.