Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
95 lines (64 sloc) 5.14 KB
title ms.date ms.topic dev_langs helpviewer_keywords author ms.author manager ms.workload
Populate Word tables with document properties programmatically
02/02/2017
conceptual
VB
CSharp
document properties, inserting in Word tables
documents [Office development in Visual Studio], document properties
John-Hart
johnhart
jillfra
office

How to: Programmatically populate Word tables with document properties

The following example creates a Microsoft Office Word table at the top of the document and populates it with the properties of the host document.

[!INCLUDEappliesto_wdalldocapp]

Populate tables in a document-level customization

To create a table and populate it with document properties

  1. Set the range to the top of the document.

    [!code-vbTrin_VstcoreWordAutomation#90] [!code-csharpTrin_VstcoreWordAutomation#90]

  2. Insert a title for the table and include paragraph marks.

    [!code-vbTrin_VstcoreWordAutomation#91] [!code-csharpTrin_VstcoreWordAutomation#91]

  3. Add the table to the document at the range.

    [!code-vbTrin_VstcoreWordAutomation#92] [!code-csharpTrin_VstcoreWordAutomation#92]

  4. Format the table and apply a style.

    [!code-vbTrin_VstcoreWordAutomation#93] [!code-csharpTrin_VstcoreWordAutomation#93]

  5. Insert the document properties into cells.

    [!code-vbTrin_VstcoreWordAutomation#94] [!code-csharpTrin_VstcoreWordAutomation#94]

    The following example shows the complete procedure. To use this code, run it from the ThisDocument class in your project.

    [!code-vbTrin_VstcoreWordAutomation#89] [!code-csharpTrin_VstcoreWordAutomation#89]

Populate tables in a VSTO Add-in

To create a table and populate it with document properties

  1. Set the range to the top of the document.

    [!code-vbTrin_VstcoreWordAutomationAddIn#90] [!code-csharpTrin_VstcoreWordAutomationAddIn#90]

  2. Insert a title for the table and include paragraph marks.

    [!code-vbTrin_VstcoreWordAutomationAddIn#91] [!code-csharpTrin_VstcoreWordAutomationAddIn#91]

  3. Add the table to the document at the range.

    [!code-vbTrin_VstcoreWordAutomationAddIn#92] [!code-csharpTrin_VstcoreWordAutomationAddIn#92]

  4. Format the table and apply a style.

    [!code-vbTrin_VstcoreWordAutomationAddIn#93] [!code-csharpTrin_VstcoreWordAutomationAddIn#93]

  5. Insert the document properties into cells.

    [!code-vbTrin_VstcoreWordAutomationAddIn#94] [!code-csharpTrin_VstcoreWordAutomationAddIn#94]

    The following example shows the complete procedure. To use this code, run it from the ThisAddIn class in your project.

    [!code-vbTrin_VstcoreWordAutomationAddIn#89] [!code-csharpTrin_VstcoreWordAutomationAddIn#89]

See also

You can’t perform that action at this time.