我有一个带有一堆书签的打开的 Word 文档,每个书签都有一个以前从 Excel 导出的 Excel 表格的内联图像。
现在,我需要更新 Word 文档中的表格,因为它们在 Excel 中已更改。
我这样做的方式是将 Excel 中的表名称与 Word 中的书签名称相匹配。如果它们相等,我想用当前图像替换 Word 中的现有图像。
到目前为止,这是我的代码:
Sub substituir()
Set WordApp = GetObject(class:="Word.Application")
Set DocumentoDestino = WordApp.ActiveDocument
For Each folha In ThisWorkbook.Worksheets
If folha.Visible Then
'loop all excel tables
For Each tabela In folha.ListObjects
tabela.Name = Replace(tabela.Name, " ", "")
nomeTabela = tabela.Name
For Each myBookmark In DocumentoDestino.Bookmarks
If Right(myBookmark.Name, 4) = "PGST" Then
'This is where I need help
If myBookmark.Name = nomeTabela Then
'code to clear the table already in myBookmark here
'then copy and paste tables in myBookmark
tabela.Range.Copy
myBookmark.Range.PasteSpecial link:=False, DataType:=wdPasteMetafilePicture, _
Placement:=wdInLine, DisplayAsIcon:=False
End If
End If
Next myBookmark
Next tabela
End If
Next folha
End Sub
我尝试了许多不同的方法,从删除书签并将其重新添加回其他人,但似乎没有任何效果。
在评论中:
'code to clear the table already in myBookmark here
我需要帮助。
最佳答案
在以下代码中,我尝试包含您的项目可能需要的语法。
Private Sub TestMark()
Dim Mark As String
Dim Rng As Range
Dim ShpRng As Range
Mark = "Text1"
With ActiveDocument
If .Bookmarks.Exists(Mark) Then
Set Rng = .Bookmarks(Mark).Range
If Rng.InlineShapes.Count Then
Set ShpRng = Rng.InlineShapes(1).Range
With ShpRng
Debug.Print .Start, .End
End With
End If
End If
End With
End Sub
当然,一旦您知道范围的开始和结束,您就可以对其进行操作,即删除和替换它。
我突然想到,您可能会使用 InlineShape 的 Caption 属性来查找和解决它。
关于vba - 从 Excel 替换 Word 书签内的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43607237/