Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从MS Excel循环浏览MS Word中的表格集合_Excel_Vba_Ms Word - Fatal编程技术网

从MS Excel循环浏览MS Word中的表格集合

从MS Excel循环浏览MS Word中的表格集合,excel,vba,ms-word,Excel,Vba,Ms Word,我正在尝试对MS Word文档中的所有表进行一些调整,我有以下代码: Dim oTbl As Table For Each oTbl In ActiveDocument.Tables With oTbl.Range.Next If .Information(wdWithInTable) = False Then .Delete End With Next 这通常在我从MS Word运行它时起作用,但当我尝试从MS Excel运行它时,我会遇到 类型13不匹配错误 有人知道为什

我正在尝试对MS Word文档中的所有表进行一些调整,我有以下代码:

Dim oTbl As Table

For Each oTbl In ActiveDocument.Tables
  With oTbl.Range.Next
    If .Information(wdWithInTable) = False Then .Delete
  End With
Next
这通常在我从MS Word运行它时起作用,但当我尝试从MS Excel运行它时,我会遇到

类型13不匹配错误

有人知道为什么吗?如何从Excel中实现这一点


谢谢

您需要从Excel中引用Word。这里有一种方法

Option Explicit

Public Sub ExampleDelete()
    const wdWithInTable as long = 12
    Dim wrd As Object, doc As Object, tbl As Object
    Set wrd = CreateObject("Word.Application")

    Set doc = wrd.documents.Open("C:\Users\Ryan\desktop\Example.docx") 'Update your path here

    For Each tbl In doc.Tables
        With tbl.Range.Next
            If .Information(wdWithInTable) = False Then .Delete
        End With
    Next

    'Do other actions here...maybe save?
End Sub

您需要从Excel中引用Word。这里有一种方法

Option Explicit

Public Sub ExampleDelete()
    const wdWithInTable as long = 12
    Dim wrd As Object, doc As Object, tbl As Object
    Set wrd = CreateObject("Word.Application")

    Set doc = wrd.documents.Open("C:\Users\Ryan\desktop\Example.docx") 'Update your path here

    For Each tbl In doc.Tables
        With tbl.Range.Next
            If .Information(wdWithInTable) = False Then .Delete
        End With
    Next

    'Do other actions here...maybe save?
End Sub

您在哪一行获得错误?•这就是您从Excel运行的全部内容吗?代码不能单独从Excel运行。请出示完整的相关代码部分。您是否设置了对Microsoft Word的引用,或者正在使用后期绑定<代码>wdWithInTable仅适用于参考集。另外,Excel在没有引用Word实例的情况下不知道
ActiveDocument
。我已经设置了引用,并且在本例中使用了早期绑定。代码的这一部分由另一个在多个场合调用的子部分分隔开。我曾尝试使用GetObject(,“Word.Application”)引用Word实例,但没有任何帮助。您在哪一行得到错误?•这就是您从Excel运行的全部内容吗?代码不能单独从Excel运行。请出示完整的相关代码部分。您是否设置了对Microsoft Word的引用,或者正在使用后期绑定<代码>wdWithInTable仅适用于参考集。另外,Excel在没有引用Word实例的情况下不知道
ActiveDocument
。我已经设置了引用,并且在本例中使用了早期绑定。代码的这一部分由另一个在多个场合调用的子部分分隔开。我曾尝试使用GetObject(,“Word.Application”)引用Word实例,但没有帮助。值得一提的是,如果Word文件已在Word实例中打开,请签出
Set wrd=GetObject(,“Word.Application”)
以获取已打开的Word实例。是的,如果经常引用同一个对象,也可以使用静态对象来加快获取引用的速度。如果您的代码帮助我解决了这个问题,还可以使用@P的解释ᴇʜ很有帮助。我没有引用MS Word应用程序实例。值得一提的是,如果Word文件已在Word实例中打开,请签出
Set wrd=GetObject(,“Word.application”)
以获取已打开的Word实例。是的,如果经常引用同一个对象,也可以使用静态对象来加快获取引用的速度。如果您的代码帮助我解决了这个问题,还可以使用@P的解释ᴇʜ很有帮助。我没有引用MS Word应用程序实例。