Share via


ApplicationFactory.HasVstoObject Method

Returns a value that indicates whether a Microsoft.Office.Tools.Word.Document host item has been created for the specified native document object.

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word (in Microsoft.Office.Tools.Word.dll)

Syntax

'Declaration
Function HasVstoObject ( _
    document As _Document _
) As Boolean
bool HasVstoObject(
    _Document document
)

Parameters

Return Value

Type: System.Boolean
true if a Microsoft.Office.Tools.Word.Document host item has been created for the specified Microsoft.Office.Interop.Word.Document object; otherwise, false.

Remarks

You can call this method in an application-level add-in to test for the existence of managed controls that you want to persist before closing or saving the Word document. For a sample that demonstrates how to persist controls in a Word document, see Word Add-In Dynamic Controls Sample.

Note

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.

Examples

The following code example checks whether the current document has an associated host item and, if so, it gets the host item. If managed controls exist in the document, the example displays a warning message that informs the user that the managed controls will not be persisted when saving the document. This code example uses the event handler of the DocumentBeforeSave event to perform the check. To use this code, run it from the ThisAddIn class in a Word add-in project that targets the .NET Framework 4.

Private Sub Application_DocumentBeforeSave( _
    ByVal Doc As Microsoft.Office.Interop.Word.Document, _
    ByRef SaveAsUI As Boolean, _
    ByRef Cancel As Boolean) Handles Application.DocumentBeforeSave

    If Globals.Factory.HasVstoObject(Doc) = True Then
        Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Doc)
        If vstoDoc.Controls.Count > 0 Then
            System.Windows.Forms.MessageBox.Show( _
                "The VSTO controls are not persisted when you save this document.", _
                "Controls Persistence", _
                System.Windows.Forms.MessageBoxButtons.OK, _
                System.Windows.Forms.MessageBoxIcon.Warning)
        End If
    End If
End Sub
void Application_DocumentBeforeSave(
    Microsoft.Office.Interop.Word.Document Doc, ref bool SaveAsUI, 
    ref bool Cancel)
{
    if (Globals.Factory.HasVstoObject(Doc) == true)
    {
        Document vstoDoc = Globals.Factory.GetVstoObject(Doc);
        if (vstoDoc.Controls.Count > 0)
        {
            System.Windows.Forms.MessageBox.Show(
                "The VSTO controls are not persisted when you save this document.",
                "Controls Persistence",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Warning);
        }
    }
}

.NET Framework Security

See Also

Reference

ApplicationFactory Interface

Microsoft.Office.Tools.Word Namespace

Other Resources

Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time

Getting Extended Objects from Native Office Objects in Document-Level Customizations

Word Add-In Dynamic Controls Sample