Multiple open pages

D

Dave Mc

Hi,

While using the COM API and using the .NavigateTo(pageID,,) feature, I often
have multiple instances of OneNote open, mostly with different pages selected.

In an attempt to try to detect this situation, I then search for the
currently viewed pages using code similar to:

Dim viewedpages As XmlNodeList = _
xmldoc.SelectNodes("//one:page[@isCurrentlyViewed='true']", nsmgr)

For Each vp As XmlNode In viewedpages
Debug.Print("vp: " & vp.Attributes("name").Value) 'view the nodes
Next

Independent of how many OneNote instances (and pages) I have open, I only
detect a single 'vp' node. Is this the expected behavior? Is it predictable
which node is reported as 'vp' (eg the most recently opened)?

Is there a better construct to use?

Apols that the snippet is in VB.

DaveMc
 
J

John Guin [msft]

Hello Dave,

Can you post your code (at least the minimal set to reproduce the problem)
and make a note of how many OneNote windows you have open?
 
D

Dave Mc

John,

Thanks once again for your prompt response. I've created a mini-project
with an example sub, for want of a better way to demonstrate it. It just
needs a single empty Form1 control.

Here's the code:
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports Microsoft.Office.Interop.OneNote
Imports System.Xml

Public Class Form1

Private Sub Form_Load(ByVal sender As Object, ByVal e As
System.EventArgs) _
Handles Me.Load

example()

End Sub

Private Sub example()

Dim appOneNote = New Microsoft.Office.Interop.OneNote.Application

'get ON hierarchy
Dim onhierarchy As String
appOneNote.GetHierarchy(Nothing,
Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages, onhierarchy)

'create XMLDocument instance and load hierarchy
Dim xmldoc As XmlDocument = New XmlDocument
xmldoc.LoadXml(onhierarchy)

'create a namespace manager for getting the onenote nodes (required
because of 'one:' prefix
Dim nsmgr As XmlNamespaceManager = New
XmlNamespaceManager(xmldoc.NameTable)
nsmgr.AddNamespace("one",
"http://schemas.microsoft.com/office/onenote/2007/onenote")

'get the ID of the first section (where new pages will be created)
Dim sections As XmlNodeList = xmldoc.SelectNodes("//one:Section",
nsmgr)
Dim selectedsection As XmlNode = sections(0)
Dim selectedsectionID = selectedsection.Attributes("ID").Value
Debug.Print("new pages created in section: " &
selectedsection.Attributes("name").Value)

'create a first new page in the first section
Dim newpageguid_1 As String = System.Guid.NewGuid.ToString()
appOneNote.CreateNewPage(selectedsectionID, newpageguid_1,
NewPageStyle.npsBlankPageWithTitle)
Debug.Print("newpage_1: " & newpageguid_1)

'navigate to the new page
appOneNote.NavigateTo(newpageguid_1, "", True)

'create a second new page in the first section
Dim newpageguid_2 As String = System.Guid.NewGuid.ToString()
appOneNote.CreateNewPage(selectedsectionID, newpageguid_2,
NewPageStyle.npsBlankPageWithTitle)
Debug.Print("newpage_2: " & newpageguid_2)

'navigate to the new page
appOneNote.NavigateTo(newpageguid_2, "", True)

'reload the modified hierarchy and find viewed pages
appOneNote.GetHierarchy(Nothing,
Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages, onhierarchy)
xmldoc.LoadXml(onhierarchy)
Dim viewedpages As XmlNodeList =
xmldoc.SelectNodes("//one:page[@isCurrentlyViewed='true']", nsmgr)

'enumerate all the pages with 'isCurrentlyViewed' attribute = 'true'
For Each vp As XmlNode In viewedpages
Debug.Print("vp: " & vp.Attributes("ID").Value)
Next

End Sub

End Class


When invoked, two new untitled pages are created in the first section of the
first workbook (for me, that's the "Getting Started With OneNote" section of
"OneNote 2007 Guide". The following code enumerates the pages with
'isCurrentlyViewed' attribute set to 'true'. I see only one page node in the
nodelist. If you run it many times you will get 'many x2' pages concurrently
open but I see still only a single 'vp' node.

Hope the example is clear enough. Just wondering if it's supposed to be
like this. If so, then how else can I detect when multiple pages are viewed?

Sorry it's VB, again.

DaveMc
 
I

Ilya Koulchin

Dave said:
While using the COM API and using the .NavigateTo(pageID,,) feature, I often
have multiple instances of OneNote open, mostly with different pages selected.

In an attempt to try to detect this situation, I then search for the
currently viewed pages using code similar to:

Dim viewedpages As XmlNodeList = _
xmldoc.SelectNodes("//one:page[@isCurrentlyViewed='true']", nsmgr)

For Each vp As XmlNode In viewedpages
Debug.Print("vp: " & vp.Attributes("name").Value) 'view the nodes
Next

Independent of how many OneNote instances (and pages) I have open, I only
detect a single 'vp' node. Is this the expected behavior? Is it predictable
which node is reported as 'vp' (eg the most recently opened)?

That is expected - OneNote will only report a single "currently viewed"
page. If more than one window is open, the last window that was
interacted with will report its page as currently viewed.
Is there a better construct to use?

You could enumerate the top level windows on the desktop to see how many
windows of OneNote are open.
 
D

Dave Mc

Ilya,

Thanks for the response. That's fine. I just needed to know whether what I
was seeing was expected behaviour and whether there was a better way to do it
within OneNote.

I'll look for a solution outside of ON.

Thanks again.

Dave

Ilya Koulchin said:
Dave said:
While using the COM API and using the .NavigateTo(pageID,,) feature, I often
have multiple instances of OneNote open, mostly with different pages selected.

In an attempt to try to detect this situation, I then search for the
currently viewed pages using code similar to:

Dim viewedpages As XmlNodeList = _
xmldoc.SelectNodes("//one:page[@isCurrentlyViewed='true']", nsmgr)

For Each vp As XmlNode In viewedpages
Debug.Print("vp: " & vp.Attributes("name").Value) 'view the nodes
Next

Independent of how many OneNote instances (and pages) I have open, I only
detect a single 'vp' node. Is this the expected behavior? Is it predictable
which node is reported as 'vp' (eg the most recently opened)?

That is expected - OneNote will only report a single "currently viewed"
page. If more than one window is open, the last window that was
interacted with will report its page as currently viewed.
Is there a better construct to use?

You could enumerate the top level windows on the desktop to see how many
windows of OneNote are open.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top