Reputation: 270
In office 2010, if you record a macro to split a screen, you get
ActiveWindow.SplitVertical = 50
But if I do the same in Office 2013, i get
ActiveWindow.Panes(3).Activate
ActiveWindow.SplitVertical = 50
What I don't understand is the pane number 3. How can this be when i only seem to have 2 panes (after the screen has split)? Also I remove the split then run the macro again, it falls over, which isn't surprising as it is trying to active a pane that doesn't exist before it has split the screen. So the recording of the macro doesn't give what it should.
This matters to me as I have a c# vsto project that splits a screen and selects a certain piece of text in the top pane and something else in the bottom pane. With Office 2010, this worked perfectly by activating pane1 or pane2 as needed, but doesn't work with Office 2013. I have found a work around that if I activate pane3 rather than pane2 when i want the bottom pane, it does what I want but this feels unreliable as I don't understand what pane 2 is. Can anyone explain what is going on and advise a reliable way to activate the pane I am interested in?
Upvotes: 2
Views: 450
Reputation: 3175
In Word 2013 the Panes collection is including some, but not all, of Word's task panes within its members. For example, the Navigation Pane can display three types of information: Headings, Pages, and Results. When set to Headings, this pane is not counted within the Panes collection. When set to Pages, it is counted within the Panes collection.
Further complicating matters, an activated pane that is a member of the Panes collection sometimes retains its index in the collection during the entire Word session. So if the Navigation Pane is set to Pages, is used on a document, and that document is then closed, a second document opened afterwards that splits its window can still have its Panes collection index affected by the use of the Navigation Pane with the first document. I was able to reproduce a Panes(3)
in this manner as well as with the Navigation Pane (set to Pages) present on the screen.
I was also able to create a Panes(4)
index and suspect higher index numbers are possible due to the tremendous variety of task panes (footnotes, thesaurus, Spelling & Grammar, etc.)
However, one can activate the desired document pane using code. The Panes collection Next
property will navigate between document panes. Additionally, when an active window is split, the bottom pane becomes the active pane. This information allows the below VBA code (which can be adapted to C# for a VSTO add-in) to work:
Sub SelectCorrectPane()
Dim DocPaneTop As Integer
Dim DocPaneBottom As Integer
Dim PaneCount As Integer
PaneCount = ActiveDocument.ActiveWindow.Panes.Count
'The Next property switches between document panes but
'if the user is clicked into a task pane when the code executes Next
'cycles through the task panes until it gets to the document pane
For i = 1 To PaneCount
ActiveDocument.ActiveWindow.ActivePane.Next.Activate
Next
ActiveWindow.SplitVertical = 50 'This activates bottom pane
DocPaneBottom = ActiveDocument.ActiveWindow.ActivePane.Index
ActiveDocument.ActiveWindow.ActivePane.Next.Activate 'this activates the top pane
DocPaneTop = ActiveDocument.ActiveWindow.ActivePane.Index
ActiveDocument.ActiveWindow.Panes(DocPaneTop).Activate
'Select text in top pane per OP
ActiveDocument.ActiveWindow.Panes(DocPaneBottom).Activate
'perform other actions in bottom pane per OP
End Sub
One final note: for some unknown reason, the code above would not work on my PC when shortened using a
With ActiveDocument.ActiveWindow
'code here
End With
although this may be specific to my setup.
Upvotes: 0