Compartir a través de


ApplicationFactory.HasVstoObject(_Document) Method

Definition

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

public:
 bool HasVstoObject(Microsoft::Office::Interop::Word::_Document ^ document);
public bool HasVstoObject (Microsoft.Office.Interop.Word._Document document);
abstract member HasVstoObject : Microsoft.Office.Interop.Word._Document -> bool
Public Function HasVstoObject (document As _Document) As Boolean

Parameters

document
_Document

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

Returns

true if a Document host item has been created for the specified Document object; otherwise, false.

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 Microsoft.Office.Interop.Word.ApplicationEvents4_Event.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 or the .NET Framework 4.5.

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);
        }
    }
}
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

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.

Applies to