从Excel中替换Word书签中的图像 [英] Replace image inside Word bookmark from Excel
问题描述
我有一个开放的Word文档,其中包含一堆书签,每个都有Excel的Excel表格的内联图像。
I have an open Word document with a bunch of bookmarks, each with an inline image of an Excel table previously exported from Excel.
现在,我需要更新Word文档中的表格在Excel中已更改。
Now, I need to update the tables in the Word document as they have changed in Excel.
我正在做的是将Excel中的表名与Word中的书签名称进行匹配。如果他们等于我想用Word替换现有的图像。
The way I'm doing this is matching the table names in Excel with the bookmark names in Word. If they are equal than I want to replace the existing images in Word by the current ones.
这是我的代码到目前为止:
This is my code so far:
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
我尝试了许多不同的方法,从删除书签并将其重新添加到其他人,但似乎没有任何效果。
I've tried lots of different approaches, from deleting the bookmark and adding it back again to others, but nothing seems to work.
在评论中:'代码清除myBookmark中的表已经在这里
我需要帮助。
In the comment: 'code to clear the table already in myBookmark here
I need help.
推荐答案
在下面的代码中,我试图添加您可能需要的项目语法。
In the following code, I have tried to include the syntax you might require for your project.
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
当然,一旦你知道范围的开始和结束,你可以操纵它,意思是删除并替换它。
Of course once you know the Start and End of the range you can manipulate it, meaning delete and replace it.
只是发生在我身上,你可以使用InlineShape'Caption属性来查找和解决它。
It just occurs to me that you might use the InlineShape' Caption property to find and address it.
这篇关于从Excel中替换Word书签中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!