Solved

VSTO 2010 Word Ribbon Add in Active Document Name

Posted on 2012-03-22
8
1,775 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:hbash
  • 3
  • 2
  • 2
8 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 37752607
The ActiveDocument object has .Name, .Path and .FullName properties
0
 
LVL 6

Assisted Solution

by:xenacode
xenacode earned 250 total points
ID: 37756237
You can query Application.ActiveDocument.FullName in the ThisAddIn.ThisAddIn_Startup event code (assuming you are programming in VB):

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

      Dim docName = Me.Application.ActiveDocument.FullName
      ...

End Sub

The Application object is declared WithEvents so you can also add code to handle the DocumentOpen event (and other Word Application events) within the ThisAddIn class and reference the ActiveDocument from there:

 Private Sub Application_DocumentOpen(Doc As Microsoft.Office.Interop.Word.Document) Handles Application.DocumentOpen

      Dim docName = Me.Application.ActiveDocument.FullName
      ...

End Sub


Pete
Xenacode Ltd
0
 
LVL 1

Author Comment

by:hbash
ID: 37756702
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 6

Expert Comment

by:xenacode
ID: 37756752
I'm not too familiar with Sharepoint. Can't help you there.
0
 
LVL 1

Author Comment

by:hbash
ID: 37757103
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37958656
Do my suggestions not work?
0
 
LVL 1

Author Comment

by:hbash
ID: 38104876
All good.  I accidently requested attention.  I will request that they remove "requested attention".  Sorry about that.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now