Forum Discussion

vondie's avatar
vondie
Contributor
9 years ago
Solved

Why Isn't my Code Working? Opening Outlook in Python Script

I have a basic script that I found that should open Microsoft Outlook and read the last email received, then log the contents of the body. I keep getting a runtime error saying "Member Not Found." I have combed through every thing that is out there about OleObject and COM objects and must admit that the literature is pretty confusing. On a basic level, are these things recognized by TestComplete out of the box or do I need to install some other plugin/add-on somewhere for this to be recognized?

 

I have went through the Object Browser, located the Outlook process, and examined the Properties in Advanced View. There is no OleObject field listed. Any help is appreciated. Thank you!

 

 

def check_email():
  msOutlook = Sys.OleObject("Outlook.Application").Activate

  inbox = msoutlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                    # the inbox. You can change that number to reference
                                    # any other folder
                                    
  messages = inbox.Items
  message = messages.getLast()
  body_content = message.body
  
  Log.Message(body_content)

 

  • Thank you for the responses, everyone. I figured out my issue. The fixed code is below for anyone else that may have this issue in the future: 

     

     

    def check_email():
      msOutlook = Sys.OleObject["Outlook.Application"] # brackets instead of parenthesis
      
      mapi = msOutlook.GetNamespace("MAPI") # different approach for identification of the application
      
      inbox = mapi.Folders("<inbox name>").Folders("Inbox") 
      
      messages = inbox.Items
      message = messages.getLast() # gets most recent email received
      body_content = message.body #grabs the body contents

     

     

4 Replies

  • Thank you for the responses, everyone. I figured out my issue. The fixed code is below for anyone else that may have this issue in the future: 

     

     

    def check_email():
      msOutlook = Sys.OleObject["Outlook.Application"] # brackets instead of parenthesis
      
      mapi = msOutlook.GetNamespace("MAPI") # different approach for identification of the application
      
      inbox = mapi.Folders("<inbox name>").Folders("Inbox") 
      
      messages = inbox.Items
      message = messages.getLast() # gets most recent email received
      body_content = message.body #grabs the body contents