Microsoft KB Archive/212618

From BetaArchive Wiki
Knowledge Base


Article ID: 212618

Article Last Modified on 6/17/2005



APPLIES TO

  • Microsoft Word 2000 Standard Edition



This article was previously published under Q212618


SYMPTOMS

When you use Visual Basic for Applications to create a new custom document property, the following error message may appear:

Run-time error '-2147467259(80004005):
Automation error

CAUSE

This error is returned if the custom document property you are creating already exists.

WORKAROUND

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To avoid this error, check the Custom Documents Property collection to see whether the document property already exists. The following sample Visual Basic for Applications macros demonstrate how this can be accomplished.

This first macro calls the SetProp subroutine. It is a separate macro so that you can insert the single command line into a larger macro.

Sub SetCustomPropertyName()
   ' **************************************************
   ' This SubRoutine passes the Custom Property Name,
   ' Value, and Property Type to the SetProp subroutine.
   ' **************************************************
   ' Set the custom property "MyCustomPropertyName" equal
   ' to "MyCustomValue". The msoPropertyTypeString constant
   ' specifies the type of property, and must be included.
   SetProp "MyCustomPropertyName", "MyCustomValue", _
   msoPropertyTypeString
End Sub
                


For more information about the Type property, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Type Property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Sub SetProp(CDPName As String, CDPValue As Variant, Optional _
CDPType As Long)
   ' ***********************************************
   ' The SetProp routine checks to see whether the Custom
   ' Document Property pre-exists. If it exists, then
   ' it adds the new value. If it does not exist,
   ' it creates the new property and adds the new value.
   ' ***********************************************
   ' Make sure the optional argument CDPType is set.
   ' If it is missing, make it a string value.
   Dim oCDP, oProp, msg
   If IsMissing(CDPType) Then
      CDPType = msoPropertyTypeString
   End If
   Set oCDP = ActiveDocument.CustomDocumentProperties
   ' Compare each custom document property to the
   ' property you want to create to see whether it exists.
   For Each oProp In oCDP
      ' If the Custom Property exists...
      If oProp.Name = CDPName Then
         With oProp
            ' ...the custom property Type you are setting
            ' must match the pre-existing custom property.
            If .Type <> CDPType Then
               msg = "The custom property types do not match."
               msg = msg + " Custom property not set."
               MsgBox msg
               ' End the routine.
               Exit Sub
            End If
            .LinkToContent = False
            ' Set the new value.
            .Value = CDPValue
         End With
         ' A match was found, so exit the routine.
         Exit Sub
      End If
   Next oProp
   ' No match was found. Create a new property and value.
   oCDP.Add Name:=CDPName, Value:=CDPValue, Type:=CDPType, _
   LinkToContent:=False
End Sub
                


For more information about setting custom properties, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type CustomDocumentProperties in the Office Assistant or the Answer Wizard, and then click Search to view the topic.


MORE INFORMATION

For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

212536 OFF2000: How to Run Sample Code from Knowledge Base Articles




REFERENCES

For additional information about getting help with Visual Basic for Applications, click the article number below to view the article in the Microsoft Knowledge Base:

226118 OFF2000: Programming Resources for Visual Basic for Applications



Additional query words: vb vba vbe

Keywords: kbmacroexample kbprb kbdtacode KB212618