none
How to set caption in special Word document in Office Add-ins C# RRS feed

  • Question

  • Please help:

    I need to change in the word document caption , I know full filename. 

    For example – save document

    documentthis.Application.ActiveDocument.SaveAs2(doc.Document.FullFileName, AddToRecentFiles: false); 

    after that I  synchronize document – about 1 minutes

    then set caption

    this.Application.ActiveWindow.Caption = this.Application.ActiveWindow.Caption + " saved"; 

    but if the user switched to another word window, the cation will change for it.

    How do I search for a document using the full name of the document?

    Thanks!

    Tuesday, September 6, 2016 10:55 AM

Answers

  • >>How do I search for a document using the full name of the document?

    You could use Documents[index] or Document[fullName] to identify the special document.

    However, if you want to use index/fullname to identify the document, the document should be opened in the current application instance.

    For more information, please visit Documents interface, you could find:

    Use Documents(index), where index is the document name or index number to return a single Document object.

    I am using office2016 and testing in the ribbon, you could see the code below.

    Using Documents["specific document's fullName"], if the specific document didn’t be opened in current application, you would get an exception: bad file name

    After opening the other document Doc1.docx in current application, succeed in showing its fullname

    However, this situation only occurs when debugging, because the other documents would be opened in the new application instance when debugging the project. In the normal condition, all the documents are in the same application instance.

    In general, you could use Documents(index) to distinguish the special document.

             // change ActiveWindow.caption 
                Globals.ThisAddIn.Application.ActiveWindow.Caption = Globals.ThisAddIn.Application.ActiveWindow.Caption + " saved";          
                System.Windows.Forms.MessageBox.Show(Globals.ThisAddIn.Application.ActiveWindow.Caption);
                // show ActiveDocument.FullName
                System.Windows.Forms.MessageBox.Show("ActiveDocument.FullName is "+Globals.ThisAddIn.Application.ActiveDocument.FullName);
                // Documents[1] refers to ActiveDocument
                System.Windows.Forms.MessageBox.Show("Documents[1].FullName is " + Globals.ThisAddIn.Application.Documents[1].FullName);
                // Using Documents["specific document's fullName"], if the specific document didnt be opened in current application, you would get an exception: bad file name
                // after opening the other document 1.docx in current application, succeed in showing its fullname
                try
                {
                  System.Windows.Forms.MessageBox.Show(Globals.ThisAddIn.Application.Documents[@"C:\Users\Administrator\Desktop\1.docx"].FullName);
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
    

    Wednesday, September 7, 2016 1:10 PM
    Moderator

All replies

  • >>How do I search for a document using the full name of the document?

    You could use Documents[index] or Document[fullName] to identify the special document.

    However, if you want to use index/fullname to identify the document, the document should be opened in the current application instance.

    For more information, please visit Documents interface, you could find:

    Use Documents(index), where index is the document name or index number to return a single Document object.

    I am using office2016 and testing in the ribbon, you could see the code below.

    Using Documents["specific document's fullName"], if the specific document didn’t be opened in current application, you would get an exception: bad file name

    After opening the other document Doc1.docx in current application, succeed in showing its fullname

    However, this situation only occurs when debugging, because the other documents would be opened in the new application instance when debugging the project. In the normal condition, all the documents are in the same application instance.

    In general, you could use Documents(index) to distinguish the special document.

             // change ActiveWindow.caption 
                Globals.ThisAddIn.Application.ActiveWindow.Caption = Globals.ThisAddIn.Application.ActiveWindow.Caption + " saved";          
                System.Windows.Forms.MessageBox.Show(Globals.ThisAddIn.Application.ActiveWindow.Caption);
                // show ActiveDocument.FullName
                System.Windows.Forms.MessageBox.Show("ActiveDocument.FullName is "+Globals.ThisAddIn.Application.ActiveDocument.FullName);
                // Documents[1] refers to ActiveDocument
                System.Windows.Forms.MessageBox.Show("Documents[1].FullName is " + Globals.ThisAddIn.Application.Documents[1].FullName);
                // Using Documents["specific document's fullName"], if the specific document didnt be opened in current application, you would get an exception: bad file name
                // after opening the other document 1.docx in current application, succeed in showing its fullname
                try
                {
                  System.Windows.Forms.MessageBox.Show(Globals.ThisAddIn.Application.Documents[@"C:\Users\Administrator\Desktop\1.docx"].FullName);
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
    

    Wednesday, September 7, 2016 1:10 PM
    Moderator
  • Еhank you very much, it works.

    Regards,

    Sergey

    Saturday, September 10, 2016 4:24 AM