Link to home
Start Free TrialLog in
Avatar of Howard Bash
Howard BashFlag for United States of America

asked on

VSTO 2010 Word Ribbon Add in Active Document Name

I need to obtain the currently loaded active document name and place it on a control (label or textbox) on a custom ribbon built using VSTO 2010.  

From the add in,  how can I detect the name of the active document when word launches and when a subsequent document is loaded?
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Howard Bash

ASKER

The problem with the application document open is that I am working with a ribbon add-in.  

What I tried is to in the ThisAddin code I added:

(1)

 public delegate void DocNameChanges(string NewDocName);
 
 public event DocNameChanges docnamechanges;


and later in that module:

(2)

  /// <summary>
        /// Handle changing reference to current loaded document
        /// </summary>
        private void myDocChangeEventHandler()
        {
            if (Globals.ThisAddIn.Application.Documents.Count > 0)
            {
                ThisDocument = Globals.ThisAddIn.Application.ActiveDocument;

                docnamechanges(ThisDocument.Name);
            }
        }

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            if (Globals.ThisAddIn.Application.Documents.Count > 0)
            {
                // Hook into the ThisDocument Change Event
                Application.DocumentChange += new Word.ApplicationEvents4_DocumentChangeEventHandler(myDocChangeEventHandler);
            }                
        }

(3)

And in myRibbon_Load event added the following:

            //Set Handler for document change and name change
            Globals.ThisAddIn.docnamechanges += HandleNameChange;


And finally that routine:

        private void HandleNameChange(string NewDocName)
        {
            string docExt = Globals.ThisAddIn.ThisDocument.Type.ToString();
            bool bHasExtension = (bool)(NewDocName.IndexOf(".") > -1);
            string AdjustedName = string.Empty;

            switch (Globals.ThisAddIn.ThisDocument.Type)
                {
                    case Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument :
                        if (bHasExtension)
                        {
                            AdjustedName = NewDocName;
                        }
                        else
                        {
                            AdjustedName = NewDocName + ".docx";
                        }
                    break;

                    case Microsoft.Office.Interop.Word.WdDocumentType.wdTypeTemplate:
                        if (bHasExtension)
                        {
                            AdjustedName = NewDocName;
                        }
                        else
                        {
                            AdjustedName = NewDocName + ".dotx";
                        }
                    break;

                    case Microsoft.Office.Interop.Word.WdDocumentType.wdTypeFrameset:
                    AdjustedName = NewDocName;
                    break;
               
                }

            this.txtSaveToFileName.Text = AdjustedName;
            this.txtUploadFileName2.Text = AdjustedName;
            this.txtLocalFileSaveName.Text = AdjustedName;
        }



All this seems to work, except when word is launched via clicking on it from a SharePoint 2010 list.
I'm not too familiar with Sharepoint. Can't help you there.
Imagine you have a hyperlink on a web page and the link is just to a DOCX file.  I don't think the issue is a SharePoint one.  It's just where the hyperlink I am launching word from resides.
Do my suggestions not work?
All good.  I accidently requested attention.  I will request that they remove "requested attention".  Sorry about that.