PDA

View Full Version : Mailitem from template ??



ksor
03-02-2018, 04:41 AM
I have this code to generate a mailitem BUT ... see this thread:

http://www.vbaexpress.com/forum/showthread.php?62133-Create-an-Outlook-mailitem-OR-just-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 ?

gmayor
03-02-2018, 06:54 AM
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.

ksor
03-02-2018, 07:58 AM
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:

21733

gmayor
03-02-2018, 10:59 PM
What's the rest of your code and are you running it from Outlook VBA or from some other Office application VBA?

ksor
03-03-2018, 12:20 AM
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 ?

gmayor
03-03-2018, 08:33 AM
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

ksor
03-03-2018, 09:27 AM
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 !