MS Word macro to delete a specific picture in the document?

musicgold

Board Regular
Joined
Jan 9, 2008
Messages
197
Hi,

Is there a way to find a specific image in a word document and delete it? I about 20 or so Word documents and there is an image in each of them with a title Figure 11. As shown below, while I am able to loop through all open documents and reach to Figure 11, I am not able to delete the image inside the table.

Thanks

1619720800632.png
 

Attachments

  • 1619720741744.png
    1619720741744.png
    19 KB · Views: 27

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Since you haven't posed any code - especially the code that locates the table - it's impossible to say what specific changes need to be made to that code.
 
Upvote 0
Since you haven't posed any code - especially the code that locates the table - it's impossible to say what specific changes need to be made to that code.

Here is the code. I want to be able to delete the picture in the table.

VBA Code:
 If doc.Name <> strName Then

    doc.Select
    
Selection.Find.Replacement.ClearFormatting

    With Selection.Find
        .Text = "Figure 11."
        .Replacement.Text = "Figure 11. Star Table*"
        .Forward = True
        .Wrap = wdFindContinue
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
   
  End If
 
Upvote 0
For excample:
VBA Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
Do While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      With .Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "Figure 11."
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchWildcards = True
        End With
        Do While .Find.Execute
          If .Information(wdWithInTable) = True Then
            .Cells(1).Range.Text = "Figure 11. Star Table*"
            .Start = .Cells(1).Range.End
          End If
          .Collapse wdCollapseEnd
        Loop
      End With
      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Loop
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
 
Upvote 0

Forum statistics

Threads
1,223,516
Messages
6,172,776
Members
452,478
Latest member
DigDug2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top