-1

I have to find the text in the format or regex from the document - "([\(]){1}([0-9]){1, 2}([\)]){1}"

I want to replace the number alone found in the regex with a cross reference to the corresponding numbered item found using word macro. The only condition is that it should not take the number item with a style - "Heading1" (which has a number item to it)

For Ex: text found: "(1)" replace the text with cross reference at the number alone

The code I used throw a run time error: "Object Required".

Dim WorkPara As String
Dim CheckP() As Boolean
Dim NumPara As Integer, J As Integer
NumPara = ActiveDocument.Paragraphs.count

ReDim CheckP(NumPara)
For J = 1 To NumPara
    CheckP(J) = False
    WorkPara = ActiveDocument.Paragraphs(J).Range.Text
    If InStr(WorkPara, regex.Pattern("([\(]){1}([0-9]){1}([\)]){1}")) > 0 Then
        CheckP(J) = True
    End If
Next J

For J = NumPara To 1 Step -1
    If CheckP(J) Then
        Selection.Range.InsertCrossReference wdRefTypeNumberedItem, wdNumberFullContext, "1"
    End If
Next J

Could any one help me out with it

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
vikash vishnu
  • 337
  • 1
  • 4
  • 11
  • Which line of code is throwing the error? – Cindy Meister May 09 '18 at 11:18
  • You are using regex the wrong way! Have a look here: [How to Use/Enable (RegExp object) Regular Expression using VBA (MACRO) in word](https://stackoverflow.com/questions/25102372/how-to-use-enable-regexp-object-regular-expression-using-vba-macro-in-word). Also you don't need 2 loops for this. You can do everything in one backwards loop (the second one). – Pᴇʜ May 09 '18 at 11:32

1 Answers1

0

You don't need RegEx for this. Consider:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "(^#)"
    .Forward = True
    .Format = False
    .Wrap = wdFindStop
    .MatchWildcards = False
    .Execute
  End With
  Do While .Find.Found
    If .Paragraphs(1).Style <> wdStyleHeading1 Then
      Set Rng = .Characters(2)
      With Rng
        .InsertCrossReference wdRefTypeNumberedItem, wdNumberFullContext, .Text, True
      End With
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

Note: the above code assumes your cross-reference is to the heading with the found number.

If the bracketed numbers you want to process might be more than one digit, you could use Word's wildcard Find:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "\([0-9]@\)"
    .Forward = True
    .Format = False
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    If .Paragraphs(1).Style <> wdStyleHeading1 Then
      Set Rng = .Duplicate
      With Rng
        .Start = .Start + 1
        .End = .End - 1
        .InsertCrossReference wdRefTypeNumberedItem, wdNumberFullContext, .Text, True
      End With
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
macropod
  • 12,757
  • 2
  • 9
  • 21