Copy/Paste: Append to Word Doc

R

ryguy7272

I found the macro below on this DG a while back.

Sub Foo()
Dim i As Long
Application.ScreenUpdating = False
Documents.Add
With Application.FileSearch
'Search in foldername
..LookIn = "C:\test"
..SearchSubFolders = False
..FileName = "*.doc"
..Execute
For i = 1 To .FoundFiles.Count
If InStr(.FoundFiles(i), "~") = 0 Then
Selection.InsertFile FileName:=(.FoundFiles(i)), _
ConfirmConversions:=False, Link:=False, Attachment:=False
Selection.InsertBreak Type:=wdPageBreak
End If
Next i
End With
End Sub


Basically, it copies and pastes everything in folder (or subfolder if the
condition is set to true). I am wondering if there is an easy way to modify
the macro to search all subfolders and copy/paste only certain sections into
a word document, like some kind of summary report. I am looking through
hundreds of word documents, and searching for criteria such as percentage "%"
and 2 or 3 numbers before that, as well as certain key words such as
"Jurisdiction" and "Domicile". Can this be done?

Regards,
Ryan--
 
J

Jay Freedman

ryguy7272 said:
I found the macro below on this DG a while back.

Sub Foo()
Dim i As Long
Application.ScreenUpdating = False
Documents.Add
With Application.FileSearch
'Search in foldername
.LookIn = "C:\test"
.SearchSubFolders = False
.FileName = "*.doc"
.Execute
For i = 1 To .FoundFiles.Count
If InStr(.FoundFiles(i), "~") = 0 Then
Selection.InsertFile FileName:=(.FoundFiles(i)), _
ConfirmConversions:=False, Link:=False, Attachment:=False
Selection.InsertBreak Type:=wdPageBreak
End If
Next i
End With
End Sub


Basically, it copies and pastes everything in folder (or subfolder if
the condition is set to true). I am wondering if there is an easy
way to modify the macro to search all subfolders and copy/paste only
certain sections into a word document, like some kind of summary
report. I am looking through hundreds of word documents, and
searching for criteria such as percentage "%" and 2 or 3 numbers
before that, as well as certain key words such as "Jurisdiction" and
"Domicile". Can this be done?

Regards,
Ryan--

Yes, it can be done, although your description is a little too vague to
provide exact code.

In general, you need to replace the Selection.InsertFile statement with a
block of code that opens the found file, searches its text for the
conditions you're looking for, and if that condition is true then extracts
the needed text. It has to know exactly how to determine the start and end
of the needed text. If it's really a Section in the sense of Word's object
model, that's easy; otherwise it might require multiple uses of the Find
method of a Range object.

Also, using the Selection object -- especially when there will be more than
one document open at a time -- is asking for disaster. Instead, create a
Document object for each document, and a Range object for the active
location in each document.

You should also avoid using copy and paste through the clipboard. When you
have a Range object named SourceRange pointing to the needed text in a found
document and another Range named DestinationRange in the summary document,
then you can duplicate the text in the summary with the simple statement

DestinationRange.FormattedText = SourceRange.FormattedText

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
R

ryguy7272

Interesting! Maybe, now that I think about it, I should use that first macro
to load everything into a large summary document (I the macro works fine for
that), and then search this summary document for certain characters, such as
the % character. I have a macro that highlights all defined characters, such
as the % character, but what I really need to do is highlight these and 2 or
3 places to the left, for instance to get 12% and 100%.

Sub Test4b()
Dim sArr() As String
Dim rTmp As Range
Dim x As Long
sArr = Split("%") ' your list
Options.DefaultHighlightColorIndex = wdYellow
For x = 0 To UBound(sArr)
Set rTmp = ActiveDocument.Range
With rTmp.Find
..Text = sArr(x)
..Replacement.Text = sArr(x)
..Replacement.Highlight = True
..Execute Replace:=wdReplaceAll
End With
Next
End Sub

Then I can copy/paste all hilighted words, characters, etc. to a new summary
document -- this should contain all items of interest. So I guess my
question now is, how do I find all % symbols as well as 2 or 3 characters to
the left and all words, such as Jurisdiction, and then one word to the right
or left of that key word? Is this possible with several macros?


Regards,
Ryan---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top