Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use a function

Status
Not open for further replies.

jahlmer

Technical User
Aug 23, 2000
143
US
I hope this is a no-brainer. I've found some awesome code that will help me automate outlook, but don't know how in the heck to get it to run. The code is stright from MS, all I need to do is get the thing working. How do you execute a function or sub??? I've look in all the help files like a lost child!

'Dim these objects in module level declarations code
Public gobjOutlook as Outlook.Application
Public gobjNamespace as Outlook.Namespace

Function CreateOutlookInstance() As Boolean
'Create application and namespace root objects
Set gobjOutlook = CreateObject("Outlook.Application.8")
If Err Then
MsgBox "Could not create Outlook Application object!", vbCritical
CreateOutlookInstance = False
Exit Function
End If
Set gobjNameSpace = gobjOutlook.GetNamespace("MAPI")
If Err Then
MsgBox "Could not create MAPI Namespace!", vbCritical
CreateOutlookInstance = False
Exit Function
End If
CreateOutlookInstance = True
End Function

Thanks.
 
This function creates a new instance of the outlook applicatin. Nothing more.

You can use this function from an another sub or function like:

sub Anithing()
dim myVar as Boolean
'...
myVar = CreateOutlookInstance()
'...
end sub

But if you would like to automate anything you must write more code, it is just the start of coding. Additionally I don't know if you create a new instance of it makes your rules to run or not.
 
Ok, can you give me an example of how to impliment this code? Can I put it in an access 97 form and run it via a button?

Thanks for any help.
 
What do you wish to do with Outlook?
pls more info
 
Ok, here are my plans:

Using an access 97 form, I would like to click a button to send an email in outlook.

Additionally, I want to be able to search outlook folders for emails sent to, or received from the email address in the record selected.

This article has quite a few resources:


It gives me plenty of code, and things to start working with, but, since I am a novice with visual basic, and kind of with access as well, I don't know how to impliment the code, where to put it, and how to run these things..

Thanks for your help.
 
This subject is the closest I could find to my situation. I need to run a sub procedure from a macro. How do I get a macro to execute my sub procedure? Help indicates that it is possible but it doesn't indicate how. Please help.
 
Are you calling the Sub from another workbook? What problem are you having? ----------------------------------------
If you are reading this, then you have read too far...

lightwarrior@hotmail.com
 
Thank you for your responses (and sorry for cutting into your own question, ggarma.

I think my question was read in too deep. I do appreciate the link though, JoaoTL. Please allow me to rephrase.

I am a very big novice. My question was less about the code and more about what to do with it. I have some experience with Access and hoped to put this into a form that I can control with buttons etc. The function that I posted is one that I can't get to "run". I tried to put it into the event procedure (code builder) for a click for a button, it didn't work. I'm missing a piece of the pie about this, and have even read what is in the help files, but even they are to particular to the function, they don't answer the how.

Thanks for all the help, too.
 
To JUST instantiate the procedure:

Code:
Private Sub cmdOutLook_Click()

    Call CreateOutlookInstance

End Sub

This is (obviously?) the code attached to a command button on a form. The command button Name property is:

[cmdOutLook]

and the code is in the "On Click" event.

Please note that this REQUIRES a reference to the outlook or outlok express librar(y | ies). this can only be set (in Ms. Access from a "Code" window, under tools-references.

Which reference you use will depend on the software installed on your machine. Most individual users would have outlook express. Also, you may need to change the line:

Set gobjOutlook = CreateObject("Outlook.Application.9") to the version installed on your system (note I needed to change ".8" to ".9").

As has been previously noted, this doesn't -by itself- do anything useful. In fact, -by itself- it is probably harmful, as suscessive instantiations of the aplication may (PROBABLY) are not cleared from memory, so with little ado about it, you will run out of memory and "CRASH"!

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top