从VBA中的函数返回对象实例时出错 [英] Error returning object instance from function in VBA

查看:662
本文介绍了从VBA中的函数返回对象实例时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实例化一个对象并从函数中返回它.我正在使用的课程是我创建的课程.但是,当我尝试将Object设置为从函数返回的内容时,出现错误.我在做什么错了?

I'm trying to instantiate an object and return it from a function. The class I'm working with is one that I've created. However when I try to set an Object to what was returned from the function I get an error. What am I doing wrong?

Function CreateBlah(NAME As String, Count As Integer, val As String) As Blah
    Dim b As Blah
    Set b = New Blah
    bkmrk.Initialize NAME, Count, val
    MsgBox (bkmrk.NAME)
    CreateBlah = bkmrk
End Function

然后在其他功能中...

Then in the other function...

Dim bmrk As Blah
Set bmrk = CreateBlah("Test", 1, Trim(AString))

我也尝试过...

Dim bmrk As Object
Set bmrk = CreateBlah("Test", 1, Trim(AString))

我是VBA的新手,有人可以告诉我我在做什么错吗?

I'm new to VBA, can anyone tell me what I'm doing wrong?

推荐答案

每次分配对象时,都需要使用set 每次.这意味着在设置返回值以及将返回值分配给变量时. 后期绑定示例:

You need to use set every time you assign an object. This means when setting return value and when assigning the return value to a variable. Late Bound Example:

Public Sub Example()
    Dim objWrd As Object
    Set objWrd = GetWord
    objWrd.Visible = True
    objWrd.Quit
End Sub

Public Function GetWord() As Object
    Set GetWord = CreateObject("Word.Application")
End Function

早期绑定示例:

Public Sub Example()
    ''//Requires reference to Microsoft Office Word
    ''//(Tools>References)
    Dim objWrd As Word.Application
    Set objWrd = GetWord
    objWrd.Visible = True
    objWrd.Quit
End Sub

Public Function GetWord() As Word.Application
    Set GetWord = New Word.Application
End Function

这篇关于从VBA中的函数返回对象实例时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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