使用字符串/文本占位符替换Word文档中的图像

时间:2013-06-10 21:45:26

标签: image vba ms-word word-vba

我有一个带有一些图像的2010 Word文档。我已经逐一导出了图像。由于文档目的地的格式化,我需要删除所有图像并留下文本块(类似于[[Image1.jpg]])图像曾经存在。

图像的名称实际上并不重要。我只需要将图像换成文本。我对Word中的VBA完全不熟悉。

以下是其他一些调整图片大小的代码,可以修改这些代码以删除图片并在该位置插入一些文字吗?

Dim oShp As Shape
Dim oILShp As InlineShape

For Each oShp In ActiveDocument.Shapes
    With oShp
        .Height = AspectHt(.Width, .Height, _
        CentimetersToPoints(11))
        .Width = CentimetersToPoints(11)
    End With
Next

For Each oILShp In ActiveDocument.InlineShapes
    With oILShp
        .Height = AspectHt(.Width, .Height, _
        CentimetersToPoints(11))
        .Width = CentimetersToPoints(11)
    End With
Next
End Sub

1 个答案:

答案 0 :(得分:2)

两种可能只适用于InlineShapes的解决方案(根据可能没问题的评论。)

解决方案1 ​​常量替换文字:

Sub ReplaceInlineShapes_constant_names()

Dim oILShp As InlineShape

For Each oILShp In ActiveDocument.InlineShapes

    'insert text in place where InlineShape is located
    ActiveDocument.Range(oILShp.Range.Start, oILShp.Range.End).Text = "[Image.Jpg]"
    'delete picture is not needed- it was simply replaced with text

Next
End Sub

解决方案2 带索引的替换文字:

Sub ReplaceInlineShapes_WithIndex()

Dim oILShp As InlineShape
Dim ILShpIndex As Integer
For Each oILShp In ActiveDocument.InlineShapes
    ILShpIndex = ILShpIndex + 1
    'insert text in place where InlineShape is located
    ActiveDocument.Range(oILShp.Range.Start, oILShp.Range.End).Text = _
                                "[Image" & ILShpIndex & ".Jpg]"
    'delete picture is not needed- it was simply replaced with text

Next
End Sub