Harbour Dialect : attempt to use Outlook - get errors
Posted: Mon Jun 12, 2023 8:29 pm
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:
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
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
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]