View Single Post
 
Old 10-19-2015, 08:20 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For this to work in Word, you'd need to store the last-used number in a document property or variable for retrieval. The code to do this might look like:
Code:
Sub InsertNextID()
Dim ID As Long, i As Long, bFnd As Boolean
Const strID As String = "MaxID"
bFnd = False
On Error Resume Next
With ActiveDocument
  For i = 1 To .Variables.Count
    If .Variables(i).Name = strID Then
      bFnd = True
      ID = .Variables(strID).Value + 1
      Variables(strID).Value = ID
      Exit For
    End If
  Next
  If bFnd = False Then
    ID = InputBox(Prompt:="Please input the First ID #", _
      Title:="ID Creation", Default:=1)
    .Variables.Add Name:=strID, Value:=ID
  End If
  Selection.Text = ID
End With
ErrExit:
End Sub
And the code you'll inevitably need to reset that ID might look like:
Code:
Sub IDReset()
Dim ID As Long, i As Long
Const strID As String = "MaxID"
On Error Resume Next
With ActiveDocument
  For i = 1 To .Variables.Count
    If .Variables(i).Name = strID Then
      ID = .Variables(strID).Value
      ID = InputBox(Prompt:="Please input the Reset ID #," & vbCr & _
        "equal to the last valid ID #", Title:="ID Creation", Default:=ID)
      Variables(strID).Value = ID
      Exit For
    End If
  Next
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote