Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-27-2014, 09:35 AM
bennymc bennymc is offline Word VBA Macro to Find and Replace based on the Alt Text of an Image Windows 7 32bit Word VBA Macro to Find and Replace based on the Alt Text of an Image Office 2013
Novice
Word VBA Macro to Find and Replace based on the Alt Text of an Image
 
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
  #2  
Old 01-27-2014, 04:23 PM
macropod's Avatar
macropod macropod is offline Word VBA Macro to Find and Replace based on the Alt Text of an Image Windows 7 32bit Word VBA Macro to Find and Replace based on the Alt Text of an Image Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

Tags
vba, word 2013

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word VBA Macro to Find and Replace based on the Alt Text of an Image Macro to find text and replace with form field containing that text iiiiifffff Word VBA 16 06-04-2016 01:47 AM
Word VBA Macro to Find and Replace based on the Alt Text of an Image Is the following too complex for find/replace macro? bertietheblue Word VBA 12 11-04-2013 05:35 PM
Word VBA Macro to Find and Replace based on the Alt Text of an Image 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:17 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft