Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

HOW TO: Determine Office Document and Managed Code Extension Paths at Run Time


View products that this article applies to.

Summary

This article describes how to programmatically determine the path of a Microsoft Office Word document or a Microsoft Office Excel workbook, and the path of the associated Microsoft .NET Framework assembly (managed code extension).

↑ Back to the top


More information

If you use System.IO.Path.GetFullPath(".") from a managed code extension to determine the path of the assembly, you may receive inconsistent results. Because the value of GetFullPath(".") depends on the location of the starting process, you might receive different results when you debug the solution in Visual Studio .NET 2003 than you receive at run time. This article describes how to receive consistent path information for both Word documents and Excel Workbooks and their associated assemblies.

Microsoft Office Word

  1. Create a new Microsoft Office System project:
    1. Start Visual Studio .NET 2003.
    2. On the File menu, point to New, and then click Project.
    3. Under Project Types, expand Microsoft Office System Projects, and then click Visual Basic Projects.
    4. Under Templates, click Word Document. Name the new project PathInformationWD. Click OK.

      The Microsoft Office Project Wizard appears.
    5. In the Microsoft Office Project Wizard, click Finish.
  2. In the new project, add the following code to the ThisDocument_Open event handler:
    	Dim asm As System.Reflection.Assembly
    	asm = System.Reflection.Assembly.GetExecutingAssembly()
            MsgBox("The location of the assembly: " & asm.CodeBase & vbCrLf & vbCrLf & _
    		"The location of the document: " & ThisDocument.Path & "\" & ThisDocument.Name)
  3. Press F5 to build and then run the project.
  4. When the document opens in Word, a message box appears. Verify the path information for the .NET Framework assembly and the Word document, and then click OK to close the message box.
  5. Quit Word.
  6. In Windows Explorer, double-click the Word document PathInformationWD.doc to open the document in Word.

    Note This step tests the project at run time.
  7. Verify that the same path information appears in the message box that appeared when you debugged the project in Visual Studio .NET 2003.

Microsoft Office Excel

  1. Create a new Microsoft Office System project:
    1. Start Visual Studio .NET 2003.
    2. On the File menu, point to New, and then click Project.
    3. Under Project Types, expand Microsoft Office System Projects, and then click Visual Basic Projects.
    4. Under Templates, click Excel Workbook. Name the new project PathInformationXL. Click OK.

      The Microsoft Office Project Wizard appears.
    5. In the Microsoft Office Project Wizard, click Finish.
  2. In the new project, add the following code to the ThisWorkbook_Open event handler:
            Dim asm As System.Reflection.Assembly
    	asm = System.Reflection.Assembly.GetExecutingAssembly()
            MsgBox("The location of the assembly: " & asm.CodeBase & vbCrLf & vbCrLf & _
    		"The location of the Workbook: " & ThisWorkbook.Path & "\" & ThisWorkbook.Name)
  3. Press F5 to build and then run the project.
  4. When the workbook opens in Excel, a message box appears. Verify the path information for the .NET Framework assembly and the Excel workbook, and then click OK to close the message box.
  5. Quit Excel.
  6. In Windows Explorer, double-click the Excel workbook PathInformationXL.xls to open it in Excel.

    Note This step tests the project at run time.
  7. Verify that the same path information appears in the message box that appeared when you debugged the project in Visual Studio .NET 2003.

↑ Back to the top


Keywords: KB824000, kbhowto, kbofficeauto, kbpia

↑ Back to the top

Article Info
Article ID : 824000
Revision : 6
Created on : 2/3/2006
Published on : 2/3/2006
Exists online : False
Views : 413