|  | 
| 
			 
			#1  
			 
			
			
			
			
		 | |||
| 
 | |||
|  Word VBA Macro to Find and Replace based on the Alt Text of an Image 
			
			I'm looking for a Macro that will use a 'Find and replace' function but for the "alt Text" of an image. Basically, I'd like to Find and image within a document based on its Alt Text Delete the image Insert a new image Give the new Image its own Alt Text I've looked everywhere and I can't find anything on this, can anyone help me? I don't need any dialog boxes for this, I'd just like to enter the search and replacement criteria inside the code if possible. Thanks in advance  PS: I already have code that will go into the header of my document, insert an image then enter it's alt text value. It's the 'Find' function I'm struggling with. Code: Sub ReplaceImage()
'
' ReplaceImage Macro
'
'
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
 Selection.InlineShapes.AddPicture FileName:= _
    "IMAGE_LOCATION", LinkToFile:=False, _
    SaveWithDocument:=True
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.InlineShapes(1).AlternativeText = "ALT_TEXT_VALUE"
End SubCode: Function getPictureByAltText(altText As String) As InlineShape
    Dim shape As Variant
    For Each shape In ThisDocument.InlineShapes
        If shape.AlternativeText = altText Then
            getPictureByAltText = shape
            Exit Function
        End If
    Next
End Function
Sub test()
Dim myPic as InlineShape
Set myPic = getPictureByAltText("AltText to be searched")
myPic.Delete
End Sub | 
| 
			 
			#2  
			 
			
			
			
			
		 | ||||
| 
 | ||||
|   
			
			Try: Code: Sub Demo()
Dim iShp As InlineShape, strTxt As String, Rng As Range, sWd As Single, sHi As Single
strTxt = "Alternative Text"
With ActiveDocument
  For Each iShp In .InlineShapes
    With iShp
      If .AlternativeText = strTxt Then
        sWd = .Width
        sHi = .Height
        Set Rng = .Range
        .Delete
        Exit For
      End If
    End With
  Next
  Set iShp = .InlineShapes.AddPicture(FileName:="IMAGE_LOCATION", _
    LinktoFile:=False, SaveWithDocument:=True, Range:=Rng)
  With iShp
    .AlternativeText = strTxt
    .Width = sWd
    .Height = sHi
  End With
End With
Set iShp = Nothing: Set Rng = Nothing
End Sub
				__________________ Cheers, Paul Edstein [Fmr MS MVP - Word] | 
|   | 
| Tags | 
| vba, word 2013 | 
|  | 
|  Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post | 
|  Macro to find text and replace with form field containing that text | iiiiifffff | Word VBA | 16 | 06-04-2016 01:47 AM | 
|  Is the following too complex for find/replace macro? | bertietheblue | Word VBA | 12 | 11-04-2013 05:35 PM | 
|  Find and Replace Format macro issue | Jack | Word VBA | 2 | 12-12-2012 09:24 PM | 
| macro or find/replace | JamesVenhaus | Word | 2 | 02-27-2012 03:34 PM | 
| Find and Replace Macro - A Better Way | Tribos | Word VBA | 0 | 10-08-2008 03:22 AM |