none
Where is the selection when I have two douments based on the same template opened? RRS feed

  • Question

  • Hi,

    I have a big problem that I have no idea how to tackle it and  even I don't know how to explain it clearly.

    I have developed a word (2007 and 2010) solution based on a template, lets say "MyTemplate".

    When I open, at the same time, two diifferent word documents based on "MyTemplate", it seems like Word got confused and while correctly recognize the Active Document (name), doesn't get the right information from the right document.

    For instance "MyTemplate" does something based on the row of a table the selection is in, or based on what is inside, lets say, the first row. But I don't get the information from the second file opened I get the information from the first one. [note ActiveDocument.Name is correct].

    It is like the two document are not correctly isolated one from the other.

    Thanks, Lauro



    • Edited by Lauro2 Sunday, April 1, 2012 8:36 AM
    Sunday, April 1, 2012 8:35 AM

Answers

  • Thanks again Hans,

    yes, I was thinking that the best thing for me was to use document variables.

    Lauro

    • Marked as answer by Lauro2 Saturday, April 7, 2012 6:47 AM
    Saturday, April 7, 2012 6:47 AM

All replies

  • You could try using

    ActiveWindow.Selection

    or even

    ActiveDocument.ActiveWindow.Selection


    Regards, Hans Vogelaar

    Sunday, April 1, 2012 9:53 AM
  • Thanks Hans for your answer.

    I never used ActiveWindow or ActiveDocument.ActiveWindow, and I really have some problems in understanding the differences between ActiveWindow and ActiveDocument; could you give my some clues, please?

    I wrote a very simple template to experiment with my problem, basically:

    • in the template there is a table.
    • I have an object initialized at document creation. At this object is assigned the name of the document open (or window, it seems the same).
    • This object can read the text in the first cell of the table a show it in a msgbox with title = name of the object.

    This is my class clsMyObject:

    Option Explicit
    Private m_sName As String
    
    Public Property Get Name() As String
       Name = m_sName
    End Property
    Public Property Let Name(ByVal sName As String)
       m_sName = sName
    End Property
    
    Public Property Get Tbl_MyObject() As Table
       Set Tbl_MyObject = ActiveDocument.Tables(1)
    End Property
    
    Public Sub TellMe()
       Dim sResult As String
       Dim rng As Range
       
       Set rng = Tbl_MyObject.Cell(1, 1).Range
       rng.End = rng.End - 1
       sResult = rng.Text
       MsgBox sResult, , Me.Name
       
    End Sub
    


    In a simple module I have an the call back for a button who launch the message  and the global variable:

    objMyObject.TellMe

    Public objMyObject As clsMyObject

    in ThisDocument I have:

    Private Sub Document_New()
       Set objMyObject = New clsMyObject
    '   objMyObject.Name = ActiveDocument.Name
    '   objMyObject.Name = ActiveDocument.ActiveWindow.Caption
       objMyObject.Name = ActiveWindow.Caption
    End Sub
    

    What I get let me think that I have some basic misconceptions.

    Launching one document, I get exactly what expected. The same when I launch the second one. But when I return to the first document still opened. The name of the object is still the second one, but the result from the table in correct.

    Of course I thought (wrongly) that each document had a separate objMyObject variable, and is not the case.

    So I have to think that every thing I do in in my much more complex object in my document project will be maintained in an other document based on the same project.

    How could I can avoid this a be sure that each document is insuleted from the others?

    thanks Lauro

    Wednesday, April 4, 2012 7:07 AM
  • You can open multiple windows on a document, each with its own selection. In that situation, you can specify to which window the Selection object refers.

    Your variable objMyObject "lives" in the template (which is automatically opened when you create or open a document based on it). Its Name property will be the caption of the document most recently created from the template.

    Just retrieve the caption of the window when you need it, the same way you retrieve the text of the first cell when you need it.


    Regards, Hans Vogelaar

    Wednesday, April 4, 2012 8:42 AM
  • Thanks Hans,

        I don't understand why someone would like to open the same document in different windows... Anyway ...

    The Selection object is not Global? (i.e. Global.Selection). But ther is also a Window.Selection which is the difference? And there is always only one selection also with multiple document opened right?

    How can I get a reference to the window or document (for me are still the same...) in which is the selection? Trough Selection.Document.Window or Selection.Document?

    Ok. I changed my project. Now every time an action is performed by clicking the menu a (new) objMyObject is created so it reads the actual situation in the document (active).

    It seems OK.

    But I have another problem. I have to adjust my ribbon on switching from one document (window) to the other, otherwise the old data are kept (I have same global variable for the status of the ribbon controls) and the ribbon doesn't. reflect the document status.

    Should I have an array with one group of variables for each document opened? I feel that there should be a simpler approach...Anyway I need to have variables at document level not project level as I did before (and/or detect when the active windows is changed).

    Thanks again, Lauro

     


    • Edited by Lauro2 Friday, April 6, 2012 4:22 PM
    Friday, April 6, 2012 4:07 PM
  • There are several selection objects:

    Without further qualification, Selection refers to the current selection in the active pane of the active window on the active document.

    You can also refer to a specific window:

    Application.Windows(3).Selection

    or to a specific pane:

    Application.Windows(3).Panes(2).Selection

    You can find out which document a specific Selection object belongs to by inspecting Selection.Document.

    You could use document variables to store document-specific information: see http://msdn.microsoft.com/en-us/library/ff839708.aspx.


    Regards, Hans Vogelaar

    Friday, April 6, 2012 9:51 PM
  • Thanks again Hans,

    yes, I was thinking that the best thing for me was to use document variables.

    Lauro

    • Marked as answer by Lauro2 Saturday, April 7, 2012 6:47 AM
    Saturday, April 7, 2012 6:47 AM