Использование UpdateHiearchy в коде VB для создания блокнота в OneNote - PullRequest
0 голосов
/ 14 марта 2012

Я пытаюсь выяснить, как добавить записную книжку в OneNote 2010. Я не могу найти примеры кода, показывающие, как использовать API UpdateHiearchy для добавления новой записной книжки. Я пытаюсь сделать это из приложения VB6. Я новичок в использовании XML из VB. код выглядит следующим образом:

Private Function GetFirstOneNoteNotebookNodes(oneNote As OneNote14.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, xs2010

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

    Dim elem As MSXML2.IXMLDOMElement

    If doc.loadXML(notebookXml) Then
        ' Here is search for a notebook that i know is not there.     mvarpAssignment.pClient.Name    is a program variable that contains a text name.
        Set GetFirstOneNoteNotebookNodes = doc.documentElement.selectNodes("//one:Notebook[@name='" & mvarpAssignment.pClient.Name & "']")
' I test the length for zero to see if anything was returned:         
If GetFirstOneNoteNotebookNodes.Length = 0 Then
' I want to create a notebook, so i beleive i need to add an element to the xml returned from the GetHiearchy API:           
Set elem = doc.createElement("ROC")
           doc.documentElement.appendChild elem
'I print out the xml and i can see the element added at the end of the xml document.            
            Debug.Print doc.XML

'Следующим шагом будет вызов API UpdateHiearchy, но я не знаю, какой объект я передам в API. Все, что я пробую, терпит неудачу. Я, очевидно, не понимаю этого достаточно, но я не могу найти примеры кода или текст, который описывает, как добавить блокнот. любая помощь или любые ссылки на информацию будет принята с благодарностью!

Ответы [ 2 ]

0 голосов
/ 21 сентября 2018

Это работает для меня, просто.

Public Sub OneNoteMoM()
    Dim OneNote As New Microsoft.Office.Interop.OneNote.Application

    Dim Path As String = "E:\ISO\VB Test folder\OneNoteTest"
    Dim NoteBookID As String = "TestON2"
    Dim FullID As String = ""

    Dim Dir 'As New IO.DirectoryInfo("")
    Dim File 'As New IO.FileInfo("")

    Dim objFSO
    Dim parentFolder As String = ""

    objFSO = CreateObject("Scripting.FileSystemObject")

    FullID = Path & "\\" & NoteBookID
    Dir = New System.IO.DirectoryInfo(FullID)
    ' Check if folder exists & Create folder
    If Not objFSO.FolderExists(FullID) Then
        objFSO.CreateFolder(FullID)
    End If

    FullID = Path & "\\" & NoteBookID
    OneNote.OpenHierarchy(FullID, "", "", Microsoft.Office.Interop.OneNote.CreateFileType.cftNone)

    FullID = Path & "\\" & NoteBookID & "\\" & "Open Notebook"
    File = New IO.FileInfo(FullID & ".onetoc2")
    If Not File.Exists Then
        OneNote.OpenHierarchy(FullID, "", "", Microsoft.Office.Interop.OneNote.CreateFileType.cftNotebook)
    End If

    FullID = Path & "\\" & NoteBookID & "\\" & "Actionlists.one"
    File = New IO.FileInfo(FullID)
    If Not File.Exists Then
        OneNote.OpenHierarchy(FullID, "", "", Microsoft.Office.Interop.OneNote.CreateFileType.cftSection)
    End If

    FullID = Path & "\\" & NoteBookID & "\\" & "MeetingReports.one"
    File = New IO.FileInfo(FullID)
    If Not File.Exists Then
        OneNote.OpenHierarchy(FullID, "", "", Microsoft.Office.Interop.OneNote.CreateFileType.cftSection)
    End If

End Sub
0 голосов
/ 23 марта 2012

Я наконец получил это, чтобы работать.у меня было две вещи неправильно, путь должен быть определен до обновления иерархии.это требует использования API GetSpecialLocation.Кроме того, имена атрибутов чувствительны к регистру, и у меня был верблюжий, а не нижний регистр.Я сделал пару других изменений из исходного кода, опубликованного, но это было для моего приложения.обновленный код для тех, кому интересно:

Private Function GetClientOneNoteNotebookNode(oneNote As OneNote14.Application,     ClientName As String) As MSXML2.IXMLDOMNodeList
Dim notebookXml As String
Dim doc As MSXML2.DOMDocument
Dim elem As MSXML2.IXMLDOMElement
Dim newNotebookPath As String
Dim defaultNotebookFolder 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, xs2010
' Use the MSXML Library to parse the XML.
Set doc = New MSXML2.DOMDocument
If doc.loadXML(notebookXml) Then
Set GetClientOneNoteNotebookNode = doc.documentElement.selectNodes("//one:Notebook[@name='" & ClientName & "']")
If GetClientOneNoteNotebookNode.Length = 0 Then
'Get the default location for the notebooks
oneNote.GetSpecialLocation slDefaultNotebookFolder, defaultNotebookFolder
newNotebookPath = defaultNotebookFolder + "\\" + ClientName
'Create new notebook for cleint
Set elem = doc.createElement("one:Notebook")
elem.setAttribute "name", ClientName
elem.setAttribute "path", newNotebookPath
' add new elelement to the document tree
doc.documentElement.appendChild elem
oneNote.UpdateHierarchy doc.XML
End If
Else
Set GetClientOneNoteNotebookNode = Nothing
End If
End Function
...