获取Word书签索引以从Excel替换书签中的图像 [英] Get Word bookmark index to replace image inside bookmark from Excel

查看:71
本文介绍了获取Word书签索引以从Excel替换书签中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与上一个有关.>

我有一个打开的Word文档,其中包含一堆书签,每个书签都包含以前从Excel导出的Excel表的内联图像.

现在,我需要更新Word文档中的表格,因为它们在Excel中已更改.

我这样做的方式是将Excel中的表名与Word中的书签名进行匹配.如果它们相等,则我想用当前图像替换Word中的现有图像.

到目前为止,这是我的代码:

 选项显式替代sub()暗淡标记为字符串昏暗范围昏暗ShpRng作为范围昏暗的WordApp作为对象暗淡的DocumentoDestino作为对象Dim folha作为工作表Dim tabela作为ListObject昏暗的NomeTabela作为字符串设置WordApp = GetObject(class:="Word.Application")设置DocumentoDestino = WordApp.ActiveDocument对于ThisWorkbook.Worksheets中的每个笑话如果folha.Visible然后'循环所有excel表格对于folha.ListObjects中的每个表tabela.Name =替换(tabela.Name,",")标记= CStr(tabela.Name)使用ActiveDocument如果.Bookmarks.Exists(Mark)然后Set Rng = .Bookmarks(Mark).Range'返回运行时错误13:类型不匹配,我想这是因为.Bookmarks需要书签索引而不是名称.如果Rng.InlineShapes.Count然后设置ShpRng = Rng.InlineShapes(1).Range有ShpRngDebug.Print .Start,.EndShpRng.Delete结束于万一万一结束于下一张Tabela万一下一个傻瓜结束子 

代码似乎正常,除了上面标记的返回运行时错误13的行之外,是否有任何方法可以获取书签索引而不是名称,还是可以使用其他方法来解决此问题?

提前谢谢!

解决方案

问题出在 Range 对象上.Excel和Word中都存在这样的对象.由于您正在运行Excel,因此Rng和ShpRng都隐式声明为Excel范围.将它们声明为 Word.Range .

通常,使用变量时要格外小心.您完美地声明了 Set DocumentoDestino = WordApp.ActiveDocument ,但随后继续进行

 使用ActiveDocument如果.Bookmarks.Exists(Mark)然后 

在Excel中,没有 ActiveDocument .也许这就是为什么Excel正确地表明您打算引用 DocumentoDestino 的原因.但是,如果不严格控制,很可能会出现控制实例-当然,每当您最不希望它们出现时-Excel就会做出错误的猜测.

This question is related with a previous one.

I have an open Word document with a bunch of bookmarks, each with an inline image of an Excel table previously exported from Excel.

Now, I need to update the tables in the Word document as they have changed in Excel.

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:

Option Explicit

Sub substituir()
Dim Mark As String
Dim Rng As Range
Dim ShpRng As Range
Dim WordApp As Object
Dim DocumentoDestino As Object
Dim folha As Worksheet
Dim tabela As ListObject
Dim nomeTabela As String

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, " ", "")
        Mark = CStr(tabela.Name)

        With ActiveDocument
        If .Bookmarks.Exists(Mark) Then

            Set Rng = .Bookmarks(Mark).Range ' returns runtime error 13: Type mismatch, I guess it is because .Bookmarks expects the bookmark index instead of the name.


            If Rng.InlineShapes.Count Then
                Set ShpRng = Rng.InlineShapes(1).Range
                With ShpRng
                     Debug.Print .Start, .End
                    ShpRng.Delete

                End With
            End If
        End If
        End With

     Next tabela

    End If
Next folha

End Sub

The code seems ok, except for the line marked above that returns runtime error 13, is there any way to get to the bookmark index instead of the name or another way to fix the issue?

Thanks in advance!

解决方案

The problem is from the Range object. There is such an object in Excel as well as in Word. Since you are running Excel, both Rng and ShpRng are declared as Excel ranges implicitly. Declare them as Word.Range.

Quite generally, be more careful with your use of variables. You perfectly declared Set DocumentoDestino = WordApp.ActiveDocument, but then you proceed with

With ActiveDocument
If .Bookmarks.Exists(Mark) Then

In Excel, there is no ActiveDocument. Perhaps that is why Excel correctly divines your intention to refer to DocumentoDestino. However, if you don't keep tight control instances are likely to arise - whenever you least expect them, of course - when Excel makes the wrong guess.

这篇关于获取Word书签索引以从Excel替换书签中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆