PDA

View Full Version : Batch delete image in table from Word files



pjs15
10-26-2011, 11:56 AM
New to the forum and to VBA. Using Word 2010 running on Windows XP.

I have in one directory several hundred Word files, each of which contains text plus a single image embedded in a table. Would it be possible to create a macro that can either delete the table and embedded image or convert the table to text and delete the image, for all of the files?

Many thanks

gmaxey
10-26-2011, 08:15 PM
Something like this:

Option Explicit
Sub ProcessBatch()
Const pathToUse = "C:\"
Dim strFile As String
Dim oDocToProcess As Word.Document
WordBasic.DisableAutoMacros 1
On Error Resume Next
strFile = Dir$(pathToUse & "*.doc*")
While strFile <> ""
Set oDocToProcess = Documents.Open(pathToUse & strFile)
ScratchMacro oDocToProcess
oDocToProcess.Close SaveChanges:=wdSaveChanges
strFile = Dir$()
Wend
WordBasic.DisableAutoMacros 0
End Sub
Sub ScratchMacro(ByRef oDoc As Word.Document)
'A quick macro scratch pad created by Greg Maxey
Dim oTbl As Word.Table
Set oTbl = oDoc.Tables(1)
oTbl.Range.InlineShapes(1).Delete
oTbl.ConvertToText Separator:=wdSeparateByParagraphs
Set oDoc = Nothing
Set oTbl = Nothing
End Sub