Consulting

Results 1 to 3 of 3

Thread: Incrementing a ref number within a docvariable

  1. #1
    VBAX Regular
    Joined
    Sep 2016
    Posts
    17
    Location

    Incrementing a ref number within a docvariable

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Sep 2016
    Posts
    17
    Location
    Thank you so much Greg, that works perfectly.

    Much appreciated!

    Anne

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •