简体   繁体   English

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

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

I'm trying to call –unsuccessfully– a set-type function in Excel, from an Outlook Sub.我试图从 Outlook 子中调用 Excel 中的集合类型 function,但未成功。

So, I have the following type in Excel VBA:所以,我在 Excel VBA 中有以下类型:

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

As well, I'm using the following function to fill it in:同样,我正在使用以下 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

Then, I have the following testing code:然后,我有以下测试代码:

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

That would give me successful results (it's just data from a worksheet):这会给我带来成功的结果(它只是工作表中的数据):

02.9 Data Cleanup
 10 
False

However, I'm not being able to call the function from Outlook VBA.但是,我无法从 Outlook VBA 调用 function。 I thought it was as simple as:我认为这很简单:

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

But I'm getting error «Only public user defined types defined in public object modules can be coerced to...».但我收到错误«只有在公共 object 模块中定义的公共用户定义类型才能被强制...»。 I tried as well to create a type-set in Outlook as well, but I haven't been able to make it run.我也尝试在 Outlook 中创建一个排版,但我无法让它运行。

I have a workaround, but it would be nice to know how I can trigger it this way.我有一个解决方法,但很高兴知道如何以这种方式触发它。

Edit 1: I'm able to call the function from Outlook to get back one value*.编辑 1:我可以从 Outlook 调用 function 以取回一个值*。 The issue I'm having is when I'm trying to get back a set of parameters in one run.我遇到的问题是当我试图在一次运行中取回一组参数时。

It seems you just need to include the workbook name:看来您只需要包含工作簿名称:

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

Then according to the error message you need to define a public function:然后根据报错信息需要定义一个公共的function:

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

See How do I use Application.Run in Excel for the sample code.有关示例代码,请参阅如何在 Excel 中使用 Application.Run

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

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