Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 02-13-2019, 07:29 AM
Kieranf Kieranf is offline Windows 7 64bit Office 2019
Novice
 
Join Date: Feb 2019
Posts: 1
Kieranf is on a distinguished road
Default Problem with submitting a word form via email.

I have created the following code (via a lot of searching) but the attached form is blank. Can anyone see what is wrong?



Code:
 
Private Sub CommandButton1_Click()
Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document
 
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set Doc = ActiveDocument
 
With EmailItem
.Subject = "Hello"
.Body = "Insert message here or just send"
 
.To = "acas-media@media.co.uk"
.Importance = olImportanceNormal 'Or olImprotanceHigh Or olImprotanceLow
.Attachments.Add Doc.FullName
.Display
.Save
 
 
End With
 
Application.ScreenUpdating = True
 
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
 
End Sub

Reply With Quote
  #2  
Old 02-13-2019, 03:27 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,118
Guessed has a spectacular aura aboutGuessed has a spectacular aura aboutGuessed has a spectacular aura about
Default

Has the activedocument been saved before running the code?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 02-13-2019, 09:51 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,704
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

If you are using late binding to Outlook you need to change two lines
Code:
Set EmailItem = OL.CreateItem(0)
and
Code:
.Importance = 1    'Or olImportanceHigh (2) Or olImportanceLow (0)
Word doesn't recognize Outlook specific commands and you must use the numeric equivalents.
If you want to keep the signature or edit the message body directly, you will need to use the following - note the comment at the top
Code:
Private Sub CommandButton1_Click()
'Use the code from http://www.rondebruin.nl/win/s1/outlook/openclose.htm
'To open Outlook correctly or this won't work
Dim OL As Object
Dim EmailItem As Object
Dim olApp As Object
Dim olInsp As Object
Dim Doc As Document
Dim wdDoc As Object
Dim oRng As Object

    Application.ScreenUpdating = False
    Set OL = OutlookApp()
    Set EmailItem = OL.CreateItem(0)
    Set Doc = ActiveDocument

    With EmailItem
        .Subject = "Hello"
        .to = "acas-media@media.co.uk"
        .Importance = 1    'Or olImportanceHigh (2) Or olImportanceLow (0)
        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor
        Set oRng = wdDoc.Range
        oRng.Collapse 1
        oRng.Text = "Insert message here or just send"
        .Attachments.Add Doc.FullName
        .Display
    End With

    Application.ScreenUpdating = True

    Set Doc = Nothing
    Set OL = Nothing
    Set EmailItem = Nothing
    Set wdDoc = Nothing
    Set oRng = Nothing
    Set olInsp = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pulling info from an email into a form on Word jlamx Word 1 07-07-2017 08:24 PM
Adding email button to Word form leilers Word 5 01-09-2012 03:21 PM
Submitting HTML form data in Outlook 2010 ramen Outlook 0 12-15-2011 02:29 PM
Word Form Problem firewatch Word 1 11-08-2011 02:23 AM
MS Word form - email button floydwood Word VBA 0 05-10-2009 04:11 PM


All times are GMT -7. The time now is 08:03 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft