• Inserting a calculated number (W97)

    Author
    Topic
    #384329

    I’m creating a template that I want to include the following functionality:

    I want to insert an ID number at the end of a paragraph. (i)
    The ID number must increase by 1 each time (i+1)
    The first ID number for a given document must start at 1
    The last ID number used must be stored within the document, so that future editing of the document may continue with the last ID number issued.

    I’ve started creating something , but don’t know where to go with it. (it’s the whole variable part that I’m still weak on…. )
    I don’t know where to store the ‘last used id number’, nor how to increment.

    Sub InsertIDNumberManually()
    Viewing 1 reply thread
    Author
    Replies
    • #658949

      One way is to use a Document Variable to hold the ID number.

      The trick with docvars is this: you can insert references to docvars
      (in order to display the value of the docvar) via Insert | Fields — and via
      code; but you can assign values to docvars only via code.

      So, you end up creating a handy set of docvar functions and subs:

      DocVarAssign
      DocVarExists
      DocVarDelete, etc.

      They are about as complex as this:
      Function DocVarDelete(DocVar As String) As String
      On Error Resume Next
      ActiveDocument.Variables(DocVar).Delete
      Err.Clear
      End Function

      and this:
      Sub DocVarAssign(DocVar As String, ByVal DocValue As Variant)
      On Error Resume Next
      With ActiveDocument
      .Variables(DocVar).Delete
      .Variables.Add Name:=DocVar, Value:=DocValue
      End With
      Err.Clear
      End Sub

      That’s a start anyway.

    • #659312

      Is the ID number to be fixed to each paragraph. For instance, is the number stream always sequential if I add a new paragraph (or remove some) early in the document does it get the next available number or will the subsequent paragraphs renumber to make way for it.

      If they renumber then I would use a seq field instead of a hard coded number.

      Is every paragraph numbered? If so you could use a Range.Paragraphs.Count to easily work out the next number available for use.

      If the numbers are fixed, can be applied out of order, and dont apply to every paragraph then I would use a macro solution with a bookmark.
      The macro would search for the bookmark – read its contents and then increment this number by one and insert this next number at the required location with the same bookmark name (this removes the previous location). Finding the last added number is then as simple as finding the bookmark.

      • #659427

        This number is an ID number for requirement traceability. It will be attached to the end of many paragraphs perminantly, but only upon assignment (Tagging each requirement). I want to attach the code to a toolbar button “Issue ID #” When the press the button, the macro assigns the next available ID number to the end of the paragraph (assigns a unique number to the requirement).

        • #659430

          Can I store this variable in the Doc Object/Document_Open?
          I tried creatint a lil workflow diagram…..

          1.

          Dim IDNUM
          i=0
          IDNUM = i+1
          (Stored in Document_Open) ???
          

          2. Code that I entered above (to execute action, activated by a toolbar button)
          3.

    Viewing 1 reply thread
    Reply To: Inserting a calculated number (W97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: