0

Here is my code:

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

On the last line where I try to return my Attachment object I get the error:

Object variable or With block variable not set

As I can see in the debugger, the newAttachment is created well and I have no "with" block so I am not sure where to go next.

1 Answer 1

4

You are missing the Set statement in your last line of code.

Set GetAttachmentById = newAttachment
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.