Consulting

Results 1 to 7 of 7

Thread: Mailitem from template ??

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    52
    Location

    Mailitem from template ??

    I have this code to generate a mailitem BUT ... see this thread:

    http://www.vbaexpress.com/forum/show...-attach-a-file

    Unfortunately it does NOT use the same template as if I myself create a new mailitem in Outlook.

    I've tried different templates Normal.dotm, NormalEmail.dotm, MAIL.oft - I'm NOT allowed to use these files in VBA.

    Here are the code I changed:

    FROM this: Set MailOutLook = appOutLook.CreateItem(olMailItem)

    TO this: Set MailOutLook = appOutLook.CreateItemFromTemplate("C:\Users\ksor\AppData\Roaming\Microsoft\ Templates\Normal.dotm", OutLook.olMailItem)


    What do I do wrong ?
    Last edited by ksor; 03-02-2018 at 06:19 AM.

  2. #2
    You cannot use Word templates for this. They must be Outlook templates. You can create a template by saving a suitably appropriate message as a template from Outlook. You can then call that template from your macro.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Posts
    52
    Location
    I now use this code:

    Set MailOutLook = appOutLook.CreateItemFromTemplate( _
    "C:\Users\ksor\AppData\Roaming\Microsoft\Templates\KS-Mail.oft", OutLook.olMailItem)



    and it stops there with this error:

    KS-Mail-error.jpg

  4. #4
    What's the rest of your code and are you running it from Outlook VBA or from some other Office application VBA?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Jan 2018
    Posts
    52
    Location
    I linked the code in my first posting ...

    I'm running it from with in different forms in Access

    Office Version 365 Home under Windows 10

    I created the Oft-template by just making a new mailitem i Outlook (and it allready have the right template because I use Word as editor)
    and saved it as a template - I just used the folder was presented to me when "Save as".

    I then changed the path to the oft-file in the code as shown.

    But still it wont work - why ?

  6. #6
    There are some syntax issues relating to Outlook, so the code should look like the following. Note that the code uses the function from http://www.rondebruin.nl/win/s1/outlook/openclose.htm to open Outlook correctly. You will see why if you read the accompanying data on that linked page. Using the test macro the following works. The code uses late binding to Outlook and does not therefore require a reference to the Outlook object library.

    Sub Test()
        sendMail "", "This is some text"
    End Sub
    
    Public Sub sendMail(Attach As String, txt As String)
    'This macro requires
    'http://www.rondebruin.nl/win/s1/outlook/openclose.htm
    'To open Outlook
    
    Dim appOutLook As Object
    Dim MailOutLook As Object
    Dim fso As Object
    
        If Attach <> "" Or txt <> "" Then
            Set appOutLook = OutlookApp()
            Set MailOutLook = appOutLook.CreateItemFromTemplate( _
                              "C:\Users\ksor\AppData\Roaming\Microsoft\Templates\KS-Mail.oft")
            With MailOutLook
                .To = ""
                .cc = ""
                '.bcc = ""
                .Subject = "Dokument fra Keld Sørensen"
                If txt <> "" Then
                    .Body = .Body & vbCrLf & vbCrLf & txt
                Else
                    .HTMLBody = "Indtast en besked her !"
                End If
                If Attach <> "" Then
                    Set fso = CreateObject("Scripting.FileSystemObject")
                    If fso.FileExists(Attach) Then
                        .Attachments.Add Attach
                    Else
                        MsgBox Attach & " eksisterer ikke"
                    End If
                End If
                .Display    ' Slet ikke denne linje
    
                ' .Send     ' Her sendes mailen umiddelbart
            End With
        Else
            MsgBox "Du vil ikke skrive tekst, du vil ikke vedhæfte noget !!" & vbCrLf & vbCrLf & _
                   "Hvad vil du egentlig ... Ret fejlen og prøv igen !"
        End If
    lbl_Exit:
        Set appOutLook = Nothing
        Set MailOutLook = Nothing
        Set fso = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Regular
    Joined
    Jan 2018
    Posts
    52
    Location
    It's not working ... it can't even compile.

    Let's drop it !

    My own code is working nicely EXCEPT for the template issue - I'll have to live with that missing template !

Posting Permissions

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