Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VSTO 2010 Word Ribbon Add in Active Document Name

Posted on 2012-03-22
8
Medium Priority
?
1,959 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 76

Accepted Solution

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

Assisted Solution

by:xenacode
xenacode earned 1000 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

879 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