Re: Send document as attachment in Outlook
- From: "Graham Mayor" <gmayor@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 9 Feb 2007 08:53:25 +0200
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
.
- References:
- Send document as attachment in Outlook
- From: jeanmac
- Re: Send document as attachment in Outlook
- From: Doug Robbins - Word MVP
- Re: Send document as attachment in Outlook
- From: jeanmac
- Re: Send document as attachment in Outlook
- From: Doug Robbins - Word MVP
- Re: Send document as attachment in Outlook
- From: jeanmac
- Send document as attachment in Outlook
- Prev by Date: Re: Copy pages from a new document to an existing one
- Next by Date: RE: Repeatedly Change First Letter After [period][blank] to Uppercase
- Previous by thread: Re: Send document as attachment in Outlook
- Next by thread: Re: Send document as attachment in Outlook
- Index(es):
Relevant Pages
|
Loading