# Run Time -2147417856 (80010100) Error - Need help for a fix



## Stildawn (Aug 20, 2013)

Hi All (also asked here)

I have a complicated set of code that runs from Outlook, into excel and then in excel runs this code below:


```
Dim objOutl
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)
Application.ScreenUpdating = False
ans = MsgBox("Do you need to add any attachments?", vbYesNo)
If ans = vbYes Then
    AttachFileName = Application.GetOpenFilename("Files (*.**)," & _
     "*.**", 1, "Select File", "Open", True)
    For a = LBound(AttachFileName) To UBound(AttachFileName)
        FName = Dir(AttachFileName(a))
        FileCopy AttachFileName(a), "N:\Customs\Customs_Docs\" & job & "\" & FName
    Next a
Else
End If
strEmailAddr = [EMAIL="email.address@domain.com"]email.address@domain.com[/EMAIL]
objMailItem.Recipients.Add strEmailAddr
objMailItem.Body = ""
objMailItem.Subject = sbj & "-" & pri & "-" & job
Dim MyObj As Object, MySource As Object, file1 As Variant
file1 = Dir("N:\Customs\Customs_Docs\" & job & "\")
While (file1 <> "")
    objMailItem.Attachments.Add "N:\Customs\Customs_Docs\" & job & "\" & file1
    file1 = Dir
Wend
objMailItem.Display
Set objMailItem = Nothing
Set objOutl = Nothing
Application.ScreenUpdating = True
```

This code works, but every so often the users get the Run Time error mentioned in the thread title... I did some googling and I found this page on a similar error regarding to Word: You may receive the "Run-time error '-2147023174' (800706ba)" error message or the "Run-time error '462'" when you run Visual Basic code that uses Automation to control Word

It looks like it would relate to my error but replace word with outlook... It seems to say that a hidden global variable reference is created and that its not released between each runs of the code?

I tried to understand all that the link above said in relation to my code, but well I just don't understand it... Could one of you guru's fix my code above??

Thanks in advance


----------



## Stildawn (Aug 22, 2013)

Any ideas guys? This is a big bug in my whole system?


----------



## Derek Brown (Aug 23, 2013)

Perhaps you need to set a reference to Outlook before declaring your variables.
Something like:

```
' Under Tools | References, select:
' Microsoft Outlook 14.0 Object Library
'
' for help with Outlook code, also see http://www.outlookcode.com/
'
Dim objOutl As Outlook.Application
Dim objMailItem As Outlook.MailItem
```


----------



## Stildawn (Aug 25, 2013)

It can't hurt I'll try add them in

The reference library is already selected though.


----------



## Derek Brown (Aug 26, 2013)

Also see the techniques described in Creating an Object Variable to Automate Another Office Application


----------



## pmontanes (Jan 31, 2014)

Derek Brown said:


> Also see the techniques described in Creating an Object Variable to Automate Another Office Application



Hi Derek, I have the exact same problem, I also have the reference selected, but the error continues. Any ideas? This is really driving me crazy. thanks!


----------

