I am trying to iterate though Section(3) of a Word document, Find a key word then find the end of the block, copy the text and paste it to a bookmark in another section. I am trouble iterating through the document. Text gets pasted into Section(3) and has this structure. It has a carriage return(^p) at the end of each line.
[tt]
|INP| BISACODYL SUPP,RTL
10MG IN RECTUM EVERY DAY AS NEEDED FOR CONSTIPATION
|REMOTE| CHLORHEXIDINE GLUCONATE RINSE,ORAL
15 ML BY MOUTH FOUR TIMES A DAY swisha nd spit
BCMA ORDER LAST ACTION: 12/15/13 20:13 GIVEN
|OPT| CHOLECALCIFEROL (VIT D3) 2,000UNIT TAB (Status = ACTIVE)
TAKE ONE TABLET BY MOUTH EVERY DAY
Last Released: 1/16/13 Days Supply: 90
Rx Expiration Date: 1/16/14 Refills Remaining: 3
|NEW| CHOLECALCIFEROL (VIT D3) TAB
2000UNIT BY MOUTH EVERY DAY
BCMA ORDER LAST ACTION: 12/15/13 08:44 GIVEN
[/tt]
I found this example Link .
I tried to incorporate it into my code, but it does not seem go through each paragraph and also no longer stays contained into Section(3). There are about 10-12 different |*| prefixs. I know I will have problems with the bookmarks also (using a Greg Maxey example), but that is a different problem.
The Selection.Find.ClearFormatting and after does work correctly.
I am open to any suggestions on how to iterate through the text. This was the only example I found. I am also open to other ways to
Thank you.
You don't know what you don't know...
[tt]
|INP| BISACODYL SUPP,RTL
10MG IN RECTUM EVERY DAY AS NEEDED FOR CONSTIPATION
|REMOTE| CHLORHEXIDINE GLUCONATE RINSE,ORAL
15 ML BY MOUTH FOUR TIMES A DAY swisha nd spit
BCMA ORDER LAST ACTION: 12/15/13 20:13 GIVEN
|OPT| CHOLECALCIFEROL (VIT D3) 2,000UNIT TAB (Status = ACTIVE)
TAKE ONE TABLET BY MOUTH EVERY DAY
Last Released: 1/16/13 Days Supply: 90
Rx Expiration Date: 1/16/14 Refills Remaining: 3
|NEW| CHOLECALCIFEROL (VIT D3) TAB
2000UNIT BY MOUTH EVERY DAY
BCMA ORDER LAST ACTION: 12/15/13 08:44 GIVEN
[/tt]
I found this example Link .
I tried to incorporate it into my code, but it does not seem go through each paragraph and also no longer stays contained into Section(3). There are about 10-12 different |*| prefixs. I know I will have problems with the bookmarks also (using a Greg Maxey example), but that is a different problem.
The Selection.Find.ClearFormatting and after does work correctly.
Code:
Option Explicit
Sub FindMoveBlockText()
Dim strContent As String
Dim strPrefix As String
Dim doc As Document
Dim para As Paragraph
Dim paraNext As Paragraph
Dim i As Integer
ActiveDocument.Sections(3).Range.Select
Set para = Selection.Paragraphs.First
Do While Not para Is Nothing
Set paraNext = para.Next
Selection.Find.ClearFormatting
With Selection.Find
.ClearFormatting
.Text = "|*|"
.Replacement.Text = ""
.Forward = True
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = True
.Wrap = wdFindStop
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^13^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
End With
Selection.Find.Execute
strContent = Selection.Text
i = 0
i = InStr(2, strContent, "|")
strPrefix = Trim(Mid(strContent, 1, i + 1))
If Len(strContent) = 0 Then
MsgBox "No Text Found"
Exit Sub
Else
Select Case strPrefix
Case "|OPT|"
MsgBox "OPT"
'Call WriteToBookmarkRange(ActiveDocument, "bmVAMed", strContent)
Case "|REMOTE|"
MsgBox "REMOTE"
'Call WriteToBookmarkRange(ActiveDocument, "bmRemoteMed", strContent)
Case "|NONVA|"
MsgBox "NONVA"
'Call WriteToBookmarkRange(ActiveDocument, "bmNonVAMed", strContent)
Case "|NEW|"
MsgBox "NEW"
'Call WriteToBookmarkRange(ActiveDocument, "bmVAMed", strContent)
'Call WriteToBookmarkRange(ActiveDocument, "bmNewMed", strContent)
Case Else
MsgBox "Unknown Prefix" & vbCrLf & _
"Call for assistance"
End Select
End If
Set para = paraNext
Loop
End Sub
Thank you.
You don't know what you don't know...