Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-30-2020, 11:39 PM
Steve2081 Steve2081 is offline VBA to email specific page of Doc Windows 7 64bit VBA to email specific page of Doc Office 2016
Advanced Beginner
VBA to email specific page of Doc
 
Join Date: Apr 2020
Posts: 44
Steve2081 is on a distinguished road
Post 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
Reply With Quote
  #2  
Old 05-01-2020, 05:06 AM
gmayor's Avatar
gmayor gmayor is offline VBA to email specific page of Doc Windows 10 VBA to email specific page of Doc Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 05-01-2020, 10:50 AM
Steve2081 Steve2081 is offline VBA to email specific page of Doc Windows 7 64bit VBA to email specific page of Doc Office 2016
Advanced Beginner
VBA to email specific page of Doc
 
Join Date: Apr 2020
Posts: 44
Steve2081 is on a distinguished road
Default thanks

Thank you GMayor that worked exactly how I wanted it to.
Reply With Quote
  #4  
Old 05-01-2020, 04:33 PM
macropod's Avatar
macropod macropod is offline VBA to email specific page of Doc Windows 7 64bit VBA to email specific page of Doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 05-04-2020, 07:41 AM
Steve2081 Steve2081 is offline VBA to email specific page of Doc Windows 7 64bit VBA to email specific page of Doc Office 2016
Advanced Beginner
VBA to email specific page of Doc
 
Join Date: Apr 2020
Posts: 44
Steve2081 is on a distinguished road
Default Sorry

Sorry
I'm very new to all this so had no idea.
Thanks for the heads up.
Reply With Quote
  #6  
Old 05-04-2020, 07:54 AM
Steve2081 Steve2081 is offline VBA to email specific page of Doc Windows 7 64bit VBA to email specific page of Doc Office 2016
Advanced Beginner
VBA to email specific page of Doc
 
Join Date: Apr 2020
Posts: 44
Steve2081 is on a distinguished road
Default 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
Reply With Quote
Reply

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
VBA to email specific page of Doc How to make a specific page as default page when I open a document? Thahir Word 3 11-13-2017 11:38 PM
VBA to email specific page of Doc Flag email to specific domain pbaldy Outlook 2 04-28-2017 08:27 AM
VBA to email specific page of Doc 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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:30 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft