none
check active word document window has customtaskpane. RRS feed

  • Question

  • Hi,

    Iam adding customtaskpane to word 2010 in document_change event. How to check whether customtakpane already added, if not new one needs to be added.

     

    Thursday, December 8, 2011 5:46 PM

Answers

  • Hi Cindy,

     

    Thanks for your reply. To identity the existance of CustomTaskPane, I tried adding the document name and customtaskpane collection to dictionary object and using the ActiveDocument property i'm able to get current document instance by which i get the CustomTaskPane collcetion fron dictionary. This works fine.

     

    Thanks,

    Megarekaa.

    Wednesday, December 14, 2011 4:09 PM

All replies

  • Hello,

     

    Are you making an Addin?

     

    If so, try the Globals.ThisAddin.CustomTaskPanes collection.Here you can iterate through all the task panes and search if there are some that are Visible, or you can search for your task panes if you added some unique identifiers to be able to recognize it.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Friday, December 9, 2011 6:54 AM
  • Hi,

    Thanks for your reply. I tried out this solution. But the thing is i'm not getting the current active window customtaskpane collection. When i open a new word document, customtaskpane collection has reference to previous word document collection. How to instantiate customtaskpane for each new document.

     

    Regards,

    Megarekaa.

    Friday, December 9, 2011 7:22 AM
  • Hello,

     

    It depends on what you put inside the CustomTaskPane. You could try to identify each taskpane with a document, and on document change to simply change the Visibility depending on the visible document. Also you would need to have 1 CustomTaskPane for each document, and Remove the CustomTaskPane on DocumentBeforeClose.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Friday, December 9, 2011 7:27 AM
  • Hi Megarekaa

    The title you assign to a custom task pane becomes the "Name" property of the CommandBar in Word that displays the Custom Task Pane. You can therefore loop the CommandBars collection of the document in question and check whether the CommandBar is visible. Example VBA code

    Sub TestCTPVisible()
        Dim doc As word.Document
        Dim ctpName As String
        ctpName = "Test CTP"
        Set doc = Application.ActiveDocument
        MsgBox IsCTPVisible(ctpName, doc)
    End Sub

    Function IsCTPVisible(ctpName As String, doc As word.Document) As Boolean
        Dim cb As Office.CommandBar
        Dim isVisible As Boolean
       
        isVisible = False
        For Each cb In doc.CommandBars
            If cb.Name = ctpName Then
                isVisible = True
            End If
        Next
        IsCTPVisible = isVisible
    End Function


    Cindy Meister, VSTO/Word MVP
    Friday, December 9, 2011 7:38 AM
    Moderator
  • Hi Cindy Meister,

    Thanks for your reply. If i understand correctly commandbars are application specific and not document specific.The first time i open a word document commandBar doesn't contain the added customtaskpane. Next time if i open a new document commandbar already contains customtaskpane since the first document was open. Is it possible to determine customtaskpane per document or in otherwords event to be used to create customtaskpane per document?

    Regards,

    Megarekaa. 

    Friday, December 9, 2011 10:15 AM
  • Hi Megarekaa

    CommandBars can be part of the application or part of a document. I did test my code before I posted, but when I tried it again on reading your reply, it didn't work. I don't know what mistake I made before that it did appear to work...

    So I don't think you're going to be able to do this using anything native to Word or VSTO. I suspect it should be possible using the Windows API to detect what window is within which main document window. But I'm not conversant enough with the Windows API to be able to tell you how to procede from here.


    Cindy Meister, VSTO/Word MVP
    Monday, December 12, 2011 10:15 AM
    Moderator
  • Hi Cindy,

     

    Thanks for your reply. To identity the existance of CustomTaskPane, I tried adding the document name and customtaskpane collection to dictionary object and using the ActiveDocument property i'm able to get current document instance by which i get the CustomTaskPane collcetion fron dictionary. This works fine.

     

    Thanks,

    Megarekaa.

    Wednesday, December 14, 2011 4:09 PM
  • Glad you found an acceptable way, and thanks for taking a moment to let us know about it :-)
    Cindy Meister, VSTO/Word MVP
    Wednesday, December 14, 2011 4:24 PM
    Moderator
  • Hello,

     

    Make sure you use the hole  name, that includes the path, and make sure you update the dictionary when Save As is triggered, or save on temporary document, because this operatinos change the name of the document.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Wednesday, December 14, 2011 4:42 PM
  • Hi ,

     

    Thanks for your reply. Yes i do add FullName of the document to dictionary collecton and as suggested for save, I was looking for some event for the same. But could find only DocumentBeforeSave() event which is fired before a document is getting saved. Any suggestions to update the dictionary with saved path of the document?

     

    Thanks,

    Megarekaa.

    Friday, December 16, 2011 7:12 AM
  • Hello,

     

    I solved this problem using an UUID stored in the XML of the document.

    You could try the same thing, or you could use the Cancel parameter of the BeforeSave method, and launch the window from the BeforeSave event, add the path + name to the document, and save it (or something like that).

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Friday, December 16, 2011 7:29 AM
  • Hi,

    Thanks for your reply. can you provide me some sample or reference for the same.

    Regards,

    Megarekaa.

     

     

    Friday, December 16, 2011 7:42 AM
  • Hello,

     

    Here is a code sample that you could use.

    public void GetUUIDOfDocument(Document doc)
            {
                if (doc == null) return null;
                foreach (Microsoft.Office.Core.CustomXMLPart customXMLPart in doc.CustomXMLParts)
                {
                    if (customXMLPart.DocumentElement.BaseName.Equals("docUUID"))
                    {
                    //store the UUID here or something that can help you identify the TaskPane.
                        customXMLPart.DocumentElement.Text = Guid.NewGuid().ToString();
                        return; 
                    }
                }
                //generate a new Part to add it to the document with the UUID or something that can help you identify the TaskPane.
                string guid = Guid.NewGuid().ToString();
                string xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                "<docUUID xmlns=\"http://schemas.microsoft.com/vsto/samples\">" + guid + "</docUUID>";
               
                Office.CustomXMLPart orderXMLPart = doc.CustomXMLParts.Add(xmlString, missing);
            }
    


    Best regards,

    Silviu.

     


    http://www.rosoftlab.net/
    Friday, December 16, 2011 8:44 AM