Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-19-2016, 12:28 AM
paik1002 paik1002 is offline Copying paragraphs from multiple documents to the current one. Windows 7 64bit Copying paragraphs from multiple documents to the current one. Office 2010 64bit
Advanced Beginner
Copying paragraphs from multiple documents to the current one.
 
Join Date: Dec 2015
Posts: 63
paik1002 is on a distinguished road
Default Copying paragraphs from multiple documents to the current one.

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?
Reply With Quote
  #2  
Old 02-19-2016, 05:40 AM
gmayor's Avatar
gmayor gmayor is offline Copying paragraphs from multiple documents to the current one. Windows 10 Copying paragraphs from multiple documents to the current one. Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 02-22-2016, 02:26 AM
paik1002 paik1002 is offline Copying paragraphs from multiple documents to the current one. Windows 7 64bit Copying paragraphs from multiple documents to the current one. Office 2010 64bit
Advanced Beginner
Copying paragraphs from multiple documents to the current one.
 
Join Date: Dec 2015
Posts: 63
paik1002 is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old 02-22-2016, 04:03 AM
macropod's Avatar
macropod macropod is offline Copying paragraphs from multiple documents to the current one. Windows 7 64bit Copying paragraphs from multiple documents to the current one. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply



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
Copying paragraphs from multiple documents to the current one. Find and Replace multiple lines/paragraphs jcw Word 1 11-18-2011 11:47 AM
Copying paragraphs from multiple documents to the current one. 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:25 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft