Harbour Dialect : attempt to use Outlook - get errors

This forum is meant for questions and discussions about the X# language and tools
Post Reply
RolWil
Posts: 67
Joined: Mon Jul 18, 2022 3:16 am

Harbour Dialect : attempt to use Outlook - get errors

Post by RolWil »

Hello my fellow XSharp enthusiasts,

I’ve made much progress with my Harbour/Clipper system migration, much of it is straight forward, but now I’m stuck again; this time on something new I’m adding, it was not in the original system: I want to be able to send an email using Outlook on any form that has an email address (I’m adding a small email icon next to any email address text box).

As always, for something new-to-me, I created a sandbox project. The form has 4 fields: recipient’s email, a subject, email body and attachment. I also added a ‘Send’ button.

The project dialect is Harbour; I’ve added the following references:
  • XSharp.Core
  • XSharp.RT
  • Microsoft.Office.Interop.Outlook
I’m using the code at this link as a starting point: Click Me

Issue: when I compile, I get the following two error messages (marked as // <--- Error 1) and // <--- Error 2) in the code sample)
1) 'Microsoft.Office.Interop.Outlook.Application' is a type, which is not valid in the given context
2) Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Outlook.MailItem'. An explicit conversion exists (are you missing a cast?)

Any help would be much appreciated. Thank you.

Cheers
Roland

Code: Select all

PRIVATE METHOD button1_Click(sender AS System.Object, e AS System.EventArgs) AS VOID STRICT
            
            // sample code from: https://www.codeproject.com/Tips/165548/Csharp-Code-Snippet-to-Send-an-Email-with-Attachme
            // Outlook integration: https://www.c-sharpcorner.com/article/outlook-integration-in-C-Sharp/


            // Create the Outlook application:         
            LOCAL oApp AS Outlook.Application
            oApp := Outlook:Application                                 // <--- Error 1)
            
         
            LOCAL oMsg AS Outlook.MailItem
            oMsg := oApp:CreateItem(Outlook:OlItemType:olMailItem)      // <--- Error 2) 
            
           // Set HTMLBody. 
            //add the body of the email
            oMsg:HTMLBody := tbBody:Text
            
            //Subject line
            oMsg:Subject := tbSubject:Text
    
            // Add a recipient.
            LOCAL oRecips AS Outlook.Recipients
            oRecips := oMsg:Recipients
            
            
            //Add an attachment:
            LOCAL sDisplayName AS STRING 
            sDisplayName := "MyAttachment"    
            LOCAL iPosition AS Int
            iPosition := oMsg:Body:Length + 1    
            LOCAL iAttachType AS Int
            iAttachType := (int)Outlook.OlAttachmentType.olByValue
            //now attach the file
            LOCAL oAttach AS Outlook.Attachment
            oAttach := oMsg:Attachments:Add(tbAttachment:Text, iAttachType, iPosition, sDisplayName)
            

            // Send.
            oMsg:Send()
    
            // Clean up.
           // oRecip := null
            oRecips := null
            oMsg := null
            oApp := null

            RETURN
        END METHOD[color=red][/color]
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Harbour Dialect : attempt to use Outlook - get errors

Post by robert »

Roland,
Try this:

Code: Select all

oApp := Outlook:Application{} // curly braces to create the object
and

Code: Select all

oMsg := (Outlook.MailItem) App:CreateItem(Outlook:OlItemType:olMailItem) // cast to type
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
RolWil
Posts: 67
Joined: Mon Jul 18, 2022 3:16 am

Harbour Dialect : attempt to use Outlook - get errors

Post by RolWil »

Thanks Robert for your quick response!

oApp := Outlook.Application{} did the trick! (note: I had to use '.' instead of ':')

oMsg := (Outlook.MailItem) oApp:CreateItem(Outlook:OlItemType:olMailItem) worked , just a typo 'App' needed to be oApp

Onwards and forwards :)
RolWil
Posts: 67
Joined: Mon Jul 18, 2022 3:16 am

Harbour Dialect : attempt to use Outlook - get errors

Post by RolWil »

I posted the working project over in the "Examples" forum, subject: "Send email via Outlook -Sample Project - Harbour Dialect"
Post Reply