FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Macros in Outlook
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Macros in Outlook
Posted: Mon Jul 22, 2024 07:53 PM

Hello,

In my application I use macros in Word : oWord:Run("Macro").

So far so good.

Now I need to run a macro in Outlook and tried it the same way : oOutlook:Run("MacroOutl").

But here I get an error saying that the export method "RUN" doesn't exist.

How do I run a macro in Outlook from my FWH-application?

Thanks a lot for any help.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Macros in Outlook
Posted: Tue Jul 23, 2024 06:05 AM

Dear Michel,

Please try this:

oOutlook:Application:RunMacro( "MacroOutl" )

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Macros in Outlook
Posted: Tue Jul 23, 2024 07:10 AM

Dear Antonio,

Thanks for your reaction.

I tried your suggestion but I got an error : WINOLE/1009 No exported method: RUNMACRO

I also tried: oOutlook:Application:Run( "MacroOutl" )

But unfortunately I got the same error : WINOLE/1009 No exported method: RUN

Any further suggestions?

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Macros in Outlook
Posted: Tue Jul 23, 2024 08:00 AM
Hello Michel,

I found some code in my own programs - this worked for me:
Code (fw): Select all Collapse
FUNCTION InFolder()

   local oOutlook := CREATEOBJECT( "Outlook.Application" )
   local oNameSpace := oOutlook:GetNameSpace("MAPI")
   local oInbox := oNameSpace:GetDefaultFolder( olFolderInbox )

   local i

   FOR i = 1 TO oInbox:Items:Count
      msginfo( oInbox:Items[ i ]:Body )
   NEXT

   oOutlook:Quit()

RETURN NIL
And read you have to use for RUN - maybe this helps? - not tested:
And you have:

Outlook is installed on the machine where this code runs.
Security settings allow macros to run and the VBA project object model is accessible (Trust Center settings in Outlook)

Code (fw): Select all Collapse
LOCAL oOutlook
LOCAL oNamespace
LOCAL oModule

TRY
    // Create Outlook application object
    oOutlook := CreateObject("Outlook.Application")

    // Get the MAPI namespace
    oNamespace := oOutlook:GetNamespace("MAPI")

    // Get the VBProject of Outlook
    oModule := oOutlook:VBProject:VBComponents("Module1") // Adjust "Module1" to the name of your module

    // Run the macro
    oOutlook:Run("'" + oModule.Name + ".MacroOutl'")

CATCH
    // Handle errors
    MsgInfo("An error occurred while trying to run the macro.")
END

// Release the Outlook object
oOutlook := NIL


Best regards,
Otto
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: Macros in Outlook
Posted: Tue Jul 23, 2024 03:42 PM
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Macros in Outlook
Posted: Tue Jul 23, 2024 03:52 PM

Hello guys,

Very interesting information.

I'm going to do some test tomorrow and let you know the results.

Thank you all very much.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Macros in Outlook
Posted: Wed Jul 24, 2024 01:42 PM
Hello Otto,

This is the code I use:
Code (fw): Select all Collapse
jOutlook   := TOleAuto():New("Outlook.Application")
jNameSpace := jOutlook:GetNameSpace("MAPI")
jEmail     := jOutlook:CreateItem(olMailItem)
jModule    := jOutlook:VBProject:VBComponents("Module1")
jOutlook:Run("'"+jModule:Name+".PasteClipboardInNewEmail'")
And this is the error I get:
Application
===========
   Path and name: M:\SOFTWARE\JUDA\JUDA_2_D.EXE (32 bits)
   Size: ********* bytes
   Compiler version: xHarbour 1.2.3 Intl. (SimpLex) (Build 20190614)
   FiveWin  version: FWH 24.04
   C compiler version: Pelles ISO C Compiler 3.0 (32-bit)
   Windows 11 64 Bits, version: 6.2, Build 9200

   Time from start: 0 hours 19 mins 53 secs
   Error occurred at: 24/07/2024, 15:34:08
   Error description: Error Outlook.Application/0  S_OK: VBPROJECT
   Args:

Stack Calls
===========
   Called from:  => TOLEAUTO:VBPROJECT( 0 )
   Called from: C:\SOFTWARE\JUDA\PRG\FGEGTL1.PRG => SENDMAIL( 3771 )
   Called from: C:\SOFTWARE\JUDA\PRG\DOCWP2.PRG => EMAILDOC( 5457 )
   Called from: C:\SOFTWARE\JUDA\PRG\JUDA.PRG => MAILSEND( 3309 )
   Called from: C:\SOFTWARE\JUDA\PRG\FGEGTL2.PRG => (b)BUILDBAR1( 3401 )
   Called from: .\source\classes\btnbmp.prg => TBTNBMP:CLICK( 809 )
   Called from: .\source\classes\btnbmp.prg => TBTNBMP:LBUTTONUP( 1096 )
   Called from: .\source\classes\control.prg => TCONTROL:HANDLEEVENT( 1851 )
   Called from: .\source\classes\btnbmp.prg => TBTNBMP:HANDLEEVENT( 2173 )
   Called from: .\source\classes\window.prg => _FWH( 1000 )
   Called from:  => WINRUN( 0 )
   Called from: .\source\classes\window.prg => TWINDOW:ACTIVATE( 1114 )
   Called from: C:\SOFTWARE\JUDA\PRG\JUDA.PRG => MAIN( 1012 )
Any idea why I get this error?

What do I want to do? I copy the text of a Word-document into the clipboard.
And I want to paste this this text into the body of the new created e-mailmessage.

Thanks a lot.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Macros in Outlook
Posted: Wed Jul 24, 2024 02:42 PM
Hello Michel,
this works for me.
Best regards,
Otto
Code (fw): Select all Collapse
#include "FiveWin.ch"

static oDlg, oClp

function Main()
    
    DEFINE DIALOG oDlg ;
        TITLE "Send email" ;
        FROM 6, 6 ;
        TO 30, 60
    
    DEFINE CLIPBOARD oClp OF oDlg
    
    @ 3, 2 BUTTON "Send Email - Text from Clipboard" OF oDlg ;
        ACTION ( EXEC_EMAILSENDEN() )
        
    @ 6, 2 BUTTON "&Bye!" OF oDlg ACTION oDlg:End()
    
    ACTIVATE DIALOG oDlg ;
        VALID MsgYesNo( "Want to end ?" )
    
return nil

//----------------------------------------------------------------------------//

function EXEC_EMAILSENDEN( )
    
    local oMail, oOutlook, oNameSpace, oAttachment
    local cMailTo           := "winhasdfotefe@fests.at"   
    local cBetreff      := "Test Clipboard"  
    local cHTMLBody         :=  ""
    local cHTMLContent  := "" 
    local cText             := ""
    
    cText := oClp:GetText()
    
    #define olMailItem 0
    #define olFormatHTML 2
    
    oOutlook    := CREATEOBJECT("Outlook.Application")
    oNameSpace  := oOutlook:GetNameSpace("MAPI")
    oMail       := oOutlook:CreateItem(olMailItem)
    oMail:Subject := cBetreff
    oMail:BodyFormat := olFormatHTML
    
    
    cHTMLContent := '<h1>test</h1><p>The new cafeteria is open...</p><br>' + cText + 'Ende'
        
    
    oMail:HTMLBody = cHTMLContent
    
    oMail:To := cMailTo
    oMail:Display = .F.
    oMail:Send()
    
    oMail := NIL
    
    ? "gesendet"
    
RETURN NIL
//----------------------------------------------------------------------------//
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Macros in Outlook
Posted: Wed Jul 24, 2024 03:10 PM

Otto,

Thank you very much for your suggestion.

My first tests look very promessing.

The first results are very good.

Thank you once again.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Macros in Outlook
Posted: Wed Jul 24, 2024 03:59 PM

Michel,

If you also need to include images, then I think you first need to split the clipboard content and link the images separately. This part is only for text.

Best regards,

Otto

Continue the discussion