【问题标题】:Excel VBA Cannot return from functionExcel VBA无法从函数返回
【发布时间】:2022-06-20 20:37:26
【问题描述】:

这是我的代码:

Function GetAttachmentById(Id As String) As Attachment
    Dim newAttachment As Attachment
    
    Set newAttachment = New Attachment
    
    Dim Directory As String
    
    Directory = "C:\Users\user\Desktop\VBA"
    
    Dim fso, newFile, folder, Files
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set folder = fso.GetFolder(Directory)
    Set Files = folder.Files
    
    For Each file In Files
    
        If InStr(file.Name, Id) > 0 Then
            newAttachment.Id = Id
            newAttachment.AttachmentName = file.Name
            newAttachment.AttachmentPath = file.Path
        End If
        
    Next file
    
    GetAttachmentById = newAttachment

在我尝试返回附件对象的最后一行出现错误:

对象变量或未设置块变量

正如我在调试器中看到的,newAttachment 创建得很好,而且我没有“with”块,所以我不确定下一步该去哪里。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您在最后一行代码中缺少 Set 语句。

    设置 GetAttachmentById = newAttachment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      相关资源
      最近更新 更多