agent86
11-11-2013, 09:09 PM
Word 2010
New to VBA...
Sample document
……Inst#: 1……
……Inst#: 2……
……Inst#: 3……
I have created a very short document example with 3 “report sections”. The is the starting marker. is the closing marker. Any number of characters can be between the starting and ending markers. The actual document will have 3-5 sets of markers. That is determined by the user.
The … indicate there couId be any number of characters between and the string “Inst#: 2” and any number of characters following the string “Inst#: 2” but always followed at some point with the closing marker. What I want to do is find the “report section” that contains the desired string and replace all the text between the markers with 3 words, “xman goes here”. I would end up with a set of markers that says “xman goes here”.
The code I have shown below finds Inst#: 1 just fine. The problem is the procedure hangs up in the first set of markers and goes into an infinite loop because it can’t find the string “Inst#: 2” string in the first set of markers. I want to search for the string Inst#: 2 in all sets of markers until it is found and not hang up if the set of markers I am looking for is not the first set. When it examines the sets of markers I want it to keep going to the next set and see if the “Inst#: 2” is there. It always hangs up in the loop with the section of code I have marked with “????”’s. I do not know how to tell it to “bypass” the first set of markers and move on to the next set of markers until it finds the string it is looking for.
Sub RemoveReportByInstance(ByVal strReportInstanceNumber As String)
Dim doc As Document
Dim txtRange As Range
Dim startTag As String
Dim endTag As String
Dim s As Long
Dim e As Long
Dim ReportFound As Boolean
strReportInstanceNumber = "Inst#: " & strReportInstanceNumber
ReportFound = False
System.Cursor = wdCursorWait
Application.ScreenUpdating = False
ActiveWindow.View.ShowHiddenText = True
' start at top of document
Selection.HomeKey Unit:=wdStory
' search report blocks until we find instance number
' instance number will always be unique for mt caseid
' moved to her to test if infinite loop goes away
Set doc = ActiveDocument
Set txtRange = doc.Content
Do
' markers
startTag = ""
endTag = ""
'Find the opening tag
With txtRange.Find
.ClearFormatting
.Text = startTag
.Forward = True
.Execute
If .Found Then
s = txtRange.Start
Else
GoTo EarlyExit
End If
End With
'Find the closing tag
Set txtRange = doc.Range(txtRange.End, doc.Content.End)
With txtRange.Find
.Text = endTag
.Forward = True
.Execute
If .Found Then
e = txtRange.End
Else
GoTo EarlyExit
End If
End With
Set txtRange = doc.Range(s, e)
' check range for Instance we are looking for
' is the instance we are looking for in there? Inst#: 2 there?
' ????????????????????????????????????????????????????????????????
With txtRange.Find
.Text = strReportInstanceNumber
.Forward = True
.Execute
If .Found Then
' you are on correct report so re-orient the start and ending
Set txtRange = doc.Range(s, e)
txtRange.Text = startTag & "xman goes here" & endTag
ReportFound = True
End If
End With
' ????????????????????????????????????????????????????????????????
Loop Until (ReportFound = True)
ActiveWindow.View.ShowHiddenText = False
Application.ScreenUpdating = True
Application.ScreenRefresh
Exit Sub
EarlyExit:
MsgBox "Embedded Report Header not found in this document!", vbInformation
End Sub
New to VBA...
Sample document
……Inst#: 1……
……Inst#: 2……
……Inst#: 3……
I have created a very short document example with 3 “report sections”. The is the starting marker. is the closing marker. Any number of characters can be between the starting and ending markers. The actual document will have 3-5 sets of markers. That is determined by the user.
The … indicate there couId be any number of characters between and the string “Inst#: 2” and any number of characters following the string “Inst#: 2” but always followed at some point with the closing marker. What I want to do is find the “report section” that contains the desired string and replace all the text between the markers with 3 words, “xman goes here”. I would end up with a set of markers that says “xman goes here”.
The code I have shown below finds Inst#: 1 just fine. The problem is the procedure hangs up in the first set of markers and goes into an infinite loop because it can’t find the string “Inst#: 2” string in the first set of markers. I want to search for the string Inst#: 2 in all sets of markers until it is found and not hang up if the set of markers I am looking for is not the first set. When it examines the sets of markers I want it to keep going to the next set and see if the “Inst#: 2” is there. It always hangs up in the loop with the section of code I have marked with “????”’s. I do not know how to tell it to “bypass” the first set of markers and move on to the next set of markers until it finds the string it is looking for.
Sub RemoveReportByInstance(ByVal strReportInstanceNumber As String)
Dim doc As Document
Dim txtRange As Range
Dim startTag As String
Dim endTag As String
Dim s As Long
Dim e As Long
Dim ReportFound As Boolean
strReportInstanceNumber = "Inst#: " & strReportInstanceNumber
ReportFound = False
System.Cursor = wdCursorWait
Application.ScreenUpdating = False
ActiveWindow.View.ShowHiddenText = True
' start at top of document
Selection.HomeKey Unit:=wdStory
' search report blocks until we find instance number
' instance number will always be unique for mt caseid
' moved to her to test if infinite loop goes away
Set doc = ActiveDocument
Set txtRange = doc.Content
Do
' markers
startTag = ""
endTag = ""
'Find the opening tag
With txtRange.Find
.ClearFormatting
.Text = startTag
.Forward = True
.Execute
If .Found Then
s = txtRange.Start
Else
GoTo EarlyExit
End If
End With
'Find the closing tag
Set txtRange = doc.Range(txtRange.End, doc.Content.End)
With txtRange.Find
.Text = endTag
.Forward = True
.Execute
If .Found Then
e = txtRange.End
Else
GoTo EarlyExit
End If
End With
Set txtRange = doc.Range(s, e)
' check range for Instance we are looking for
' is the instance we are looking for in there? Inst#: 2 there?
' ????????????????????????????????????????????????????????????????
With txtRange.Find
.Text = strReportInstanceNumber
.Forward = True
.Execute
If .Found Then
' you are on correct report so re-orient the start and ending
Set txtRange = doc.Range(s, e)
txtRange.Text = startTag & "xman goes here" & endTag
ReportFound = True
End If
End With
' ????????????????????????????????????????????????????????????????
Loop Until (ReportFound = True)
ActiveWindow.View.ShowHiddenText = False
Application.ScreenUpdating = True
Application.ScreenRefresh
Exit Sub
EarlyExit:
MsgBox "Embedded Report Header not found in this document!", vbInformation
End Sub