Skip to content

Latest commit

 

History

History
53 lines (40 loc) · 2.39 KB

how-to-programmatically-open-existing-documents.md

File metadata and controls

53 lines (40 loc) · 2.39 KB
title description ms.date ms.topic dev_langs helpviewer_keywords author ms.author manager ms.subservice
Programmatically open existing documents
Open an existing Microsoft Word document by using a fully qualified path and file name programmatically from Visual Basic or C# in Visual Studio.
02/02/2017
how-to
VB
CSharp
documents [Office development in Visual Studio], opening
Word [Office development in Visual Studio], opening documents
John-Hart
johnhart
mijacobs
office-development

Programmatically open existing documents

The xref:Microsoft.Office.Interop.Word.Documents.Open%2A method opens the existing Microsoft Office Word document specified by a fully qualified path and file name. This method returns a xref:Microsoft.Office.Interop.Word.Document that represents the opened document.

[!INCLUDEappliesto_wdalldocapp]

To open a document

  • Call the xref:Microsoft.Office.Interop.Word.Documents.Open%2A method of the xref:Microsoft.Office.Interop.Word.Documents collection and supply a path to the document.

    :::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreWordAutomationCS/ThisDocument.cs" id="Snippet5":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/Trin_VstcoreWordAutomationVB/ThisDocument.vb" id="Snippet5":::

To open a document as read-only

  • Call the xref:Microsoft.Office.Interop.Word.Documents.Open%2A method, supply a path to the document, and set the ReadOnly argument to True in the method call.

    :::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreWordAutomationCS/ThisDocument.cs" id="Snippet6":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/Trin_VstcoreWordAutomationVB/ThisDocument.vb" id="Snippet6":::

Compile the code

This code example requires the following:

  • A document named NewDocument.doc must exist in a directory named Test on drive C.

Related content