View Single Post
 
Old 01-27-2014, 09:35 AM
bennymc bennymc is offline Windows 7 32bit Office 2013
Novice
 
Join Date: Jan 2014
Posts: 1
bennymc is on a distinguished road
Default 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 Sub
So far I have this, But I get "Runtime Error '91': Object variable or With block variable not set" And Debug takes me to the line "myPic.Delete"

Code:
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
Reply With Quote