PDA

View Full Version : Nested do loop, find stuck on first selection



lkpederson
03-22-2014, 03:05 PM
What the code is *supposed* to do:
1. Search to first occurrence of the word "SUBMITTALS" then
2. Search after that for sentences starting with "RSN ## ## ##-#" where # is a number.
3. Search stops when these statements are loaded into an array. Currently I have the search stopping when it
reaches a style named "_03_CSI TEMPLATE". My thought is that the search is linear i.e. follows the document
word by word thus when it comes to the next occurrence of this style, it should stop. Obviously... not so much.
4. Copy the array into another document

What it's doing:
1. Finding "SUBMITTALS"
2. Copying the first occurrence endlessly into another document.

Any suggestions?

By the bye, I do have a working macro that searches the entire document and returns every occurrence of the above but this
is not what I want. The other occurrences, if they occur elsewhere in a sentence (not the beginning), are unimportant
to the search.


Option Explicit

Function GetFolder(fLoc) As String
Dim oFolder As Object
GetFolder = ""
' Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, fLoc, 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.path
Set oFolder = Nothing
End Function

Sub RSN_FileSrchTableA()
' proggie works but finds all RSN XX XX XX-X in a document instead of stopping in the _
submittals section. still needs fine tuning. NOT WORKING. Currently it _
stays at the same line and keeps copying it instead of looping through.

Dim files ' these are all variant variables and must be converted to strings to use
Dim nDoc As Word.Document
Dim SourceFolder As String, FileFolder As String, pathToPutSaveDoc As String
Dim fLoc As String, AllTogether As String, cRngSplit() As String
Dim cRng As Word.Range
Dim numSentences As Integer, i As Integer
Dim tbl As Table

Set nDoc = Documents.Add
fLoc = "Location of files directory to change?"
SourceFolder = GetFolder(fLoc) & "\"
files = Dir(SourceFolder & "*.doc")

Do While files <> ""
Documents.Open SourceFolder & files

' Sets the variable cRng to include the entire document's contents
' so it can be searched
Set cRng = ActiveDocument.Content

' removes any formatting from the "Find" command
cRng.Find.ClearFormatting

' loop to search for text starting with "RSN" and ending in a period.
With cRng.Find
Application.ScreenUpdating = False
.Forward = True
.Text = "SUBMITTALS"
.Replacement.Text = ""
.MatchCase = True
.Wrap = wdFindStop
.Execute

If .Found Then
cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
Do While .Found
With cRng.Find
.Forward = True
.Text = "RSN ^#^# ^#^# ^#^#-^#,"
.Replacement.Text = ""
.MatchCase = True
.Wrap = wdFindContinue
.Execute

cRng.Expand Unit:=wdParagraph 'Unit:=wdCharacter, use this line copy to a hard return
' cRng.MoveEndUntil Cset:=".", Count:=Word.wdForward ' use this to search _
for a specfic character
' Using split() splits a string. By using the "2", it stops the search at the first _
occurrence. In this case, it stops looking after the first comma.
cRngSplit() = Split(cRng, ",", 2)
' AllTogether = cRngSplit(0) & vbTab & ActiveDocument.Name & _
' vbTab & Trim(Left(cRngSplit(1), Len(cRngSplit(1)) - 1)) & vbCrLf
nDoc.Range.InsertAfter cRng.Text 'AllTogether
cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
If cRng.ParagraphStyle = "_03_CSI TEMPLATE" Then
Exit Do
End If
.Execute
End With
Selection.MoveDown Unit:=wdLine, Count:=2
Loop
End If
End With
' closing the open document before opening another one.
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

files = Dir()
Loop

numSentences = nDoc.Paragraphs.Count

' parse data into a table.
nDoc.Range.Select
Selection.ConvertToTable Separator:=wdSeparateByTabs, NumColumns:=3, _
NumRows:=numSentences, AutoFitBehavior:=wdAutoFitFixed

' user selects where to save file
Application.ScreenUpdating = True
fLoc = "Directory to save results?"
pathToPutSaveDoc = GetFolder(fLoc) & "\"
nDoc.SaveAs2 filename:=pathToPutSaveDoc & "RSN.docx"

End Sub

snb
03-22-2014, 03:13 PM
sub M_snb()
sn=split(activedocument.content,vbcr &"RSN ")
for j=1 to ubound(sn)
msgbox "RSN " & split(sn(j),",")(0)
next
End sub

You didn't indicate what string should be copied after "RSN ......"
In this example I took the first comma as the limit.

fumei
03-22-2014, 04:40 PM
You didn't indicate what string should be copied after "RSN ......"
In this example I took the first comma as the limit.


1. Search to first occurrence of the word "SUBMITTALS" then
2. Search after that for sentences starting with "RSN ## ## ##-#" where # is a number.

.Text = "RSN ^#^# ^#^# ^#^#-^#,"

lkpederson
03-22-2014, 05:32 PM
snb,
I believe your codes will find ALL occurrences of "RNS XX XX XX-X" and that is not correct. The search needs to stop after the section of the document entitled "SUBMITTALS".

The selection needs to include the entire sentences (Unit:=wdParagraph). Thanks though.

lkpederson
03-22-2014, 05:35 PM
fumei,
I believe your codes will find ALL occurrences of "RNS XX XX XX-X" and that is not correct. The search needs to stop after the section of the document entitled "SUBMITTALS". One way of differentiating that is by a change in Paragraph Style. The next section starts with a paragraph style of "_03_ CSI ARTICLE".

The selection needs to include the entire sentences (Unit:=wdParagraph). Thanks though.

macropod
03-22-2014, 09:15 PM
Try the following. I'll leave you to integrate it into the rest of your code:

Sub Demo()
Application.ScreenUpdating = False
Dim RngFnd As Range
With ActiveDocument
Set RngFnd = .Range
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.MatchWildcards = False
.Text = "SUBMITTALS"
.Format = False
.Execute
End With
If .Find.Found = True Then RngFnd.Start = .End
With .Find
.Text = ""
.Format = True
.Forward = True
.Style = "_03_CSI TEMPLATE"
.Execute
End With
If .Find.Found Then RngFnd.End = .Start
End With
With .Range
With .Find
.Text = "RSN [0-9]{2} [0-9]{2} [0-9]{2}-[0-9]"
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .InRange(RngFnd) Then
With .Duplicate
.End = .Paragraphs.First.Range.End - 1
MsgBox .Text
End With
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End With
Application.ScreenUpdating = True
End Sub

lkpederson
03-23-2014, 05:24 AM
Well I guess I shouldn't type when I'm tired. Macropod's entry works great for what I asked for. Unfortunately I wrote my last post incorrectly. Let's try again.
1. Search for Heading "SUBMITTAL" (has a Style of "_03_CSI ARTICLE").
2. Start search for sentences that begin with "RSN XX XX XX-X" where X = number. These will be discontiguous.
3. Stop the search when the next occurrence of Style = "_03_CSI ARTICLE".

Otherwise the search will pick up other instances of "RSN XX XX XX-X" which are not desired.

Sorry about that.

snb
03-23-2014, 05:36 AM
Sub M_snb()
sn=split(activedocument.content,vbcr &"SUBMITTAL")

For j=1 To UBound(sn)
msgbox "RSN " & split(sn(j),vbcr & RSN ")(1)
Next
End Sub

macropod
03-23-2014, 05:45 AM
Correctly specifying the problem is indeed important!

Try:

Sub Demo()
Application.ScreenUpdating = False
Dim RngFnd As Range
With ActiveDocument
Set RngFnd = .Range
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Text = ""
.Text = "SUBMITTAL"
.Style = "_03_CSI TEMPLATE"
.Format = True
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.MatchWildcards = False
.Execute
End With
If .Find.Found = True Then
.Start = .Duplicate.Paragraphs.Last.Range.End
RngFnd.Start = .Start
End If
With .Find
.Text = ""
.Execute
End With
If .Find.Found Then RngFnd.End = .Start
End With
With .Range
With .Find
.Text = "RSN ^#^# ^#^# ^#^#-^#"
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
If .InRange(RngFnd) Then
With .Duplicate
.End = .Paragraphs.First.Range.End - 1
MsgBox .Text
End With
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End With
Application.ScreenUpdating = True
End Sub

macropod
03-23-2014, 05:50 AM
Sub M_snb()
sn=split(activedocument.content,vbcr &"SUBMITTAL")

For j=1 To UBound(sn)
msgbox "RSN " & split(sn(j),vbcr & RSN ")(1)
Next
End Sub
Once again, your code ignores the clearly stated requirements re the "_03_CSI ARTICLE" Style. See posts #1, #5 and #7.

SamT
03-23-2014, 07:23 AM
:dunno


Sub M_snb()
sn=split(activedocument.content,vbcr &"SUBMITTAL")

msgbox "SUBMITTAL" & split(sn(1),vbcr & "RSN")(1) 'or "RSN")(0)

End Sub

lkpederson
03-24-2014, 11:28 AM
Brilliant! Thank you, gents. You are all gentlemen and scholars.