![]() |
|
#1
|
|||
|
|||
|
I'd like to do the following:
1. Find all paragraphs that contain occurences of text stored in the clipboard, for all word documents in a specific folder, then 2. Copy those paragraphs, and 3. Paste them one-by-one into the presently active document, starting at the current cursor position. (The order of the paste results is irrelevant. Neither does it have to be batch processed.) Could it be done? |
|
#2
|
||||
|
||||
|
If you copy and paste the texts, that will clear the original clipboard content so the short answer is no. However it would be possible to search a batch of documents for a particular text string and reproduce the paragraphs containing that text in the current document, without the need to copy and paste e.g.
Code:
Option Explicit
Sub Copy_Paras()
Dim strPath As String
Dim strFilename As String
Dim oTarget As Document
Dim oDoc As Document
Dim oRng As Range
Dim oRng2 As Range
Dim strText As String
Set oTarget = ActiveDocument
Set oRng2 = Selection.Range
strText = InputBox("Enter the text to find")
If strText = "" Then GoTo lbl_Exit
strPath = "C:\Path\" 'The folder with the documents
strFilename = Dir$(strPath & "*.docx")
While Len(strFilename) <> 0
Set oDoc = Documents.Open(strPath & strFilename)
Set oRng = oDoc.Range
With oRng.Find
Do While .Execute(FindText:=strText)
oRng2.Text = oRng.Paragraphs(1).Range.Text
Loop
End With
oDoc.Close SaveChanges:=0
strFilename = Dir$()
DoEvents
Wend
lbl_Exit:
Set oRng = Nothing
Set oRng2 = Nothing
Set oDoc = Nothing
Set oTarget = Nothing
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#3
|
|||
|
|||
|
Thank's for the help!
However, to further with this topic, 1. Could it be done without opening all the documents? I'd like just to have a working active document open, and nothing else popping up. 2. The result shown in the active document seems to display only the last occurence. Is it possible to display all the occurences in the active document, each separated by a carriage return or the like? |
|
#4
|
||||
|
||||
|
You cannot copy content from a document without opening it. That said, you could hide the documents being opened by changing:
Set oDoc = Documents.Open(strPath & strFilename) to: Set oDoc = Documents.Open(strPath & strFilename, False, True, False, , , , , , , , False) The reason for only the last document's content displaying is that the code needs to have something like: oRng2.InsertAfter vbCr oRng2.Collapse wdCollapseEnd inserted after: oRng2.Text = oRng.Paragraphs(1).Range.Text
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Copying tables between Word documents | FIN | Word | 1 | 02-10-2015 05:59 PM |
| Copying Selected Text Between Word Documents | Iluvatar343 | Word VBA | 1 | 06-05-2014 05:15 AM |
Find and Replace multiple lines/paragraphs
|
jcw | Word | 1 | 11-18-2011 11:47 AM |
MS Excel 2004 for Mac - copying dates between documents
|
BCRenton | Excel | 4 | 11-10-2009 07:28 AM |
| Copying tables between Word documents best practices? | dylane | Word Tables | 0 | 12-18-2008 12:21 PM |