vbams-wordword-2007sequential-number

Is there an "elegant" way to enact Auto-Numbering in a Word 2007 Macro (VBA)?


Ok, so basically we have a couple of unique sections in a macro-enabled template for Word 2007 and for each section, we have 2 entries that are standard for the form, and then there are about 20 optional entries that are all handled by AutoText. The formatting is identical between the template and the AutoText entries and I'm wanting to auto-number the entries as they are added (either by the user typing the AutoText keyphrase or hitting a button on the ribbon to insert it). Is there an easy way to do this? Here is the block of code where one of these (numbered) entries is handled and what I've tried to implement as a numbering scheme from other suggestions on other forums (couldn't find anything useful here):

    Case "cboFF"
                        SetMargins 0, 1, 1
                        Selection.ParagraphFormat.Space1
                        Selection.Text = "FINDINGS OF FACT" & vbLf
                        Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
                        Selection.Font.Bold = True
                        Selection.Font.Underline = wdUnderlineSingle
                        Selection.Collapse (wdCollapseEnd)
                        Selection.Text = vbLf
                        Selection.ParagraphFormat.SpaceAfter = 6
                        Selection.Collapse (wdCollapseEnd)
                        Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
                        Selection.Font.Underline = wdUnderlineNone
                        Selection.Font.Bold = False
                        SetMargins 0, 1, 1
                        'With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
                        '.NumberFormat = "%1."
                        '.TrailingCharacter = wdTrailingTab
                        '.NumberStyle = wdListNumberStyleNone
                        '.NumberPosition = InchesToPoints(0.5)
                        '.Alignment = wdListLevelAlignLeft
                        '.TextPosition = InchesToPoints(0.5)
                        '.ResetOnHigher = 0
                        '.StartAt = 1
                        'AutoNumberOnFOF
                        Selection.Text = "On " & strDateFOF & ", an industrial appeals judge certified that the parties agreed to include the Jurisdictional History in the Board record solely for jurisdictional purposes." & vbLf
                        Selection.ParagraphFormat.SpaceAfter = 6
                        Selection.Collapse (wdCollapseEnd)
                        Select Case strCaseCategory
                            Case "IND", "IND SELF-I"
                                 If frmIIOD.optII.Value = True Then
                                    Selection.Text = "II-FF"
                                    Selection.Range.InsertAutoText
                                    Selection.Collapse (wdCollapseEnd)
                                    Selection.Text = vbLf
                                    Selection.ParagraphFormat.SpaceAfter = 6
                                    Selection.Collapse (wdCollapseEnd)
                                    GoToEnd
                                End If
                                If frmIIOD.optOD.Value = True Then
                                    Selection.Text = "OD-FF"
                                    Selection.Range.InsertAutoText
                                    Selection.Collapse (wdCollapseEnd)
                                    Selection.Text = vbLf
                                    Selection.ParagraphFormat.SpaceAfter = 6
                                    Selection.Collapse (wdCollapseEnd)
                                    GoToEnd
                                End If
                                If frmIIOD.optNotNeeded.Value = True Then
                                    Selection.Text = vbLf
                                    Selection.ParagraphFormat.SpaceAfter = 6
                                    Selection.Collapse (wdCollapseEnd)
                                End If
                            Case Else
                                'Do Nothing
                        End Select

Any constructive comments will be much appreciated to help solve this issue. I'm still very new to programming as a whole and most of my experience lies in C# and Java.

Edit: The structure of the document is essentially a set of itemized lists containing legal text that is updated by a user as the appeal process goes through various stages. In each of the last 2 sections the itemized lists need to follow a specific numbering scheme (num at .5", text at 1", right tab at 1") which is not native to Word 2007. There is a bolded heading for each of the sections that is the starting point of the numbering. The AutoText entries will be added as needed by the user. The rest of the document pulls information from our database and contains the legal wording necessary for the document. If I could just figure out how to initiate the numbering for each section individually, then I could finish this up.


Solution

  • I've actually figured out my own solution to the issue. The problem was trying to insert an AutoText entry on the (2nd) line of numbering since it reads the whole line and thinks it is an AutoText entry. Rather than only reading ii-ff or od-ff, it was reading 1. ii-ff, which isn't a valid AutoText entry (by name).

                        Case "cboFF"
                            SetMargins 0, 1, 1
                            Selection.ParagraphFormat.Space1
                            Selection.Text = "FINDINGS OF FACT" & vbLf
                            Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
                            Selection.Font.Bold = True
                            Selection.Font.Underline = wdUnderlineSingle
                            Selection.Collapse (wdCollapseEnd)
                            Selection.Text = vbLf
                            Selection.ParagraphFormat.SpaceAfter = 6
                            Selection.Collapse (wdCollapseEnd)
                            Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
                            Selection.Font.Underline = wdUnderlineNone
                            Selection.Font.Bold = False
                            SetMargins -0.5, 1, 1
                            'AutoNumberOn
                            Selection.Text = "1." & vbTab & "On " & strDateFOF & strEntry1 & vbLf
                            Selection.ParagraphFormat.SpaceAfter = 6
                            Selection.Collapse (wdCollapseEnd)
                            Select Case strCaseCategory
                                Case "IND", "IND SELF-I"
                                     If frmIIOD.optII.Value = True Then
                                        Selection.Text = "2." & vbTab & strIIEntry & vbLf
                                        Selection.ParagraphFormat.SpaceAfter = 6
                                        Selection.Collapse (wdCollapseEnd)
                                        Selection.Text = "3." & vbTab & "" & vbLf
                                        Selection.Collapse (wdCollapseEnd)
                                    End If
                                    If frmIIOD.optOD.Value = True Then
                                        Selection.Text = "2." & vbTab & strODEntry & vbLf
                                        Selection.Text = "3." & vbTab & "" & vbLf
                                        Selection.Collapse (wdCollapseEnd)
                                    End If
                                    If frmIIOD.optNotNeeded.Value = True Then
                                        Selection.Text = "2." & vbTab & "" & vbLf
                                        Selection.ParagraphFormat.SpaceAfter = 6
                                        Selection.Collapse (wdCollapseEnd)
                                    End If
                                Case Else
                                    'Do Nothing
                            End Select
    

    This allows the to insert AutoText entries on the empty lines and continue the pre-formatted numbering system which isn't native to Word 2007. Now I just need to figure out the easiest way to higlight specific words within the inserted selection. Shouldn't be too bad:

        'set array of text entries (6)
        'begin loop
        'find and highlight entry(i)
        'end loop