Vba 从Outlook向OneNote发送电子邮件的替代方法

Vba 从Outlook向OneNote发送电子邮件的替代方法,vba,onenote,outlook-2013,Vba,Onenote,Outlook 2013,我在互联网上搜索寻找答案,但找不到答案。我的问题是: 我的组织在Outlook 2013收件箱大小方面有大小限制。因此,我们必须拿出可能的解决方案,以一种易于管理和查找的综合方式保存这些电子邮件 我听说我们可以自动将电子邮件直接发送到OneNote 2013。我使用过Outlook中的“发送到OneNote”按钮,但我想知道是否有更好的解决方案,可以在电子邮件发件人的姓名不存在时自动创建一个名为该姓名的分区,并将电子邮件复制到新页面 我在搜索中发现的唯一VBA代码是创建OneNote页面或搜索,

我在互联网上搜索寻找答案,但找不到答案。我的问题是:

我的组织在Outlook 2013收件箱大小方面有大小限制。因此,我们必须拿出可能的解决方案,以一种易于管理和查找的综合方式保存这些电子邮件

我听说我们可以自动将电子邮件直接发送到OneNote 2013。我使用过Outlook中的“发送到OneNote”按钮,但我想知道是否有更好的解决方案,可以在电子邮件发件人的姓名不存在时自动创建一个名为该姓名的分区,并将电子邮件复制到新页面

我在搜索中发现的唯一VBA代码是创建OneNote页面或搜索,但我对XML的知识非常有限,无法进一步深入

谁能给我指出正确的方向吗

以下是我找到的一些代码:

Option Explicit
子CreateNewPage() '连接到OneNote 2010。 要查看代码的结果, '您需要确保OneNote 2010用户 '界面是可见的

Dim OneNote As OneNote.Application
Set OneNote = New OneNote.Application

' Get all of the Notebook nodes.
Dim nodes As MSXML2.IXMLDOMNodeList
Set nodes = GetFirstOneNoteNotebookNodes(OneNote)
If Not nodes Is Nothing Then
    ' Get the first OneNote Notebook in the XML document.
    Dim node As MSXML2.IXMLDOMNode
    Set node = nodes(2)
    Dim noteBookName As String
    noteBookName = node.Attributes.getNamedItem("name").Text

    ' Get the ID for the Notebook so the code can retrieve
    ' the list of sections.
    Dim notebookID As String
    notebookID = node.Attributes.getNamedItem("ID").Text

    ' Load the XML for the Sections for the Notebook requested.
    Dim sectionsXml As String
    OneNote.GetHierarchy notebookID, hsSections, sectionsXml, xs2013
   ' Dim a As MSXML2.DOMDocument60
    Dim secDoc As MSXML2.DOMDocument60
    Set secDoc = New MSXML2.DOMDocument60

    If secDoc.LoadXML(sectionsXml) Then
        ' select the Section nodes
        Dim secNodes As MSXML2.IXMLDOMNodeList
        Debug.Print secDoc.DocumentElement.XML
        Dim soapNS
        soapNS = "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
        secDoc.SetProperty "SelectionNamespaces", soapNS
        Set secNodes = secDoc.DocumentElement.SelectNodes("//one:Section")

        If Not secNodes Is Nothing Then
            ' Get the first section.
            Dim secNode As MSXML2.IXMLDOMNode
            Set secNode = secNodes(0)

            Dim sectionName As String
            sectionName = secNode.Attributes.getNamedItem("name").Text
            Dim sectionID As String
            sectionID = secNode.Attributes.getNamedItem("ID").Text

            ' Create a new blank Page in the first Section
            ' using the default format.
            Dim newPageID As String
            OneNote.CreateNewPage sectionID, newPageID, npsDefault

            ' Get the contents of the page.
            Dim outXML As String
            OneNote.GetPageContent newPageID, outXML, piAll, xs2013

            Dim doc As MSXML2.DOMDocument60
            Set doc = New MSXML2.DOMDocument60
            ' Load Page's XML into a MSXML2.DOMDocument object.
            If doc.LoadXML(outXML) Then
                ' Get Page Node.
                Dim pageNode As MSXML2.IXMLDOMNode
                soapNS = "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
                doc.SetProperty "SelectionNamespaces", soapNS
                Set pageNode = doc.SelectSingleNode("//one:Page")

                ' Find the Title element.
                Dim titleNode As MSXML2.IXMLDOMNode
                Set titleNode = doc.SelectSingleNode("//one:Page/one:Title/one:OE/one:T")

                ' Get the CDataSection where OneNote store's the Title's text.
                Dim cdataChild As MSXML2.IXMLDOMNode
                Set cdataChild = titleNode.SelectSingleNode("text()")

                ' Change the title in the local XML copy.
                cdataChild.Text = "A Page Created from VBA"
                ' Write the update to OneNote.
                OneNote.UpdatePageContent doc.XML

                Dim newElement As MSXML2.IXMLDOMElement
                Dim newNode As MSXML2.IXMLDOMNode

                ' Create Outline node.
                Set newElement = doc.createElement("one:Outline")
                Set newNode = pageNode.appendChild(newElement)
                ' Create OEChildren.
                Set newElement = doc.createElement("one:OEChildren")
                Set newNode = newNode.appendChild(newElement)
                ' Create OE.
                Set newElement = doc.createElement("one:OE")
                Set newNode = newNode.appendChild(newElement)
                ' Create TE.
                Set newElement = doc.createElement("one:T")
                Set newNode = newNode.appendChild(newElement)

                ' Add the text for the Page's content.
                Dim cd As MSXML2.IXMLDOMCDATASection
                Set cd = doc.createCDATASection("Is this what I need to change?")

                newNode.appendChild cd


                ' Update OneNote with the new content.
                OneNote.UpdatePageContent doc.XML

                ' Print out information about the update.
                Debug.Print "A new page was created in "
                Debug.Print "Section " & sectionName & " in"
                Debug.Print "Notebook " & noteBookName & "."
                Debug.Print "Contents of new Page:"

                Debug.Print doc.XML
            End If
        Else
            MsgBox "OneNote 2010 Section nodes not found."
        End If
    Else
        MsgBox "OneNote 2010 Section XML Data failed to load."
    End If
Else
    MsgBox "OneNote 2010 XML Data failed to load."
End If
端接头

Private Function GetAttributeValueFromNode(node As MSXML2.IXMLDOMNode, attributeName As String) As String
If node.Attributes.getNamedItem(attributeName) Is Nothing Then
    GetAttributeValueFromNode = "Not found."
Else
    GetAttributeValueFromNode = node.Attributes.getNamedItem(attributeName).Text
End If
端函数

Private Function GetFirstOneNoteNotebookNodes(OneNote As OneNote.Application) As MSXML2.IXMLDOMNodeList
' Get the XML that represents the OneNote notebooks available.
Dim notebookXml As String
' OneNote fills notebookXml with an XML document providing information
' about what OneNote notebooks are available.
' You want all the data and thus are providing an empty string
' for the bstrStartNodeID parameter.
OneNote.GetHierarchy "", hsNotebooks, notebookXml, xs2013

' Use the MSXML Library to parse the XML.
Dim doc As MSXML2.DOMDocument60
Set doc = New MSXML2.DOMDocument60

If doc.LoadXML(notebookXml) Then
   Dim soapNS
   soapNS = "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
    doc.SetProperty "SelectionNamespaces", soapNS
    Set GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes("//one:Notebook")
    Debug.Print doc.DocumentElement.XML

Else
    Set GetFirstOneNoteNotebookNodes = Nothing
End If
端函数

Private Function GetFirstOneNoteNotebookNodes(OneNote As OneNote.Application) As MSXML2.IXMLDOMNodeList
' Get the XML that represents the OneNote notebooks available.
Dim notebookXml As String
' OneNote fills notebookXml with an XML document providing information
' about what OneNote notebooks are available.
' You want all the data and thus are providing an empty string
' for the bstrStartNodeID parameter.
OneNote.GetHierarchy "", hsNotebooks, notebookXml, xs2013

' Use the MSXML Library to parse the XML.
Dim doc As MSXML2.DOMDocument60
Set doc = New MSXML2.DOMDocument60

If doc.LoadXML(notebookXml) Then
   Dim soapNS
   soapNS = "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
    doc.SetProperty "SelectionNamespaces", soapNS
    Set GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes("//one:Notebook")
    Debug.Print doc.DocumentElement.XML

Else
    Set GetFirstOneNoteNotebookNodes = Nothing
End If

谢谢您的帮助。

您是否正在使用OneDrive进行业务?如果是这样,这些Microsoft流模板可能非常有用:。特别是,其中一个用于向OneNote发送重要电子邮件。您还可以创建自己的具有不同触发器的流,并指定要创建的页面/节的名称

以下是一个示例流程:

注意“添加动态内容”按钮。这允许您指定节的名称和内容


如果您不使用O365,则可以同时使用Microsoft Graph API和OneNote REST API来实现所需的功能。这里没有VBA示例,但还有许多其他示例。

感谢Vishaal的快速响应。我对任何编码都不是很精通,虽然我通常会做我最初开始做的事情,但这一次,我完全被难住了。我不是在使用OneDrive for Business,只是普通的Office 2013 Professional,所以我必须按照您的建议使用graph和REST API。你能给我举个例子,说明你说的使用graph和RESTAPI是什么意思吗?