We help IT Professionals succeed at work.
Get Started

Microsoft Word Macro Automation

johns_ar
johns_ar asked
on
1,154 Views
Last Modified: 2010-05-18
Hi,

Ive created a word template which has various autotext entries in it.  Nothing fancy just date, username, filename etc.  My main problem is that by default Word will not automatically update these when the document is saved like it does when you go to print.  I want to automate this process so that when a user saves the file the fields are populated correctly.  I have almost sorted it using a macro and a few word commands but ive fallen at the final hurdle.

Ive got an updateall macro that updates the fields and filesave, filesaveas, filesaveall and autoopen macro/word commands to finish off the job.  The macro/word commands run perfectly and update the fields as required but theres one strange problem thats annoying me.  If a document has been saved as "filename x" and then the user continues to work and makes changes then presses save again it doesnt just save over the top of the document, the save box comes up and they have to choose where to save the file, they can just replace the current file but this is not what I want.

Can anyone point me in the right direction here? My macros/word commands are below.  Hopefully theres a schoolboy error in there that someone can pick out straight away! Thanks in advance.

Attribute VB_Name = "AutoOpen"
    Sub AutoOpen()
     Dim aStory As Range
     Dim aField As Field
     Dim aSection As Section
     Dim aHeaderFooter As HeaderFooter

     For Each aSection In ActiveDocument.Sections
         For Each aHeaderFooter In aSection.Headers
             Set aStory = aHeaderFooter.Range
       
             For Each aField In aStory.Fields
                 aField.Update
             Next aField
         Next aHeaderFooter
       
         For Each aHeaderFooter In aSection.Footers
             Set aStory = aHeaderFooter.Range
           
             For Each aField In aStory.Fields
                 aField.Update
             Next aField
         Next aHeaderFooter
     Next aSection
     
    End Sub
-----------------------------------------------------------
Attribute VB_Name = "FileSave"
Sub FileSave()
Dim pRange As Word.Range
Dim oFld As Field
On Error GoTo Handler
Retry:
With Dialogs(wdDialogFileSaveAs)
    If .Show = 0 Then Exit Sub
End With
System.Cursor = wdCursorNormal
ActiveWindow.Caption = ActiveDocument.FullName
For Each pRange In ActiveDocument.StoryRanges
    Do
        For Each oFld In pRange.Fields
            If oFld.Type = wdFieldFileName Then
                oFld.Update
            End If
        Next oFld
        Set pRange = pRange.NextStoryRange
    Loop Until pRange Is Nothing
Next
ActiveDocument.Save
Exit Sub
Handler:
If Err.Number = 5155 Or Err.Number = 5153 Then
MsgBox Err.Decription, vbOKOnly
Resume Retry
End If
End Sub
--------------------------------------------------
Attribute VB_Name = "FileSaveAll"
Sub FileSaveAll()
Dim pRange As Word.Range
Dim oFld As Field
On Error GoTo Handler
Retry:
With Dialogs(wdDialogFileSaveAs)
    If .Show = 0 Then Exit Sub
End With
System.Cursor = wdCursorNormal
ActiveWindow.Caption = ActiveDocument.FullName
For Each pRange In ActiveDocument.StoryRanges
    Do
        For Each oFld In pRange.Fields
            If oFld.Type = wdFieldFileName Then
                oFld.Update
            End If
        Next oFld
        Set pRange = pRange.NextStoryRange
    Loop Until pRange Is Nothing
Next
ActiveDocument.Save
Exit Sub
Handler:
If Err.Number = 5155 Or Err.Number = 5153 Then
MsgBox Err.Decription, vbOKOnly
Resume Retry
End If
End Sub
---------------------------------------------------
Attribute VB_Name = "FileSaveAs"
Sub FileSaveAs()
Dim pRange As Word.Range
Dim oFld As Field
On Error GoTo Handler
Retry:
With Dialogs(wdDialogFileSaveAs)
    If .Show = 0 Then Exit Sub
End With
System.Cursor = wdCursorNormal
ActiveWindow.Caption = ActiveDocument.FullName
For Each pRange In ActiveDocument.StoryRanges
    Do
        For Each oFld In pRange.Fields
            If oFld.Type = wdFieldFileName Then
                oFld.Update
            End If
        Next oFld
        Set pRange = pRange.NextStoryRange
    Loop Until pRange Is Nothing
Next
ActiveDocument.Save
Exit Sub
Handler:
If Err.Number = 5155 Or Err.Number = 5153 Then
MsgBox Err.Decription, vbOKOnly
Resume Retry
End If
End Sub
---------------------------------------------------------

Sorry its a big long winded!
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE