M
malamin
At the outset let me say that I'm trying to do this project after
familiarizing myself with the word object model. In a nutshell, I'm opening
a document, storing text in a variable and opening another document to
search for that same text. My problem is my search fails even though I can
verify the existence of the search text in the document. Example:
In document usecase I'm storing the following text in a variable.
UCR332.2 SA or PA has entitlement to run this report.
In document Trace I see the following value but my search fails.
UCR332.2: SA or PA has entitlement to run this report.
In the first document I realize that I don't have a colon but I'm adding the
colon into the string before performing my search. When I loop through the
document to perform my search, sometimes it finds the results, sometimes it
doesn't. The most frustrating part is it's not failing every time. Here's
my code. I'd greatly appreciate if anyone could point out any logical
errors I may be making.
Thanks
Sub Combine()
Dim docTraceTree As Document, docUseCase As Document
Dim para As Paragraph
Dim rngUseCase As Range, rngTraceTree As Range
Dim intBkmCount As Integer
Dim bkm As Bookmark
Dim strCaseBookMark As String
Dim J As Integer
Dim strMyPara As String
Dim strMyTrace As String
Set docTraceTree = Documents.Open(FileName:="C:\Documents and
Settings\A021871\Desktop\TraceTree.doc", Visible:=True)
Set docUseCase = Documents.Open(FileName:="C:\Documents and
Settings\A021871\Desktop\UseCase.doc", Visible:=True)
Dim strLeftOfColon As String
Dim strRightOfColon As String
Dim intLength As Integer
Dim intColonLocation As Integer, intPeriodLocation As Integer
Dim strSearch As String, strTemp As String
Set rngTraceTree = docTraceTree.Content
docUseCase.SaveAs "C:\Malik.doc"
Call CheckSpaces
For J = 1 To docUseCase.Bookmarks.Count
Set bkm = docUseCase.Bookmarks(J)
Set rngUseCase = docUseCase.Bookmarks(J).Range
strMyPara = Trim(rngUseCase.Text)
intLength = Len(strMyPara)
strLeftOfColon = Left(strMyPara, (InStr(1, strMyPara, " ") - 1)) & ":"
intColonLocation = InStr(1, strMyPara, " ") - 1
strRightOfColon = (Mid(strMyPara, intColonLocation + 1))
strRightOfColon = RTrim(strRightOfColon)
strSearch = strLeftOfColon & strRightOfColon
strSearch = Trim(strSearch)
intPeriodLocation = InStr(intColonLocation + 1, strSearch, ".")
If intPeriodLocation > 0 Then
strTemp = Left(strSearch, intPeriodLocation)
strSearch = strTemp
End If
'strSearch = strTemp
rngTraceTree.Find.Text = strSearch
'rngTraceTree.Find.MatchAllWordForms = False
rngTraceTree.Find.MatchCase = False
rngTraceTree.Find.MatchWildcards = True
rngTraceTree.Find.Execute
If rngTraceTree.Find.Found Then
Do
Set rngTraceTree = rngTraceTree.Next(wdParagraph, 1)
If Not rngTraceTree.Text Like "UCR*" Then
With rngUseCase
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter
.Text = vbCrLf & rngTraceTree.Text
.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Font.Bold = True
.Font.Color = wdColorBlue
End With
End If
Loop Until rngTraceTree.Text Like "UCR*"
End If
Next
docTraceTree.Close
docUseCase.Close
Set rngUseCase = Nothing
Set rngTraceTree = Nothing
Set bkm = Nothing
Set docTraceTree = Nothing
Set docUseCase = Nothing
End Sub
Sub CheckSpaces()
Call MakeChanges(".")
Call MakeChanges("!")
Call MakeChanges(":")
Call MakeChanges(Chr$(34))
End Sub
Sub MakeChanges(PuncMark As String)
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
'Selection.Find.Style = ActiveDocument.Styles(StyName)
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = PuncMark & " "
.Replacement.Text = PuncMark & ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Text = PuncMark & " "
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
familiarizing myself with the word object model. In a nutshell, I'm opening
a document, storing text in a variable and opening another document to
search for that same text. My problem is my search fails even though I can
verify the existence of the search text in the document. Example:
In document usecase I'm storing the following text in a variable.
UCR332.2 SA or PA has entitlement to run this report.
In document Trace I see the following value but my search fails.
UCR332.2: SA or PA has entitlement to run this report.
In the first document I realize that I don't have a colon but I'm adding the
colon into the string before performing my search. When I loop through the
document to perform my search, sometimes it finds the results, sometimes it
doesn't. The most frustrating part is it's not failing every time. Here's
my code. I'd greatly appreciate if anyone could point out any logical
errors I may be making.
Thanks
Sub Combine()
Dim docTraceTree As Document, docUseCase As Document
Dim para As Paragraph
Dim rngUseCase As Range, rngTraceTree As Range
Dim intBkmCount As Integer
Dim bkm As Bookmark
Dim strCaseBookMark As String
Dim J As Integer
Dim strMyPara As String
Dim strMyTrace As String
Set docTraceTree = Documents.Open(FileName:="C:\Documents and
Settings\A021871\Desktop\TraceTree.doc", Visible:=True)
Set docUseCase = Documents.Open(FileName:="C:\Documents and
Settings\A021871\Desktop\UseCase.doc", Visible:=True)
Dim strLeftOfColon As String
Dim strRightOfColon As String
Dim intLength As Integer
Dim intColonLocation As Integer, intPeriodLocation As Integer
Dim strSearch As String, strTemp As String
Set rngTraceTree = docTraceTree.Content
docUseCase.SaveAs "C:\Malik.doc"
Call CheckSpaces
For J = 1 To docUseCase.Bookmarks.Count
Set bkm = docUseCase.Bookmarks(J)
Set rngUseCase = docUseCase.Bookmarks(J).Range
strMyPara = Trim(rngUseCase.Text)
intLength = Len(strMyPara)
strLeftOfColon = Left(strMyPara, (InStr(1, strMyPara, " ") - 1)) & ":"
intColonLocation = InStr(1, strMyPara, " ") - 1
strRightOfColon = (Mid(strMyPara, intColonLocation + 1))
strRightOfColon = RTrim(strRightOfColon)
strSearch = strLeftOfColon & strRightOfColon
strSearch = Trim(strSearch)
intPeriodLocation = InStr(intColonLocation + 1, strSearch, ".")
If intPeriodLocation > 0 Then
strTemp = Left(strSearch, intPeriodLocation)
strSearch = strTemp
End If
'strSearch = strTemp
rngTraceTree.Find.Text = strSearch
'rngTraceTree.Find.MatchAllWordForms = False
rngTraceTree.Find.MatchCase = False
rngTraceTree.Find.MatchWildcards = True
rngTraceTree.Find.Execute
If rngTraceTree.Find.Found Then
Do
Set rngTraceTree = rngTraceTree.Next(wdParagraph, 1)
If Not rngTraceTree.Text Like "UCR*" Then
With rngUseCase
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter
.Text = vbCrLf & rngTraceTree.Text
.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Font.Bold = True
.Font.Color = wdColorBlue
End With
End If
Loop Until rngTraceTree.Text Like "UCR*"
End If
Next
docTraceTree.Close
docUseCase.Close
Set rngUseCase = Nothing
Set rngTraceTree = Nothing
Set bkm = Nothing
Set docTraceTree = Nothing
Set docUseCase = Nothing
End Sub
Sub CheckSpaces()
Call MakeChanges(".")
Call MakeChanges("!")
Call MakeChanges(":")
Call MakeChanges(Chr$(34))
End Sub
Sub MakeChanges(PuncMark As String)
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
'Selection.Find.Style = ActiveDocument.Styles(StyName)
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = PuncMark & " "
.Replacement.Text = PuncMark & ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Text = PuncMark & " "
Selection.Find.Execute Replace:=wdReplaceAll
End Sub