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?
Printable View
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.
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 codeQuote:
Originally 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.
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.
then how do I get it to work with the reference turned off. I get an object required error
Probably because you have qualified it with the application object.
ok so how do I unqualify it?
I meant haven't not have.
I think Bob's saying you have to dim it as an object too...just as you did in this code for the email object:
[VBA]Dim OL As Object
Dim EmailItem As Object
Dim WB As Workbook
TurnOffFeatures
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(0)
[/VBA]
I was assuming he had already done that Steve, although I was thinking Word, and that he just needed to pre-pend Documents with that object variable.
so would this be what you mean
[VBA]dim Documents as object[/VBA]
Just trying to cover the bases Bob....I didn't see in his previous code where he had dealt with the Word object so I thought I would remind him of the concept...not meaning to step on toes.:whistle:
Nor did I Steve, he hasn't mentioned it specifically, hence my obtuseness borne of lack of information.
Reading between the lines in not my best suite. It seems they think we can look over thier shouders somtimes and see what they are doing.....
Several regular supporters here have signatures that refer to complete descriptions of what your trying to do but if you don't get the info you have to work in the dark.....time consuming and frustrating.
No, no!Quote:
Originally Posted by Djblois
Letr's assume, because you haven't told me, that you have started word from your code and have a Word object. Let us further assume that that object is called oWord (spooky stuff!).
IF these assumptions are correct, you probably want
oWord.Documents.Open (blah blah blah)
no I haven't started word yet but I guess I need to?
How do I start word programatically?
create object....just as you did with outlook..
You do if you are working with Word documnts. Are you sure that is what you need?Quote:
Originally Posted by Djblois
THis is what I have now and it is I think opening the file hidden:
[VBA]Sub Instructions()
Set WD = CreateObject("Word.Application")
TurnOffFeatures
On Error Resume Next
WD.Documents.Open ("H:\@Business_Reporting_Today\References\Business Reporting Today.doc")
TurnOnFeatures
End Sub[/VBA]
I think that because it doesn't show the first time I run it and if I run it again it says it is locked for editing by me.
Add
WD.Visible = True
after creating the Word instance.
that works perfectly but now I want to create it so it checks if it is open already and if it is already open then it will switch to it. I know how to do this with excel but not with word.
Same, just Word.Application not Excel.APplication.
PS when you go consulting I claim 10% of your income.