【问题标题】:Word VBA Macro to Find and Replace based on the Alt Text of an ImageWord VBA 宏基于图像的替代文本进行查找和替换
【发布时间】:2014-07-11 17:04:08
【问题描述】:

我正在寻找一个将使用“查找和替换”功能但用于图像的“替代文本”的宏。

基本上,我想

  • 根据替代文本在文档中查找和图像
  • 删除图片
  • 插入新图片
  • 为新图像提供自己的替代文本

我到处找了,我找不到任何东西,有人可以帮我吗?我不需要任何对话框,如果可能的话,我只想在代码中输入搜索和替换条件。

提前致谢:)

PS:我已经有将进入我的文档标题的代码,插入图像然后输入它的替代文本值。这是我正在努力解决的“查找”功能。

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

【问题讨论】:

  • 它不适用于find &gt;&gt; replace word 功能。您需要有一个宏来检查文档中的每个对象。您是在谈论您想要处理的仅InslineShapes(嵌入形状)还是Shapes(浮动形状)或两者?
  • 谢谢,我说的是InlineShapes
  • 我认为@Manu 的答案非常接近您的需要。也可以在答案下方查看我的 cmets。

标签: image vba replace ms-word


【解决方案1】:

要通过 alt 文本搜索图片,您可以使用如下函数:

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

并像这样使用它:

Dim myPic as InlineShape
Set myPic = getPictureByAltText("YourAltText")
myPic.Delete

【讨论】:

  • 嗨 Manu,谢谢你,我得到“运行时错误 '91':对象变量或未设置块变量”,调试将我带到“myPic.Delete”行有什么想法吗?
  • 这行可能缺少一个set instructionset getPictureByAltText = shape
  • 顺便说一句,+1 使用function 的非常好的解决方案。
  • @KazJaw activedocument.inlineshapes.count 返回 1 :((是的,文件中只有一张图片 :P)
  • 难以置信。这一直是问题所在,我不敢相信我自己没有尝试过。非常感谢你们都是绝对的传奇!!!!
【解决方案2】:

所以在ManuKazJaw 的帮助下,我们找到了问题的根源。

这是我自己添加的正确且有效的代码以消除错误:)

Function getPictureByAltText(altText As String) As InlineShape
Dim shape As Variant

For Each shape In ActiveDocument.InlineShapes
    If shape.AlternativeText = altText Then
        Set getPictureByAltText = shape
        Exit Function
    End If
Next
End Function

然后:

Sub DeletePicByAltText()

Dim myPic As InlineShape
Set myPic = getPictureByAltText("ENTER ALT TEXT YOU ARE SEARCHING FOR HERE")
If myPic Is Nothing Then
    MsgBox "Your Picture was not found. Check the 'Alt Text' is correct and try again."
    End If
On Error Resume Next
myPic.Delete

End Sub

再次感谢您的帮助!

【讨论】:

    【解决方案3】:

    在使用了一段时间后,我让它变得更简单了

    Dim pic as InlineShape
    Dim alt as String
    
    Alt = "ENTER ALT TEXT HERE"
    
    For Each pic in activedocument.inlineshapes
        If pic.AlternativeText = Alt Then
            pic.Delete
    Exit For
        End If
    Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 2014-10-12
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      相关资源
      最近更新 更多