OneNote Email Page from Section | VBA Macro #63

Share on facebook
Share on twitter
Share on linkedin
This is a OneNote macro designed to give you the ability to quickly email the contents of a OneNote Page directly from Outlook. When this macro is run a popup will display all of the OneNote Page options from the OneNote Section (.one file) indicated in the VBA code. To select a OneNote Page simply enter in the leading number that displays in from of its title. Once a leading number is entered an Outlook email will display including all of the contents from the OneNote Page selected. If the OneNote Page contains attachments these will automatically be attached to the Outlook emails that displays. This handling of attachments from within the OneNote Page mirrors the Email Page button you can click from OneNote directly. If you would rather have the attachments embedded into the Outlook email body you can update the VBA code below so the format is Rich Text.

See it in Action!

Watch this video to see this macro in action.

***Video coming soon!***

The Code

Here is the code for this macro. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Outlook 16.0 Object Library, Microsoft Office 16.0 Object Library, Microsoft Word 16.0 Object Library
'Leverage & Lean "Less Clicks, More Results"
Sub OneNoteEmailPagefromSection()
Dim Action As String '
Dim AttachmentCheck As String '
Dim Attachment As Variant '
Dim Attachments() As String '
Dim AttachmentsString As String '
Dim Counter As Integer '
Dim Editor As Object '
Dim fso As Object '
Dim ID As Variant '
Dim IDs() As String '
Dim Name As Variant '
Dim Names() As String '
Dim PageID As String '
Dim PathLocation As String '
Dim PageName As String '
Dim PublishContentTo As String '
Dim objEmail As MailItem '
Dim objWord As Object '
Dim oneNote As oneNote.Application '
Dim Options As String '
Dim outXML As String '
Dim PageIDs As String '
Dim PageNames As String '
Dim sectionID As String '
Dim Start As Integer '
Dim TempFolder As Object '
Dim WordDocument As Object '
 
On Error GoTo LeverageLean

PathLocation = "" 'OneNote Section to display the Page options for selection
Set oneNote = New oneNote.Application 'Activate OneNote Application
oneNote.OpenHierarchy PathLocation, "", sectionID 'Opens the OneNote Section (Hierarchy) indicated in the PathLocation
oneNote.GetHierarchy sectionID, hsPages, outXML
Counter = 1
Start = 1
Do Until InStr(Start, outXML, "Page ID=") = 0
Start = InStr(Start, outXML, "Page ID=") + 9
PageID = Mid(outXML, Start, InStr(Start, outXML, "name=") - Start - 2)
Start = InStr(Start, outXML, "name=") + 6
PageName = Mid(outXML, Start, InStr(Start, outXML, "dateTime=") - Start - 2)
If Options = "" Then
PageNames = PageName
PageIDs = PageID
Options = Counter & ". " & PageName
ElseIf Options <> "" Then
PageNames = PageNames & ";" & PageName
PageIDs = PageIDs & ";" & PageID
Options = Options & vbNewLine & Counter & ". " & PageName
End If
Counter = Counter + 1
Loop

Action = InputBox(Options, "OneNote Email Page from Section") 'Display the OneNote Page Selection options
 
If IsNumeric(Action) = False Then 'If the Action enter is NOT value Exit Sub
Exit Sub
ElseIf IsNumeric(Action) = True Then 'Determine what the PageID is for the selected OneNote Page
Counter = 1
Names = Split(PageNames, ";")
For Each Name In Names
If Counter = Action Then
PageName = Name
End If
Counter = Counter + 1
Next
Counter = 1
IDs = Split(PageIDs, ";")
For Each ID In IDs
If Counter = Action Then
PageID = ID
End If
Counter = Counter + 1
Next
End If
oneNote.GetPageContent PageID, outXML, piAll, xs2013 'Get the XML contents of the page.
Do Until InStr(1, outXML, "pathSource=") = 0
outXML = Replace(outXML, Left(outXML, InStr(1, outXML, "pathSource=") - 1), "")
If AttachmentsString = "" Then
AttachmentsString = Mid(outXML, InStr(1, outXML, "pathSource=") + 12, InStr(1, outXML, "preferredName=") - 15)
ElseIf AttachmentsString <> "" Then
AttachmentsString = AttachmentsString & ";" & Mid(outXML, InStr(1, outXML, "pathSource=") + 12, InStr(1, outXML, "preferredName=") - 15)
End If
outXML = Replace(outXML, Left(outXML, InStr(1, outXML, "preferredName=") - 1), "")
Loop
Set fso = CreateObject("Scripting.FileSystemObject") 'Set fso
Set TempFolder = fso.GetSpecialFolder(2)
PublishContentTo = TempFolder.Path & "\" & Replace(PageName, "/", "_") & " PublishContentTo.docx"
oneNote.Publish PageID, PublishContentTo, pfWord
Set objWord = CreateObject("Word.Application")
Set WordDocument = objWord.Documents.Open(PublishContentTo)
objWord.Visible = False
WordDocument.Content.Copy
WordDocument.Close
fso.DeleteFile PublishContentTo 'Delete the file in the Temporary Folder
Set objEmail = Application.CreateItem(olMailItem)
'objEmail.To = "" 'Set if you want a default To Recipient
'objEmail.CC = "" 'Set if you want a default CC Recipient
objEmail.Subject = PageName 'Email Subject is set to the selected OneNote Page's Name
'objEmail.BodyFormat = olFormatRichText 'Set if you want the attachments to be set within the Email Body.
objEmail.Display
Set Editor = objEmail.GetInspector.WordEditor
Editor.Content.Paste
AttachmentsString = Replace(AttachmentsString, "amp;", "")
Attachments = Split(AttachmentsString, ";")
For Each Attachment In Attachments
AttachmentCheck = Dir(Attachment)
If AttachmentCheck <> "" Then
objEmail.Attachments.Add Attachment
End If
Next
'objEmail.Send

Set objEmail = Nothing
Set Editor = Nothing
Set objWord = Nothing
Set WordDocument = Nothing
Set fso = Nothing
Set TempFolder = Nothing
Set oneNote = Nothing

Exit Sub
  
LeverageLean:
MsgBox (Err.Number & " - " & Err.Description & vbNewLine & vbNewLine & "Don't hesitate to email me: brentschneider@leveragelean.com")

End Sub
'Stay Awesome

Insider Content

Here is the Insider code for this macro. This code will allow you to setup a Word Document to reference that contains the additional content you want pulled into the beginning of your Outlook email! This can contain a simply greeting, a reference to the OneNote Page below, even your Outlook Email Signature (including images). Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Outlook 16.0 Object Library, Microsoft Office 16.0 Object Library, Microsoft Word 16.0 Object Library

First time using VBA?

The Developer Tab is an additional section of the ribbon when activated allows you access to Visual Basic in Applications like Access, Excel, Outlook, PowerPoint, Word.
Setup Now
Once the Developer Tab is available you will have the capability to update your security to allow Macros to run in the current application.
Setup Now
A sub of code is a collection of objects and variables. For the code to successfully run a library of references needs to be set.
Setup Now
Subscribe as an Insider to receive additional rights.
If you like our content and want to show your support tip us here!

Contact Us

Looking to improve your computer processes?
Leverage & Lean is here to help!

Leverage & Lean

Is focused on improving computer processes by Leveraging programming and applying Lean principles. It is our goal to make you more efficient and effective on your computer producing “Less Clicks and More Results!”