View Single Post
 
Old 03-27-2015, 05:23 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Insert the following DOCVARIABLE field in the document where you want the number to appear: { DOCVARIABLE VarNum \# "'BIR-'000000" }
Add the following macro to the document and save as a macro enabled document:

Code:
Sub AutoOpen()
Dim oVars As Variables
Dim bVar As Boolean
Dim lngCount As Long
    Set oVars = ActiveDocument.Variables
    For Each oVar In ActiveDocument.Variables
        If oVar.Name = "varNum" Then
            bVar = True
            lngCount = oVar.Value + 1
            Exit For
        End If
    Next oVar
    If Not bVar Then lngCount = 1
    oVars("varNum").Value = lngCount
    UpdateAllFields
    ActiveDocument.Save
lbl_Exit:
    Exit Sub
End Sub

Private Sub UpdateAllFields()
Dim oStory As Range
    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
lbl_Exit:
    Exit Sub
End Sub
If you need to change the number set the value of the document variable 'varNum' as required.

A simple way to do this is to use http://www.gmayor.com/BookmarkandVariableEditor.htm

See also http://www.gmayor.com/automatic_numbering_documents.htm for two other methods that may be adapted to your requirements.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote