I use some Outlook code and Word code in my add-in. However sometimes I forget to turn those resources on in the VBA for some of my users. Is there a way to turn them on with code?
I use some Outlook code and Word code in my add-in. However sometimes I forget to turn those resources on in the VBA for some of my users. Is there a way to turn them on with code?
Turn on? What do you mean?
Sorry, I meant the References in VBA under tools on the VBA toolbar
I have to turn on the Outlook reference library and the Word Reference Library.
Once you have done it in a workbook, it remains. You don't need to do it again.
I know that but sometimes when I install it on someone else's machine for the first time I forget to turn it on then when they try to use some of those features they get errors.
So use late binding, then you can forgte about it.
So instaed of
[vba]
Dim oOutlook As Outlook.Application
Dim oNameSpace As Outlook.NameSpace
Dim oMailItem As Outlook.MailItem
Dim oRecipient As Outlook.Recipient
Set oOutlook = New Outlook.Application
Set oNameSpace = oOutlook.GetNamespace("MAPI")
oNameSpace.Logon , , True
Set objMailItem = oOutlook.CreateItem(olMailItem)
[/vba]
you would use
[vba]
Dim oOutlook As Object
Dim oMailItem As Object
Dim oRecipient As Object
Dim oNameSpace As Object
Set oOutlook = CreateObject("Outlook.Application")
Set oNameSpace = oOutlook.GetNameSpace("MAPI")
oNameSpace.Logon , , True
Set oMailItem = oOutlook.CreateItem(0)
[/vba]
xld,
what is the difference between early binding and late binding? Here is some code I am using for word:
[VBA]Documents.Open ("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")[/VBA]
Here is some code I am using for Outlook:
[VBA]Sub eMailActiveWorkbook()
Dim OL As Object
Dim EmailItem As Object
Dim WB As Workbook
TurnOffFeatures
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set WB = ActiveWorkbook
WB.Save
With EmailItem
.Attachments.Add WB.FullName
.Display
End With
TurnOnFeatures
Set WB = Nothing
Set OL = Nothing
Set EmailItem = Nothing
End Sub[/VBA]
Early binding is just binding to the type library at compile time, late binding is at run time.
Early binding is more efficient as VBA retrieves information from the library up-front, and uses that throughout, whereas late binding has to go through the registry, find the library, look up the details, every time a reference is made to an object within that library. But early binding ties you to a praticular version, late binding is more flexible.
With late binding, you cannot use that libraries constants, such as olMailItem, as you will get a compile error if you do. You could declare them as constants in your code, but otherwise you have to use the constant value.
Last edited by Bob Phillips; 03-13-2007 at 08:28 AM.
How do I change this to late binding?
[VBA]Dim OL As Object
Dim EmailItem As Object
Dim WB As Workbook
TurnOffFeatures
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set WB = ActiveWorkbook
WB.Save
With EmailItem
.Attachments.Add WB.FullName
.Display
End With
TurnOnFeatures
Set WB = Nothing
Set OL = Nothing
Set EmailItem = Nothing[/VBA]
xld,
can you fix it so your post is horizontal not vertical?
It's VBAX, not me, but I will try again
[vba] Dim OL As Object
Dim EmailItem As Object
Dim WB As Workbook
TurnOffFeatures
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(0)
Set WB = ActiveWorkbook
WB.Save
With EmailItem
.Attachments.Add WB.FullName
.Display
End With
TurnOnFeatures
Set WB = Nothing
Set OL = Nothing
Set EmailItem = Nothing
[/vba]
Fixed, MD
It's no good.
Now it is telling me object variable or with block not set when I try to run the code after I turn off the Outlook references.
Where?
it is weird because it is giving me that error message where I show the form that I created for email options.
[VBA]emailform.show[/VBA]
I don't see that line in the code.
that is because that code is called from the form
go to your UF codeOriginally Posted by Djblois
Ensure you have Option explicit set.
Recompile your code.... see where it highlights your error.
It would appear ypou may still have a early bound reference set.
Kind Regards,
Ivan F Moala From the City of Sails
Thank you all, I got that to work. Now how do I change this to late binding:
[VBA]Documents.Open ("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")[/VBA]
That code is neither late nor early.