Solved

VSTO 2010 Word Ribbon Add in Active Document Name

Posted on 2012-03-22
8
1,821 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
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
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…

820 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