Udostępnij za pośrednictwem


ApplicationFactory.GetVstoObject(_Document) Method

Definition

Returns a Document host item that extends the functionality of the specified native document object.

public:
 Microsoft::Office::Tools::Word::Document ^ GetVstoObject(Microsoft::Office::Interop::Word::_Document ^ document);
public Microsoft.Office.Tools.Word.Document GetVstoObject (Microsoft.Office.Interop.Word._Document document);
abstract member GetVstoObject : Microsoft.Office.Interop.Word._Document -> Microsoft.Office.Tools.Word.Document
Public Function GetVstoObject (document As _Document) As Document

Parameters

document
_Document

The native document object to extend. Although this parameter is of type _Document, you typically pass a Document object to this method.

Returns

A host item that extends the functionality of the native document object.

Examples

The following code example creates a Microsoft.Office.Tools.Word.Document host item for the active Word document. The example then adds a PlainTextContentControl to the document. To use this code, run it from the ThisAddIn class in a Word add-in project that targets the .NET Framework 4 or the .NET Framework 4.5.

Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);

vstoDoc.Paragraphs.Last.Range.InsertParagraphAfter();
PlainTextContentControl myPlainTextControl =
    vstoDoc.Controls.AddPlainTextContentControl(
    vstoDoc.Paragraphs.Last.Range,
    "myPlainTextControl");
myPlainTextControl.PlaceholderText = "Insert first name";
Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)

vstoDoc.Paragraphs.Last.Range.InsertParagraphAfter()
Dim myPlainTextControl As PlainTextContentControl = _
    vstoDoc.Controls.AddPlainTextContentControl( _
    vstoDoc.Paragraphs.Last.Range, _
    "myPlainTextControl")
myPlainTextControl.PlaceholderText = "Insert first name"

Remarks

Call this method in an application-level add-in to customize any document that is open in Word. This method generates a new Microsoft.Office.Tools.Word.Document object if no such object has already been generated. Subsequent calls to this method return the cached instance of the existing Microsoft.Office.Tools.Word.Document object. For more information, see Extending Word Documents and Excel Workbooks in VSTO Add-ins at Run Time.

Note

VSTO follows the “Automation Security” Office group policy setting when it is set to “Macros enabled” or “Disable Macros by default”. However, VSTO will always allow automation to run if the setting is set to “Use application macro security level”, regardless of the application setting. To prevent VSTO from allowing automation to run, choose the “Disable Macros by default” setting.

The document parameter is of type Microsoft.Office.Interop.Word._Document, which is the parent interface of Microsoft.Office.Interop.Word.Document. Therefore, this method can accept objects of both types: Microsoft.Office.Interop.Word._Document and Microsoft.Office.Interop.Word.Document. Typically, when you reference a Word document, you use a Microsoft.Office.Interop.Word.Document.

Applies to