VB6 - word reference: compile error: user-defined type not defined

Viewed 146

When trying to compile my VB6 project I get the as mentioned error. The line where it stops is:

Dim p as DocumentProperty

I checked the references:

  • Visual Basic Foor Applications, Visual Basic runtime objects and procedures, Visual Basic objects and procedures, OLE Autoomation, Microsoft Word 16.0 Object Library (and Microsoft Outlook 16.0 Library for sending results to Outlook)

So I don't have an idea what more references I could be missing to be able to use the Word DocumentProperty?

1 Answers

This looks very similar to a question I just tried to answer that had an Excel VBA tag

Basically same answer - use Variant for the data type instead of using the Word object defined type

The key is to declare the properties variables as Variants.

Here's my example of looping through all properties and trapping for value errors when they're not defined.

Dim wdApp       As Word.Application
Dim wdDoc       As Word.Document
Dim wdProp      As Variant
Dim wdProps     As Variant
  
Set wdApp = GetObject(, "Word.Application")
Set wdDoc = wdApp.ActiveDocument

Set wdProps = wdDoc.BuiltInDocumentProperties
wdProp = wdDoc.BuiltInDocumentProperties.Item(1)

Debug.Print wdProps.Count

On Error Resume Next

For Each wdProp In wdProps
    Debug.Print wdProp.Name
    Debug.Print wdProp.Value
    If Err.Number <> 0 Then
        MsgBox "ERROR: No Value For Property: " & wdProp.Name
        Err.Clear
    End If
Next wdProp
Related