Solved

VSTO 2010 Word Ribbon Add in Active Document Name

Posted on 2012-03-22
8
1,854 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:Howard Bash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Howard Bash
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
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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:Howard Bash
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:Howard Bash
ID: 38104876
All good.  I accidently requested attention.  I will request that they remove "requested attention".  Sorry about that.
0

Featured Post

SuperAntiSpyware Licenses Discounted by 25% !

Exclusive offer to Experts Exchange Members!
Buy SuperAntiSpyware License(s) from us and save 25% on the regular purchase price.
- Includes Full SuperAntiSpyware Vendor Support Entitlements
- Your Subscription does not begin until you activate your license
- Buy for your friends

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 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