N
Nic
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
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