Save Macro

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dear All,

I have a document which i wish to automate via a macro. What i would like
the macro to do is increment the number within a particular field, i.e.
FormID, and then save the document as the FormID and the name of the person
to which the form belongs, (name kept in Name field).

I would also like the Macro to automatically execute on close.

Is this possible?

Thanks in advance
 
Hi Sukh,

Do the following:

Record any macro as you like, and save it in the document.
Choose Tools | Macro > Macros...
Select your macro and click Edit. The VBA editor opens.
Replace the Sub <name>()... End Sub in which the cursor currently stands
with the following:
-
Sub AutoClose()
Dim lngFormID As Long

On Error GoTo ErrorTrap_AutoClose

' determine sequence number
lngFormID = ActiveDocument.Variables("FormID").Value
lngFormID = lngFormID + 1
ActiveDocument.Variables("FormID").Value = lngFormID
ActiveDocument.Saved = True

' save
With Application.Dialogs(wdDialogFileSaveAs)
.Name = LTrim(Str(lngFormID)) & "_" & Application.UserName & ".doc"
.Show
End With
Exit Sub

ErrorTrap_AutoClose:
Select Case Err.Number
Case 5825
ActiveDocument.Variables.Add Name:="FormID", Value:=0
Resume
Case Else
Error Err.Number
End Select
End Sub
-

This should do the trick. I am not entirely sure what you mean by FormID
field; the macro stores the sequence number in a document variable. Works
just as fine.

Success,
Cooz
 
Back
Top