'用户定义类型未定义'错误 [英] 'User Defined Type Not Defined' error

查看:155
本文介绍了'用户定义类型未定义'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试执行此宏时,我收到上述错误。我非常喜欢宏和编码,所以请原谅无知。

I'm getting the above error when trying to execute this macros. I'm pretty new to Macros and coding in general so please forgive the ignorance.

谢谢

Sub DeleteEmptyRows()

Dim oTable As Table, oRow As Row, _
TextInRow As Boolean, i As Long

Application.ScreenUpdating = False

For Each oTable In ActiveDocument.Tables
    For Each oRow In oTable.Rows

        TextInRow = False

        For i = 2 To oRow.Cells.Count
            If Len(oRow.Cells(i).Range.Text) > 2 Then
                'end of cell marker is actually 2 characters
                TextInRow = True
                Exit For
            End If
        Next

        If TextInRow = False Then
            oRow.Delete
        End If
    Next
Next
Application.ScreenUpdating = True

End Sub


推荐答案

您的错误是由以下原因引起的:

Your error is caused by these:

Dim oTable As Table, oRow As Row,

这些类型,不是Excel原生的变体类型。您可以通过以下两种方法之一解决此问题:

These types, Table and Row are not variable types native to Excel. You can resolve this in one of two ways:


  1. 包含对Microsoft Word对象模型的引用。从工具|中执行此操作引用,然后添加对MS Word的引用。虽然不是绝对必要的,您可以喜欢将 Dim oTable之类的对象完全限定为Word.Table,例如Word.Row 。这被称为早期绑定。

  2. 或者,为了使用后期绑定方法,您必须将对象声明为通用 Object type:将对象视为对象,以对象为对象。使用这种方法,您不需要添加对Word的引用,但您也会失去VBE中的智能帮助。

  1. Include a reference to the Microsoft Word object model. Do this from Tools | References, then add reference to MS Word. While not strictly necessary, you may like to fully qualify the objects like Dim oTable as Word.Table, oRow as Word.Row. This is called early-binding.
  2. Alternatively, to use late-binding method, you must declare the objects as generic Object type: Dim oTable as Object, oRow as Object. With this method, you do not need to add the reference to Word, but you also lose the intellisense assistance in the VBE.

I没有测试过您的代码,但我怀疑 ActiveDocument 将无法在Excel中使用方法#2,除非正确将其适用于Word.Application对象的实例。我没有看到你提供的代码中的任何地方。一个例子就像:

I have not tested your code but I suspect ActiveDocument won't work in Excel with method #2, unless you properly scope it to an instance of a Word.Application object. I don't see that anywhere in the code you have provided. An example would be like:

Sub DeleteEmptyRows()
Dim wdApp as Object
Dim oTable As Object, As Object, _
TextInRow As Boolean, i As Long

Set wdApp = GetObject(,"Word.Application")

Application.ScreenUpdating = False

For Each oTable In wdApp.ActiveDocument.Tables

这篇关于'用户定义类型未定义'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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