Jump to content

Recommended Posts

Posted

I am attempting to craft a macro in Word which will find a string that matches a certain pattern, and then replaces it with an image based on that pattern.

 

So, for example, in the file may be [signature-Cross]. It should find it based on [signature-*], then extract the "Cross" part.

 

I've got the find/replace with image code sorted already, but my brain refuses to work with Regex stuff. No matter how much I try to learn it.

 

Can someone give me an idea of the regex pattern I need to find that stuff, and how to then extract into a variable the "Cross" part?

Posted

This interested me so I had a go. Don't know if this is actually what you wanted and not tested exhaustively but this is all I've got and I need to sleep now. Hope at least some of it helps.

Sub ReplaceTags()
'
'
Dim RegEx As RegExp
Dim extractedPortion As String

Set RegExp = New RegExp
RegExp.IgnoreCase = ignore_case
RegExp.Global = False
RegExp.Pattern = "\[signature-\S*\]"

Do While RegExp.Test(ActiveDocument.range.text)
   Set Matches = RegExp.Execute(ActiveDocument.range.text)
   For Each Match In Matches
       
       extractedPortion = Right(Match.Value, Len(Match.Value) - 11)
       extractedPortion = Left(extractedPortion, Len(extractedPortion) - 1)
       If extractedPortion <> "" Then
           'extracted portion now holds the relevant text from the placeholder. Use this to insert appropriate image here
           'for now just puts the extracted text back into the document
           ActiveDocument.range(Match.FirstIndex, Match.FirstIndex + Len(Match.Value)).text = extractedPortion
       End If
   Next
Loop

End Sub

  • Thanks 1
Posted

And now something a bit more like what you were asking for using capture groups to extract the required part of the tags. Ti si sfar more elegant.

 

Sub ReplaceTags()
'
'
Dim RegEx As RegExp
Dim replacePattern As String
Dim extractedPortion As String

Set RegExp = New RegExp
RegExp.IgnoreCase = ignore_case
RegExp.Global = True
RegExp.Pattern = "(\[signature-)(\S*)(\])"
replacePattern = "$2"

Set Matches = RegExp.Execute(ActiveDocument.Range.Text)
For Each Match In Matches
   ActiveDocument.Range.Text = RegExp.Replace(ActiveDocument.Range.Text, replacePattern)
Next

End Sub

  • Thanks 1
Posted

Is there a way of getting the second half of the regex match into its own variable in the second method - ie. outside of the RegEx.Replace function?

 

Reason being I have to pass that part specifically to another sub.

Posted

Is this along the lines of what you mean? It's using the Match.submatches(index) property to obtain the value of a capture group.

 

Sub ReplaceTags()
'
'
Dim RegEx As RegExp
Dim replaceText As String
Dim extractedPortion As String

Set RegExp = New RegExp
RegExp.IgnoreCase = ignore_case
RegExp.Global = True
RegExp.Pattern = "(\[signature-)(\S*)(\])"

Set Matches = RegExp.Execute(ActiveDocument.Range.Text)
For Each Match In Matches
   
   replaceText = doSomething(Match.SubMatches.Item(1))
   
   ActiveDocument.Range.Text = RegExp.Replace(ActiveDocument.Range.Text, replaceText)
Next

End Sub


Function doSomething(aString As String)
   doSomething = "( Value Inserted: " & aString & " )"
End Function

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...