#1
|
|||
|
|||
VBA to email specific page of Doc
Can anyone help?
I would like code that on clicking the command button on an individual page of a word document, send that page to email so it can be sent to someone else. Ideally I'd like the page to sent as a new word document. Thanks |
#2
|
||||
|
||||
The following macro will copy the selected page of the document to a new temporary document and attach that new document to an e-mail message. The temporary document is then deleted. Note the comment at the start of the macro about additional code required!
Code:
Sub Send_Selected_Page_As_Mail_Attachment() 'Graham Mayor - https://www.gmayor.com - Last updated - 01 May 2020 'Send the current page of the document as an attachment _ in an Outlook Email message 'Requires the code from - http://www.rondebruin.nl/win/s1/outlook/openclose.htm 'to either retrieve an open instance of Outlook or open Outlook if it is closed. Dim bStarted As Boolean Dim olApp As Object Dim oItem As Object Dim oDoc As Document Dim olInsp As Object Dim wdDoc As Document Dim oRng As Range Dim oTempDoc As Document Dim strName As String Dim strPath As String Dim oHeader As HeaderFooter, oFooter As HeaderFooter Set oDoc = ActiveDocument 'Save the document oDoc.Save If Len(oDoc.Path) = 0 Then MsgBox "Document must first be saved" GoTo lbl_Exit End If 'Copy the selected page oDoc.Bookmarks("\page").Range.Copy 'Prompt the user to save the document strPath = Environ("TEMP") & "\PageCopy.docx" On Error GoTo err_Handler: WordBasic.DisableAutoMacros 1 Set oTempDoc = Documents.Add(Template:=oDoc.FullName) For Each oHeader In oTempDoc.Sections(1).Headers oHeader.Range.Text = "" Next For Each oFooter In oTempDoc.Sections(1).Footers oFooter.Range.Text = "" Next oTempDoc.Range.Text = "" oTempDoc.Range.Paste oTempDoc.SaveAs2 FileName:=strPath, AddToRecentFiles:=False oTempDoc.Close WordBasic.DisableAutoMacros 0 'Get Outlook if it's running Set olApp = OutlookApp() On Error GoTo 0 'Create a new mailitem Set oItem = olApp.CreateItem(0) With oItem .Subject = "This is the subject" .Attachments.Add strPath .BodyFormat = 2 Set olInsp = .GetInspector Set wdDoc = olInsp.WordEditor Set oRng = wdDoc.Range oRng.Collapse 1 oRng.Text = "This is the coveriung message text" _ & vbCr & vbCr & "Another line of message text! ... etc" .Display End With 'delete the temporary file Kill strPath lbl_Exit: Set oItem = Nothing Set olApp = Nothing Set oTempDoc = Nothing Set oDoc = Nothing Set olInsp = Nothing Set oRng = Nothing Set wdDoc = Nothing Exit Sub err_Handler: Err.Clear GoTo lbl_Exit End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
thanks
Thank you GMayor that worked exactly how I wanted it to.
|
#4
|
||||
|
||||
Cross-posted at:
Word VBA Email Page and: VBA to email specific page of Doc For cross-posting etiquette, please read: Excelguru Help Site - A message to forum cross posters
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Sorry
Sorry
I'm very new to all this so had no idea. Thanks for the heads up. |
#6
|
|||
|
|||
Help please
Hi
I attached to supplied code. I visited the suggested website also to get the code and inserted it as a new module. However when I click on the command button it only opens that page as a new word doc, it doesn't connect it to outlook. Sorry I'm very new to this so any help you could provide would be great. thanks |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How can I transfer specific data from email to specific excel cells? | Mauro | Outlook | 1 | 11-11-2019 09:17 PM |
How to make a specific page as default page when I open a document? | Thahir | Word | 3 | 11-13-2017 11:38 PM |
Flag email to specific domain | pbaldy | Outlook | 2 | 04-28-2017 08:27 AM |
prevent email from being sent to specific email address | ianhaney | Outlook | 5 | 07-19-2016 11:09 PM |
move new email to a specific folder | roofi | Outlook | 1 | 10-30-2015 07:20 AM |