Re: Send document as attachment in Outlook



See http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

jeanmac wrote:
Hello again

Sorry to keep bothering you, however I have inserted this piece of
code in my document and it doesn't delete the document. It stays
there. Am I inserting the code at the wrong place? I know you must
think I am really stupid, but I do want to learn. My code now is:

Sub Send_As_Mail_Attachment()

' send the document as an attachment in an Outlook Email message
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

'unprotect the two protected sections of the document (need to do this
before protecting all)
ActiveDocument.Unprotect Password:="secret"

'protect the document before sending
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(2).ProtectedForForms = True
ActiveDocument.Sections(3).ProtectedForForms = True
ActiveDocument.Protect Password:="secret", NoReset:=False, Type:= _
wdAllowOnlyFormFields

ActiveDocument.Save

'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")

'Outlook wasn't running, start it from code
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
'Set the recipient for a copy
.CC = "me@xxxxxxxxxx"
'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"
.Display
End With

Dim DeleteDoc As String
DeleteDoc = MsgBox("Do you want to delete this document?", vbYesNo)

If DeleteDoc = vbYes Then

Dim Doc As Document
Dim DocPathName As String
Set Doc = ActiveDocument
DocPathName = Doc.FullName
Doc.Close wdDoNotSaveChanges
Kill DocPathName
End If


'If we started Outlook from code, then close it
If bStarted Then
oOutlookApp.Quit
End If

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing


End Sub


"Doug Robbins - Word MVP" wrote:

Dim Doc As Document
Dim DocPathName As String
Set Doc = ActiveDocument
DocPathName = Doc.FullName
Doc.Close wdDoNotSaveChanges
Kill DocPathName

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"jeanmac" <jeanmac@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:2D305BA6-F5B3-4A08-8F2F-5E465E9FF926@xxxxxxxxxxxxxxxx
Thanks for your reply. Can you give me an example of the code that
would be
used to delete the document please? I'm a very rusty VBA user I'm
afraid. Thanks for your help

Jean

"Doug Robbins - Word MVP" wrote:

There is no way around that other than to amend the code Kill the
document
after it has been sent.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of
my services on a paid consulting basis.

Doug Robbins - Word MVP

"jeanmac" <jeanmac@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:54B3966F-85AA-49CF-B860-A4D5852DF4EE@xxxxxxxxxxxxxxxx
I need to send a docoment directly from Word as an Outlook
attachment with
a
specific address in the cc field. I have managed to do this with
help. However, the users do not want to save the document before
sending. Can I
amend the code to reflect that? Would really appreciate the
help. The code I
have is:

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

'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")

'Outlook wasn't running, start it from code
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
'Set the recipient for a copy
.CC = "MyEmail.com"
'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"
.Display
End With

'If we started Outlook from code, then close it
If bStarted Then
oOutlookApp.Quit
End If

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing


.



Relevant Pages

  • Re: Formatted e-mails
    ... Dim oOutlookApp As Object ... 'Outlook wasn't running, start it from code ... Set oOutlookApp = CreateObject ... Set oItem = oOutlookApp.CreateItem ...
    (microsoft.public.word.vba.general)
  • Re: Send document as attachment in Outlook
    ... Dim oOutlookApp As Outlook.Application ... 'Get Outlook if it's running ... Set oOutlookApp = CreateObject ... Set oItem = oOutlookApp.CreateItem ...
    (microsoft.public.word.vba.general)
  • Re: Formatted e-mails
    ... Dim source As Document, Maillist As Document, TempDoc As Document ... ' Check if Outlook is running. ... Set oOutlookApp = CreateObject ... Set oItem = oOutlookApp.CreateItem ...
    (microsoft.public.word.vba.general)
  • Re: Automate the "File/Send/As Attachment" as an embedded link in
    ... the assumption is being made that everyone will be using Outlook. ... Dim oOutlookApp As Outlook.Application ... Set oOutlookApp = CreateObject ... Set oItem = oOutlookApp.CreateItem ...
    (microsoft.public.word.vba.general)
  • Document as Outlook Attachment
    ... based on a template as an attachment in Outlook. ... Dim oOutlookApp As Outlook.Application ... Set oOutlookApp = CreateObject ... Set oItem = oOutlookApp.CreateItem ...
    (microsoft.public.word.vba.general)

Loading