Results 1 to 17 of 17

Thread: Emailing using late biinding

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Emailing using late biinding

    Sending email using late binding

    Could someone please correct this for sending email using late binding (using the structure as I have, as far as possible)? I am back to VB6 after an absence. This is what I have constructed from memory

    Code:

    Code:
      'Email1    Send email using late binding
    
        Const outlookReplace = 1 'why?
    
        Option Explicit
        Dim objApp As Object ' * 'Object' is for late binding
        Dim objEmail As Object
        Dim Message As String
    
        Private Sub Form_Load() ' * needed?
    
        End Sub
    
        Private Sub cmdGo_Click()
        Message = txtMessage.Text
        Set objApp = CreateObject("outlook.application")
            With objEmail
                .To = "(myown email address)"
                .Subject = "Testing automatic send at " & Time
                .body = Message
                .send
            End With
        End Sub
    
        Private Sub cmdExit_Click()
            Set objEmail = Nothing
            Set objApp = Nothing
            Unload Me
        End Sub
    Last edited by el84; Today at 05:45 AM.
    Thanks all !

  2. #2
    Addicted Member jg.sa's Avatar
    Join Date
    Nov 2017
    Location
    South Australia ( SA )
    Posts
    199

    Re: Emailing using late biinding

    G'Day

    It is a very long time since I debugged a late bound email app. so going all in on memory here

    Isn't a line missing between these

    Set objApp = CreateObject("outlook.application")
    With objEmail

    What is objEmail defined as ???

    Plus, I hate to be the bearer, but I found I had to create FIFO queues 'outbox' on the disk and write the emails to them first, makes it a lot more complicated.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Emailing using late biinding

    Quote Originally Posted by jg.sa View Post
    G'Day

    Isn't a line missing between these

    Set objApp = CreateObject("outlook.application")
    With objEmail

    What is objEmail defined as ???
    Yes, I see I need to set it to something. What do you suggest?
    Thanks all !

  4. #4
    Addicted Member jg.sa's Avatar
    Join Date
    Nov 2017
    Location
    South Australia ( SA )
    Posts
    199

    Re: Emailing using late biinding

    G'Day el84

    Were about are you in geo terms, isnt it Sat. nite at the moment ???

    Have you been drinking

    To me this is a design issue.

    Something like this

    Set msg = outlook.CreateItem(mailitem) <- you need to be using the class explorer to find the .'s or properties or methods or ??? of outlook, turn it on

    But, I have never used outlook to send an email, why are you using M$ Out... ???

    Late binding should only be tackled ( Oz football term ) in Ver. II of any app.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Emailing using late biinding

    Aus. I start very early in the morning. Re drinks, funny you should say that, but I did have 187ml champagne with our traditional Sunday breakfast Eggs Benedict but that was ages ago!

    The reason I am using late binding is I want to use the app across several machines, and they all have different versions of MS Office. I use Outlook because I am used it it and am comfortable with all features (except one!)

    After a bit of research I came up with this 'missing' line and am about to test it!

    Code:
    Set objEmail = objApp.Createitem(0)
    .
    Thanks all !

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Emailing using late biinding

    Well that didn't work! The app gets hung up (both before and after inserting that line). The message on the lines of process still running... An the only way I can get out of it is by closing VB from Task Manager!
    Thanks all !

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Emailing using late biinding

    as you would be creating a new outlook object for each email you are trying to send, it could open many instances of outloook
    you should test if objApp is already an open instance of outlook, before creating another
    Code:
    if objApp is nothing then Set objApp = CreateObject("outlook.application")
    certainly having multiple instance of outlook open could cause the program to hang

    you should avoid using global variable where not essential
    objEmail should be dimensioned within the procedure and the message variable is not really required at all .body = txtMessage.Text, but that is your choice, but again should be only in scope during the procedure

    please note that outlook may prevent the sending of automated emails, depending on security settings and versions
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    Addicted Member jg.sa's Avatar
    Join Date
    Nov 2017
    Location
    South Australia ( SA )
    Posts
    199

    Re: Emailing using late biinding

    G'Day el84

    Quote Originally Posted by el84 View Post
    Aus.
    SA ???

    Quote Originally Posted by el84 View Post
    I start very early in the morning.
    So you work in a bakery ???


    Quote Originally Posted by el84 View Post
    Re drinks, funny you should say that, but I did have 187ml champagne with our traditional Sunday breakfast Eggs Benedict but that was ages ago!
    When you were typing your reply I was in a famous Barossa Valley bakery eating wait for it

    Eggs Benedict


    Quote Originally Posted by el84 View Post
    The reason I am using late binding is I want to use the app across several machines, and they all have different versions of MS Office. I use Outlook because I am used it it and am comfortable with all features (except one!)
    See below for a DLL that will mean you never need to use M$ Out again


    [SNIP]


    This is really difficult for me as I have been sending SMTP mail for 20+ years with VB - Pre 6 - try this DLL http://www.freevbcode.com/ShowCode.asp?ID=109

    Plus I have code to send SMTP in MASM , C , C++ and Java - I have been building virus's for a very long time

    The trick with this DLL is that it has a list of domains and I removed this as there is so many TLDs these day 21st Cen. and all

    As Molly would say do yourself a favor !!!

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Emailing using late biinding

    I have pored over this latest version and it appears OK, both looking at previous resolved threads and looking at one of my other programs that successfully automates word stuff, using late binding. But this does not work, and hangs up if I try to exit after no apparent activity. I can see from the taskbar that Outlook is opening. but email is not being sent

    Code:
    'Email1    Send email using late binding.  No extra references
    
    Const outlookReplace = 1 'why?
    
    Option Explicit
    Dim objApp As Object ' * 'Object' is for late binding
    Dim objEmail As Object
    Dim Message As String
    
    Private Sub Form_Load() ' * needed?
    
    End Sub
    
    Private Sub cmdGo_Click()
    Message = txtMessage.Text
    If objApp Is Nothing Then Set objApp = CreateObject("Outlook.Application")
    If objEmail Is Nothing Then Set objEmail = objApp.CreateItem(0)
        With objEmail
            .To = "{my email address}
            .Subject = "Testing automatic send at  " & Time
            .Body = Message
            .send
        End With
    End Sub
    
    Private Sub cmdExit_Click()
        Set objEmail = Nothing
        Set objApp = Nothing
        Unload Me
    End Sub
    Last edited by el84; Jun 20th, 2021 at 03:58 PM.
    Thanks all !

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Emailing using late biinding

    Message = txtMessage.Text
    If objApp Is Nothing Then Set objApp = CreateObject("Outlook.Application")
    If objEmail Is Nothing Then Set objEmail = objApp.CreateItem(0)
    unlike objapp, obj email does need to create a new email everytime, this is why it should be dimensioned within the procedure

    i can not see any reason within your code that it should not work as desired, but when i tested your code, as mentioned above Name:  clipic.jpg
Views: 291
Size:  11.6 KB if outlook is hidden (not topmost) the code will stop while the dialog is displayed and your program will hang until the dialog is dismissed, personally i would not use outlook for several reasons
    Last edited by westconn1; Jun 21st, 2021 at 05:45 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Emailing using late biinding

    Thanks Westconn1. You always take pains to fix solutions for me. Much appreciated.

    Two things arise.

    1 That warning message is not showing for me but no doubt that's why my app hangs. So how can I reply Yes from my Vb6 code, or at least show the dialogue box

    2 Could you say why you would not use outlook, and say what you suggest.
    Thanks all !

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Emailing using late biinding

    Could you say why you would not use outlook, and say what you suggest.
    that message is one reason, but until recently i did not use outlook at all, still don't like it for daily use
    personally i use cdo.message for all my email sending, the code is very similar, but uses a different mapi object and you have to specify the server. port and some other settings, to tell how to send the message

    So how can I reply Yes from my Vb6 code, or at least show the dialogue box
    such is a complex coding which may work or not, but i have had success with some similar situations, i did see some solution in this forum by robdog previously, never tested to see if it works, it was possibly for a much earlier version outlook, and any solution may become invalid in a future version, also i have no experience of this for cloud based office
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Emailing using late biinding

    Quote Originally Posted by westconn1 View Post
    also i have no experience of this for cloud based office
    Presumably you mean the dreaded Office 365? I have kept well away from that, after giving it a trial for a very short time. Smacks of everything being under Msoft's control. Like continual niggles with each W10 'update', (the latest being Firefox on taskbar now showing blank instead of its Icon).
    Thanks all !

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Emailing using late biinding

    Still trying to make this work. I did write a program which did, but I have unfortunately deleted it. But I know from that that it must be possible. Here is the latest code, with local dims as advised

    Code:
    'Email1    Send email using late binding.No extra references
    
    Option Explicit
    
    Private Sub cmdGo_Click()
    Dim objApp As Object ' * 'Object' is for late binding
    Dim objEmail As Object
    Dim Message As String
    Message = txtMessage.Text
    If objApp Is Nothing Then Set objApp = CreateObject("Outlook.Application") 'hangs up on this line (does open Outlook as shown on task bar)
    If objEmail Is Nothing Then Set objEmail = objApp.CreateItem(0)
        With objEmail
            .From = "(me)"
            .To = "(me)"
            .Subject = "Testing automatic send at  " & Time
            .Body = Message
            .send
        End With
    End Sub
    
    Private Sub cmdExit_Click()
        Set objEmail = Nothing
        Set objApp = Nothing
        Unload Me
    End Sub
    Thanks all !

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Emailing using late biinding

    objapp should still be global, the others should be at procedure level, not that this is going to cause the problem you were having

    the same code may well work form within outlook, the problem is when it is run from some outside application

    some reading that may help
    https://stackoverflow.com/questions/...macro-in-excel

    Outlook is specifically designed to prevent you from doing this. The user always should know that someone is sending emails on their behalf. (After all, that's why the warning was added in the first place) As @SiddharthRout says, you might be able to break Outlook's security by "acting as the user" with the security dialog, but I'm fairly certain there will be mitigations in place to make it hard for you to do that. – Billy ONeal Aug 13 '12 at 4:47
    from https://stackoverflow.com/questions/...automation-vba

    microsoft has been doing its very best to prevent what you are trying to do
    Last edited by westconn1; Jun 23rd, 2021 at 05:18 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16
    Addicted Member jg.sa's Avatar
    Join Date
    Nov 2017
    Location
    South Australia ( SA )
    Posts
    199

    Re: Emailing using late biinding

    Quote Originally Posted by jg.sa View Post
    G'Day el84
    this is a design issue.
    Late biding is gr8 for VBA, not a standalone app.

  17. #17
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Emailing using late biinding

    i doubt very much that early vs late binding will make any difference to this problem, the problem is trying to use outlook at all
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width