Generating email in Word

S

Sean

I have code (below) to generate an email in Word, but I get an error: 'User
defined type not defined'. I believe it is referring to oOutlookApp and
oItem. I can't figure out what the issue is though. Thanks for the help!

***

Sub Email_manager()

Application.ScreenUpdating = False
ActiveDocument.Save


'email report

Application.DisplayAlerts = False

Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

If Len(ActiveDocument.Path) = 0 Then
MsgBox "Document needs to be saved first"
Exit Sub
End If

Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

Set oItem = Outlook.Application.CreateItem(olMailItem)
FileName = ActiveDocument.CustomDocumentProperties("WeekEndingDate").Value
Consult = ActiveDocument.CustomDocumentProperties("ConsultLast").Value

With oItem
.To = ActiveDocument.CustomDocumentProperties("ManagerEmail").Value

.Subject = "SCWU (" & Consult & ") & FileName"

'Add the document as an attachment, you can use the .displayname property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue, _
DisplayName:="Document as attachment"
.Body = ActiveDocument.CustomDocumentProperties("ManagerName").Value &
"," & vbCrLf & vbCrLf & "Attached is my SCWU for week ending " &
ActiveDocument.CustomDocumentProperties("WeekEndingDate").Value & " ." &
vbCrLf _
& "Please let me know if you have any questions or comments." &
vbCrLf & vbCrLf & "Thanks," & vbCrLf &
ActiveDocument.CustomDocumentProperties("ConsultFirst").Value
'.Send
.Display

On Error Resume Next
End With

Application.ScreenUpdating = True
Application.DisplayAlerts = True


End Sub
 
J

Jezebel

Either --

1. Use early binding (which is what you're declaration line expects): In VBA
go to Tools > References and add a reference to the "Microsoft Outlook
object library". Then fix your instantion code accordingly --

Set oOutlookApp = Outlook.Application

In this case, there's no need to use the error check on new vs existing
instance on Outlook. The code you have is correct for some applications
(like Word) to pick up an existing instance if any or create a new one if
not. However some applications (Excel, Outlook) do this automatically.


or 2. Use late binding. Change the declaration to declare oOutlookApp and
oItem as object. In this case your instantion code is correct as it is.
 
S

Sean

Thanks. One issue though. When I declare oItem and oOutlookApp as objects
and the code executes, nothing happens. The email is not generated. Any
ideas?
 
J

Jezebel

You have the Send command commented out.



Sean said:
Thanks. One issue though. When I declare oItem and oOutlookApp as
objects
and the code executes, nothing happens. The email is not generated. Any
ideas?
 
S

Sean

The .Send command is commented out because I am using the .Display command so
that the email is shown on-screen and the user can send it manually after
review. Is there any other reason why this email isn't displayed?
Thanks,
Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top