|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Need to find/replace text in many word files - but text is in embedded word files
Hi all, I'm having an issue where my predecessor made a ton of Word files, and instead of copying/pasting paragraphs like a sane person, he embedded Word documents into Word documents. I now need to add a sentence to each and every one of these documents. I intended to use a find/replace macro to search each file in the folder, but I'm not sure how to modify it to search through each embedded Word object within each Word document. Here's the VBA macro I had found and planned to use before realizing text was in embedded objects within the documents: http://www.extendoffice.com/document...ple-files.html |
#2
|
||||
|
||||
If the embedded content is linked to another file (as a reasonable person might do if the content needs to be kept in synch), all you need do is edit the source files.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Unfortunately, that isn't the case--this guy wasn't very computer savvy. To the best of my knowledge, this means I need to go through and open each file in the folder, open each embedded document in each file, and do a find/replace.
|
#4
|
|||
|
|||
This may give you a start. I do not know that it will reach the embedded documents.
How to Find & ReplaceAll on a batch of documents in the same folder |
#5
|
|||
|
|||
While you are at it, you might try changing the embedded files to links or straight text.
|
#6
|
|||
|
|||
I cobbled together a solution
Thanks for your help everyone; I ended up getting it figured out on my own. I probably should have mentioned that find and replace does not reach embedded documents without first opening them, and that many of the embedded files contain only pictures so changing them to straight text may not have been a good idea.
Likely not the best way to do it, but it works for my purposes: Code:
Sub Test() Dim numObjects As Integer Dim oDoc As Document Dim strFilename As String Const strPath As String = "C:\Test Folder\" 'Our folder to look at 'This makes a directory of files you want to look at (in the folder above, with the file extension specified) strFilename = Dir$(strPath & "*.doc*") 'Loops through and checks every applicable file in the folder While Len(strFilename) <> 0 Set oDoc = Documents.Open(strPath & strFilename) 'Makes a Document object for the current file oDoc.ActiveWindow.Visible = False numObjects = oDoc.InlineShapes.Count 'Count the number of InlineShapes (e.g. OLEs) If numObjects > 0 Then 'If there are any shapes (e.g. embedded objects), interact with them For Num = 1 To numObjects 'Iterate through each inline shape via index numbers If oDoc.InlineShapes(Num).Type = 1 Then 'If it's an embedded object (InlineShapes().Type = 1 indicates an OLE) oDoc.InlineShapes(Num).OLEFormat.Open 'Open up the embedded object ActiveDocument.ActiveWindow.Visible = False Call FAR 'Find And Replace subroutine ActiveDocument.Close 'Close the embedded object and continue to the next. Not sure how _ '_ to reference it besides "ActiveDocument" tbh. End If Next Num End If Debug.Print oDoc.FullName 'Error handling. 'Closes the Document object and saves changes oDoc.Close wdSaveChanges 'Clears it for the next iteration of the loop Set oDoc = Nothing strFilename = Dir$() Wend End Sub Sub FAR() 'Find And Replace ActiveDocument.ActiveWindow.Visible = False With Selection.Find 'Throughout the selection (the whole document).. .Text = "beep" .Replacement.Text = "boop" .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With End Sub |
Tags |
embedded word document, macro find and replace |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Find & replace footer text in a folder of Word 2010 documents | kennethc | Word | 3 | 03-28-2015 02:49 AM |
Word VBA Find Table Text Shading Colour and replace with another | QA_Compliance_Advisor | Word VBA | 10 | 09-19-2014 08:36 AM |
Word VBA Macro to Find and Replace based on the Alt Text of an Image | bennymc | Word VBA | 1 | 01-27-2014 04:23 PM |
Bulk Text alignment for multiple word files | jbradf | Word | 0 | 08-29-2013 06:41 PM |
Merging two Word files with one file having a text box | Timothy2001 | Word | 3 | 01-23-2011 07:01 PM |