使用VBA在onenote中创建新部分

问题描述 投票:0回答:2

您好,我已经在互联网上搜索了创建新的 onenote 部分的示例,但我找不到适合我理解的示例。我能得到的最接近的是使用

.OpenHierarchy
函数,但我对它仍然很陌生,我无法获得正确的参数。

我目前正在为多个 PDF 文件开发 OCR 宏。一切都工作正常,直到我意识到我在计算机上创建了巨大的垃圾文件。

这是我用来删除该部分中创建的所有页面的代码

将 oneNote 调暗为 OneNote14.Application

Dim secDoc As MSXML2.DOMDocument60
Set secDoc = New MSXML2.DOMDocument60

Dim secNodes As MSXML2.IXMLDOMNodeList
Set secNodes = secDoc.DocumentElement.getElementsByTagName("one:Section")

' 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

oneNote.DeleteHierarchy (sectionID)
oneNote.OpenHierarchy
End Sub

Deletehierarchy 函数会删除整个部分,不留下任何部分,但我的 OCR 宏至少需要一个部分才能工作。

感谢您的阅读并提前感谢您!

vba excel onenote
2个回答
1
投票

vba 中的oneNote.OpenHierarchy 不允许使用bracklet,这就是导致错误的问题。

解决方案:

oneNote.OpenHierarchy 文件名, "", "新建第 1 节", 3


0
投票

@Lingxing 的原始答案是正确的,但这个答案是我使用它时出现的。

枚举取自 OneNote 开发人员参考

Option Explicit
    '
    ' Member  Value   Description
    ' cftNone     0   Creates no new object.
    ' cftNotebook 1   Creates a notebook by using the specified name and location.
    ' cftFolder   2   Creates a section group by using the specified name and location.
    ' cftSection  3   Creates a section by using the specified name and location.
    '
    Sub sbCheckAnswer()
        Dim oneNote As oneNote.Application
        Set oneNote = New oneNote.Application
        Dim sNewSectionID1 As String
        ' oneNote.OpenHierarchy fileName, "", "New Section 1", 3 ' the answer being checked
        oneNote.OpenHierarchy "C:\Users\david\Documents\OneNote Notebooks\My Check Section.one", "", "New Section 1", 3
        ' above line creates a section called "My Check Section"
        ' the default folder for OneNote is "C:\Users\david\Documents\OneNote Notebooks\"
    
    ''    oneNote.OpenHierarchy "C:\Users\david\Documents\OneNote Notebooks\My New Section.one", "", "", cftSection
    ''     the above line creates a section called "My New Section"
    
    '''    oneNote.OpenHierarchy "C:\Users\david\Documents\OneNote Notebooks", "", sNewSectionID1, cftFolder ' this runs without error
    '''    MsgBox sNewSectionID1 ' returns section ID ending 5E5
    ''''     <one:Notebook name="OneNote Notebooks" nickname="OneNote Notebooks" ID="{4...8F1B-9.85-4.AD-9.E0-9283F4DC5E5}{1}{B0}" path="C:\Users\david\Documents\OneNote Notebooks" lastModifiedTime="2024-06-27T17:32:17.000Z" color="#D5A4BB">
    ''''     the above XML is from the subroutine sbGetPagesXML below
    
    End Sub
    
    Sub sbGetPagesXML()
        Dim oneNote As oneNote.Application
        Set oneNote = New oneNote.Application
        Dim sPagesXML As String
        oneNote.GetHierarchy "", hsPages, sPagesXML, xs2013
        MsgBox sPagesXML
        sbPrint (sPagesXML)
    End Sub
    
    Sub sbPrint(sText As String): Open "C:\tmp\z-" & Format(Now(), "HHMMSS") & ".txt" For Output As #1: Print #1, sText: Close #1: End Sub

Microsoft® OneNote® for Microsoft 365 MSO(版本 2405 内部版本 16.0.17628.20006)64 位

© www.soinside.com 2019 - 2024. All rights reserved.