none
I need a function to open the folder of the office file that I am using. RRS feed

  • Question

  • Hello there.

    I'm using office files like excel, powerpoint, doc...

    I cannot open the file folder from the office file that I'm using...

    I have to attach office file to my email.

    Whenever I attach my office file, I have to open the file folder.

    As every office user does, I have a lot of files and folders. Sometimes it's really hard to find the folder location..

    I need the function that can open the folder from office file...

    Please make this function...I'm begging you...Help me...

    Thank you MS members in advance.

    Tuesday, March 6, 2012 6:53 AM

Answers

  • Hi Kevin,

    If you want to implement the demand by code, I think what you need is a Shared add-in for the Office Application, namely excel, powerpoint, doc etc.

    Please take a look at the following steps.

    1. New the Shared Add-In. 

     1.1Open Visual Studio 2010 as a Administrator (Right click on the Visual Studio 2010 Icon, go to "Run As Administrator" ). 

    1.2 In the New Project dialog, in the left "Installed Templates", expand "Other Project Types" node, select "Extensibility". In the Right panel, select "Shared Add-in".  Now in the Name textbox, type the name for the project. Here I used "MySharedAddin" as the name. Click "Ok".

    1.3 In Shared Add-in wizard, "Welcome to the Add-in Wizard", click on Next
    -> select the "Create an Add-in using Visual C#" (The default value), click on "Next"
    -> In the "Select An Application Host", check "Microsoft Word", "Microsoft  PowerPoint","Microsoft Excel". Here the addin is for word, powerpoint and excel. Click on "Next"
    -> Set the Name of the Add-in. I took "ShowFolderSharedAddin" as the input.
    ->"Choose Add-in Options", check the two checkbox. Click "Next"
    ->"Finish".

    2. The MySharedAddin Project.

    2.1 Open the Solution Explorer panel. In the Reference node, remove the Microsoft.Office.Core. 

    Right click on the "Reference", go to "Add References...". In the COM tab, add "Microsoft Excel 14.0 Object Library" "Microsoft Word 14.0 Object Library" "Microsoft PowerPoint 14.0 Object Library" as references one by one. In the COM tab, add "Microsoft Office 14.0 Object Library" (TypeLib Version 2.5) as a reference. In the .NET tab, select "System.Windows.Form" as a reference. 

    Import the namespaces in the beginning of Connect.cs :

    using Office = Microsoft.Office.Core;
    using Excel = Microsoft.Office.Interop.Excel;
    using Word = Microsoft.Office.Interop.Word;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using System.Windows.Forms;


    2.2 Add a xml file into the project. (Right click on the project -> Add -> New Item-> add a xml file named as MyButton.xml) The content of MyButton.xml is shown as following:

    <?xml version="1.0" encoding="utf-8" ?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <ribbon>
        <tabs>
          <tab idMso="TabHome">
            <group id ="group1"  label ="Folder">
              <button id ="button1" label ="Show Folder" onAction ="button1_click" size ="large" imageMso ="HappyFace"/>
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>

    Now in the Properties window of MyButton.xml, set Build Action to "Embeded Resource". 

    Click on the Properties node of the project, go to the "Resources" tab, click on the link. then drag "MyButton.xml" from solution explorer onto the Resources panel. 

    2.3 Go to the Connect.cs code. The code is shown as below:

    public class Connect : Object, Extensibility.IDTExtensibility2,Office.IRibbonExtensibility { ... private object applicationObject; private object addInInstance; // After the code, add the following code: private string getApplicationName(object application) { string name = string.Empty; name = application.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, application, null).ToString(); return name; } #region callback string Office.IRibbonExtensibility.GetCustomUI(string RibbonID) { return Properties.Resources.MyButton; }

    public void button1_click(Office.IRibbonControl control) { string appName = getApplicationName(applicationObject); switch (appName) { case "Microsoft Excel": Excel.Application oExcel = applicationObject as Excel.Application; OpenFolder(oExcel); break; case "Microsoft Word": Word.Application oWord = applicationObject as Word.Application; OpenFolder(oWord); break; case "Microsoft PowerPoint": PowerPoint.Application oPPT = applicationObject as PowerPoint.Application; OpenFolder(oPPT); break; } } private void OpenFolder(Excel.Application oExcel) { Excel.Workbook oBook = oExcel.ActiveWorkbook; string path = oBook.Path; OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = path; dialog.ShowDialog(); } private void OpenFolder(Word.Application oWord) { Word.Document oDoc = oWord.ActiveDocument; string path = oDoc.Path; OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = path; dialog.ShowDialog(); } private void OpenFolder(PowerPoint.Application oPPT) { PowerPoint.Presentation pre = oPPT.ActivePresentation; string path = pre.Path; OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = path; dialog.ShowDialog(); } #endregion }

    2.4 Now go to the Properties window of the Project -> Debug tab, set the "Start external project" to the absolute path of the office application. I set it to "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE" as the path for excel.

    2.5 Then we can debug the project, and we can see the "Show Folder" button on the Home tab. Click on it and it shows open the folder contains the current workbook. 

    3. Right click on the "MySharedAddinSetup" project, build it. After the build success, right click on the project -> go to install. This will install the add in to the computer. After finish the wizard, we can start the office project to see the "Show Folder" button. 

    Hope it helps.

    Have a nice day!

    yoyo


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us


    Monday, March 12, 2012 5:55 AM
    Moderator

All replies

  • Hi kevin,

    Welcome to the MSDN forum!

    What do you mean about "I cannot open the file folder from the office file that I'm using... "? Is it that it is difficult to find the location of the office file? 

    When we open an office file -> click "File" ->Go to the "Info" tab, it show us the absolute path of the current opened file, which of course contains the location of the file. 

    Also, there are some tips for you when we attach the file to an email:

    Tip 1. From Windows Explorer, drag the target file onto the new-email message panel. You will see it automatically attached. 

    Tip 2. When we click on "Attach File" from the new-email message window, from the "Insert File" dialog, click on the "Show the preview panel" on the upper right of the dialog, it will show us the preview of the file we select.

    Please feel free to let us know if you have any concern.

    Have a nice day!

    yoyo


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 9, 2012 7:13 AM
    Moderator
  • Hi Kevin,

    If you want to implement the demand by code, I think what you need is a Shared add-in for the Office Application, namely excel, powerpoint, doc etc.

    Please take a look at the following steps.

    1. New the Shared Add-In. 

     1.1Open Visual Studio 2010 as a Administrator (Right click on the Visual Studio 2010 Icon, go to "Run As Administrator" ). 

    1.2 In the New Project dialog, in the left "Installed Templates", expand "Other Project Types" node, select "Extensibility". In the Right panel, select "Shared Add-in".  Now in the Name textbox, type the name for the project. Here I used "MySharedAddin" as the name. Click "Ok".

    1.3 In Shared Add-in wizard, "Welcome to the Add-in Wizard", click on Next
    -> select the "Create an Add-in using Visual C#" (The default value), click on "Next"
    -> In the "Select An Application Host", check "Microsoft Word", "Microsoft  PowerPoint","Microsoft Excel". Here the addin is for word, powerpoint and excel. Click on "Next"
    -> Set the Name of the Add-in. I took "ShowFolderSharedAddin" as the input.
    ->"Choose Add-in Options", check the two checkbox. Click "Next"
    ->"Finish".

    2. The MySharedAddin Project.

    2.1 Open the Solution Explorer panel. In the Reference node, remove the Microsoft.Office.Core. 

    Right click on the "Reference", go to "Add References...". In the COM tab, add "Microsoft Excel 14.0 Object Library" "Microsoft Word 14.0 Object Library" "Microsoft PowerPoint 14.0 Object Library" as references one by one. In the COM tab, add "Microsoft Office 14.0 Object Library" (TypeLib Version 2.5) as a reference. In the .NET tab, select "System.Windows.Form" as a reference. 

    Import the namespaces in the beginning of Connect.cs :

    using Office = Microsoft.Office.Core;
    using Excel = Microsoft.Office.Interop.Excel;
    using Word = Microsoft.Office.Interop.Word;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using System.Windows.Forms;


    2.2 Add a xml file into the project. (Right click on the project -> Add -> New Item-> add a xml file named as MyButton.xml) The content of MyButton.xml is shown as following:

    <?xml version="1.0" encoding="utf-8" ?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <ribbon>
        <tabs>
          <tab idMso="TabHome">
            <group id ="group1"  label ="Folder">
              <button id ="button1" label ="Show Folder" onAction ="button1_click" size ="large" imageMso ="HappyFace"/>
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>

    Now in the Properties window of MyButton.xml, set Build Action to "Embeded Resource". 

    Click on the Properties node of the project, go to the "Resources" tab, click on the link. then drag "MyButton.xml" from solution explorer onto the Resources panel. 

    2.3 Go to the Connect.cs code. The code is shown as below:

    public class Connect : Object, Extensibility.IDTExtensibility2,Office.IRibbonExtensibility { ... private object applicationObject; private object addInInstance; // After the code, add the following code: private string getApplicationName(object application) { string name = string.Empty; name = application.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, application, null).ToString(); return name; } #region callback string Office.IRibbonExtensibility.GetCustomUI(string RibbonID) { return Properties.Resources.MyButton; }

    public void button1_click(Office.IRibbonControl control) { string appName = getApplicationName(applicationObject); switch (appName) { case "Microsoft Excel": Excel.Application oExcel = applicationObject as Excel.Application; OpenFolder(oExcel); break; case "Microsoft Word": Word.Application oWord = applicationObject as Word.Application; OpenFolder(oWord); break; case "Microsoft PowerPoint": PowerPoint.Application oPPT = applicationObject as PowerPoint.Application; OpenFolder(oPPT); break; } } private void OpenFolder(Excel.Application oExcel) { Excel.Workbook oBook = oExcel.ActiveWorkbook; string path = oBook.Path; OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = path; dialog.ShowDialog(); } private void OpenFolder(Word.Application oWord) { Word.Document oDoc = oWord.ActiveDocument; string path = oDoc.Path; OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = path; dialog.ShowDialog(); } private void OpenFolder(PowerPoint.Application oPPT) { PowerPoint.Presentation pre = oPPT.ActivePresentation; string path = pre.Path; OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = path; dialog.ShowDialog(); } #endregion }

    2.4 Now go to the Properties window of the Project -> Debug tab, set the "Start external project" to the absolute path of the office application. I set it to "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE" as the path for excel.

    2.5 Then we can debug the project, and we can see the "Show Folder" button on the Home tab. Click on it and it shows open the folder contains the current workbook. 

    3. Right click on the "MySharedAddinSetup" project, build it. After the build success, right click on the project -> go to install. This will install the add in to the computer. After finish the wizard, we can start the office project to see the "Show Folder" button. 

    Hope it helps.

    Have a nice day!

    yoyo


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us


    Monday, March 12, 2012 5:55 AM
    Moderator