Reputation: 11
I'm triying to use this regExp pattern ^SFT(-[A-Z]+){2,3}(-\d+)$
to find a key word in a Word document with VBA Excel but it doesn't work and I get a 5625 error due to special caracter.
Anybody knows how to solve out this?
Examples of data I'm triying to find:
SFT-LTE-SEC-8, SFT-LTE-ROLES-PMR-1, SFT-LTE-MCPTT-2
Sub FulfilMatrix()
Dim oFSO As Scripting.FileSystemObject
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim strPath, headerAndDocument As String
Dim wordRange As Word.Range
Dim BoolFound As Boolean
Dim wordPage As Integer
strPath = "C:\Users\Requirements\LLD LTE.docx"
Set oFSO = New Scripting.FileSystemObject
If oFSO.FileExists(strPath) Then
Set wordApp = New Word.Application
wordApp.Visible = True
With wordApp
'It hides markup when opening
.Options.ShowMarkupOpenSave = False
'file is opened
Set wordDoc = wordApp.Documents.Open(strPath)
End With
End If
Do
With wordDoc.ActiveWindow.Selection.Find
wordDoc.ActiveWindow.Selection.HomeKey unit:=wdStory
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^SFT(-[A-Z]+){2,3}(-\d+)$" ' requirements pattern
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
'.Execute
BoolFound = .Found
End With
If BoolFound Then
' capture du numéro de page du HLD où se trouve la SFT
wordPage = wordDoc.ActiveWindow.Selection.Information(wdActiveEndPageNumber)
debug.print wordpage
end if
Loop While BoolFound
'to quit the entire Application:
wordApp.Quit SaveChanges:=wdDoNotSaveChanges
'screenUpdating on
Application.ScreenUpdating = True
Set objWApp = Nothing
Set oFSO = Nothing
Set wordDoc = Nothing
Set wordRange = Nothing
Set wordParagraph = Nothing
EndTime = Timer '
Debug.Print EndTime - StartTime;
If EndTime - StartTime Mod (60) = 0 Then
Debug.Print EndTime - StartTime;
Else
Debug.Print ((EndTime - StartTime) \ 60)
End If
End Sub
Upvotes: 0
Views: 470
Reputation: 7860
There are numerous errors in your code so I have added comments to show where they are and how to fix them.
You will still need to fix the wildcards as there isn't enough data in your question to identify your exact requirements. You can find details on using wildcards at the Word MVP site: https://wordmvp.com/FAQs/General/UsingWildcards.htm
Sub FulfilMatrix()
Dim oFSO As Scripting.FileSystemObject
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
'each variable in the line must have a datatype
Dim strPath As String, headerAndDocument As String
Dim wordRange As Word.Range
'Dim BoolFound As Boolean - not needed
Dim wordPage As Integer
strPath = "C:\Users\Requirements\LLD LTE.docx"
Set oFSO = New Scripting.FileSystemObject
If oFSO.FileExists(strPath) Then
Set wordApp = New Word.Application
wordApp.Visible = True
With wordApp
'It hides markup when opening
.Options.ShowMarkupOpenSave = False
'file is opened
Set wordDoc = wordApp.Documents.Open(strPath)
End With
End If
'This do loop is in the wrong place. You need to loop the execution of the Find
' Do
'don't use Selection with VBA work with objects directly
'With wordDoc.ActiveWindow.Selection.Find
Set wordRange = wordDoc.Content
With wordRange
With .Find
'not needed if working with objects directly
'wordDoc.ActiveWindow.Selection.HomeKey unit:=wdStory
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^SFT(-[A-Z]+){2,3}(-\d+)$" ' requirements pattern
.Forward = True
'need to stop execution at end of document
'.Wrap = wdFindContinue
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
'if you use wildcards in .Text, Find needs to know that they are not literal characters
'.MatchWildcards = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
'.Execute
'Unless the find is executed Found will evaluate to false
'BoolFound = .Found
End With
'Loop the exectution, not the result
'If BoolFound Then
Do While .Find.Execute()
'Find redefines wordRange to the range of the match
' capture du numéro de page du HLD où se trouve la SFT
wordPage = .Information(wdActiveEndPageNumber)
Debug.Print wordPage
'need to collapse the range so that Find continues after the match
.Collapse wdCollapseEnd
' End If
' Loop While BoolFound
Loop
End With
'to quit the entire Application:
wordApp.Quit SaveChanges:=wdDoNotSaveChanges
'screenUpdating on
Application.ScreenUpdating = True
'Set objWApp = Nothing - you don't have a variable named objWApp
Set wordApp = Nothing
Set oFSO = Nothing
Set wordDoc = Nothing
Set wordRange = Nothing
Set wordParagraph = Nothing
EndTime = Timer '
Debug.Print EndTime - StartTime;
If EndTime - StartTime Mod (60) = 0 Then
Debug.Print EndTime - StartTime;
Else
Debug.Print ((EndTime - StartTime) \ 60)
End If
End Sub
Upvotes: 2