PDA

View Full Version : [SOLVED:] Incrementing a ref number within a docvariable



AnnieM
09-22-2016, 11:12 AM
Hi,

I have a Word template that contains several docvariables, including a reference number (varRef). As it is right now, the user manually inputs the unique reference number into a userform but it would work better if this could be automated, with a specific format i.e. YY/0000 (eg 16/0001). I need the new document's reference number to increment by 1, but ideally recycle the number if the new document is abandoned.

I've tried a few solutions that I've seen elsewhere but nothing has worked so far and I would really appreciate any advice.

Many thanks,

Anne

gmaxey
09-23-2016, 07:24 AM
Something like this:


Option Explicit
Dim m_lngSeqNum As Long
Sub AutoNew()
On Error Resume Next
m_lngSeqNum = ThisDocument.Variables("SeqNum").Value
If Err.Number <> 0 Then
m_lngSeqNum = 1
ThisDocument.Variables("SeqNum").Value = 1
ActiveDocument.Fields.Update
End If
On Error GoTo 0
lbl_Exit:
Exit Sub
End Sub
Sub AutoClose()
If Len(ActiveDocument.Path) > 0 Then
ThisDocument.Variables("SeqNum").Value = m_lngSeqNum + 1
ThisDocument.Save
End If
lbl_Exit:
Exit Sub
End Sub

AnnieM
09-23-2016, 10:33 AM
Thank you so much Greg, that works perfectly.

Much appreciated!

Anne