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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

outlook automation using 100% of cpu

Status
Not open for further replies.

samsnead

Programmer
Sep 3, 2005
100
CA
Hello - I copied the following code from a Microsoft article, put in activate on a page in one of my forms and when I click on that page my CPU usage jumps to 100% and stays there. Is there something wrong with this code - or my app!?

oOutlookObj = CREATEOBJECT("Outlook.Application")
oNamespace = oOutlookObj.GetNamespace("MAPI")
oFolder = oNamespace.GetDefaultfolder(6) && See below for other values
oFolder.Display

* Code to pause FoxPro while Outlook window is on top
IF NOT 'FOXTOOLS' $ SET('LIBRARY')
SET LIBRARY TO SYS(2004)+"FoxTools"
ENDIF
FoxWind = MAINHWND()
GetActive=RegFn('GetActiveWindow','','I') && Determine if FoxPro is
&& on top
DO WHILE .T. && Keep looping until the ActiveWindow = FoxWind
IF FoxWind = CallFn(GetActive)
EXIT
ENDIF
ENDDO
SET LIBRARY TO

oOutlookObj.Quit
 
Sorry - I didn't notice the loop at end of this code - commented it out and all is ok
 
Hi Samsnead,

Yes, I can see how that would happen.

The problem is that you have put the code in the form's Activate. Basically, what you are doing is activating the main Foxpro window, which in turn activates your form, which in turn activates the main Foxpro window, and so on.

Worse, every time round that loop, you are creating new instance of the Outlook Application object. That will take time and use resources. Although there's only ever one instance of Outlook running on your system, you are still constantly instantiating a new object based on it.

So, the first thing to do is to move the code to the form's Init. But I would also ask just what do you want to achieve from all this. Whatever it is, there must surely be a better way.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Hi Mike - Thanks for pointing out that problem - still haven't figured out how to only have 1 outlook running since I want to access subfolders in outlook when looking at a clients file. For instance, I may have 100 clients (101-200) that I have sent email to and saved in subfolders in my inbox. When I go into my accounting program, I want a page that can show me the email by client. So in the accounting program, I enter my client number (e.g. 199) to view the accounting details. Then to view email details, just click on the new page and what it to show me inbox with subfolder 199 and list of emails
 
Samsnead,

It's certainly possible to do that - without creating multiple instances of Outlook.

Essentially, you need to create your Outlook object, and to store the object reference somewhere where it will stay in scope, such as a public variable.

Just to give a quick demonstration of the general technique:

Code:
* In your startup program (executed once)
PUBLIC goOutlook
goOutlook = CREATEOBJECT("outlook.application")


* In your form
LOCAL oNS, oFolder, oMessage
oNS = goOutlook.GetNameSpace("MAPI")
oFolder = oNS.GetDefaultFolder(6)
oFolder.Display  && show the inbox
FOR EACH oMessage IN oFolder
  * loop through the messages in the inbox
ENDFOR


* In your closedown code
goOutlook.Quit
RELEASE goOutlook

By the way, in general I don't advocate the use of public variables. I've used one here to simplify the example. In practice, it's generally better to use something like a property of a global application object - but that's another issue.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top