繁体   English   中英

Call Excel VBA function with set-type from Outlook VBA Sub

[英]Call Excel VBA function with set-type from Outlook VBA Sub

我试图从 Outlook 子中调用 Excel 中的集合类型 function,但未成功。

所以,我在 Excel VBA 中有以下类型:

Type Settings
    Value As String
    Stop As Boolean
    ID As Long
End Type

同样,我正在使用以下 function 来填写:

Function Projects_List_1(Var_Num As Long) As Settings

Dim c As Long, Sets As Settings
Set Rng5 = CDU.Range("CDU_Projects")

For c = 1 To Rng5.Count
    If Rng5.Cells(c, 1).Offset(0, 1).Value = "" Then
        If Var_Num = c Then
            Sets.Value = Rng5.Cells(c, 1).Value
            If Var_Num > Rng5.Count Then
                Sets.Stop = True
                Exit Function
            Else
                Sets.ID = Var_Num + 1
                Sets.Stop = False
                Exit For
            End If
        End If
    End If
Next c
Projects_List_1 = Sets

End Function

然后,我有以下测试代码:

Sub Test_Excel()

Dim mysets As Settings, aaa As Long

aaa = 9
mysets = Projects_List_1(aaa)

Debug.Print mysets.Value
Debug.Print mysets.ID
Debug.Print mysets.Stop

End Sub

这会给我带来成功的结果(它只是工作表中的数据):

02.9 Data Cleanup
 10 
False

但是,我无法从 Outlook VBA 调用 function。 我认为这很简单:

Sub Test_Outlook()

'Here I'm just creating the conextion to my workbook. There are no issues here.
Dim ExApp As Excel.Application, ExWbk As Workbook, WB As Workbook
Dim mysets As Settings, aaa As Long
Set ExApp = GetObject(, "Excel.Application")
For Each WB In ExApp.Workbooks
    If WB.Name = "3 Control de Requerimientos Main.xlsm" Then
        Set ExWbk = WB
    End If
Next WB

aaa = 9
mysets = ExWbk.Application.Run("Projects_List_1", aaa)

Debug.Print mysets.Value
Debug.Print mysets.ID
Debug.Print mysets.Stop

End Sub

但我收到错误«只有在公共 object 模块中定义的公共用户定义类型才能被强制...»。 我也尝试在 Outlook 中创建一个排版,但我无法让它运行。

我有一个解决方法,但很高兴知道如何以这种方式触发它。

编辑 1:我可以从 Outlook 调用 function 以取回一个值*。 我遇到的问题是当我试图在一次运行中取回一组参数时。

看来您只需要包含工作簿名称:

Application.Run "'My Work Book.xls'!Macro1"

然后根据报错信息需要定义一个公共的function:

Public Sub Macro1(Var_Num As Long)
' your code goes here
End Sub

有关示例代码,请参阅如何在 Excel 中使用 Application.Run

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM