Document Properties


(It gets information about custom properties) Dim oPropName As Object = typeDocProperties.InvokeMember("Item", Reflection.BindingFlags.Default Or Reflection.BindingFlags.GetProperty, Nothing, objDocumentProperties, New Object() {sPropertyName})
Dim oPropNameType As Type = oPropName.GetType
Dim val As String = oPropNameType.InvokeMember("Value", Reflection.BindingFlags.Default Or Reflection.BindingFlags.GetProperty, Nothing, oPropName, New Object() {}).ToString Return val


a strange error upgraded to Win 7 Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{2DF8D04D-5BFA-101B-BDE5-00AA0044DE52}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).



It looks like that the method doesn't work in Win 7 anymore BUT just in some specific condition (It seems it has problem when a document is being opened, otherwise it's to be working fine)


I changed the code to use 'normal' approach and it seems to be working fine.



Dim properties As Microsoft.Office.Core.DocumentProperties
properties = CType(objDocument.CustomDocumentProperties, Office.DocumentProperties)


Dim prop = properties.Cast(Of Office.DocumentProperty)().FirstOrDefault(Function(x) x.Name.ToLower.Equals(sPropertyName.ToLower))
If prop Is Nothing Then Return String.Empty Return prop.Value



    Public Class CustomDocProperties 

' Note: Object type is used instead of Office.DocumentProperties to avoid a type conversion error
        Private CustomDocProps As Object

' DocProperty code using Object
        Private ReadOnly Property DocProperty(ByVal Name As String) As Object
            <DebuggerStepThrough()> _
            Get
                Try
' Note: This is significantly faster than looping through the properties
                    Return CustomDocProps(Name.ToString)
                Catch ex As Exception
                    Return Nothing
                End Try
            End Get
        End Property

        Property Value(ByVal Name As EraCustomPropertyName) As Object
            Get
                Dim docProp As Object = DocProperty(Name)
                If docProp IsNot Nothing Then
                    Return docProp.Value
                Else
                    Return Nothing
                End If
            End Get
            Set(ByVal value As Object)
                Dim docProp As Object = DocProperty(Name)
                If docProp IsNot Nothing Then
                    docProp.Value = value ' set value
                Else
                    Add(Name, value) ' add new prop and set value
                End If
            End Set
        End Property

        Private Function Add(ByVal Name As String, ByVal Value As Object) As Object
            If TypeOf Value Is Integer OrElse TypeOf Value Is Double Then
' Numeric
                Return CustomDocProps.Add(Name.ToString, False, Core.MsoDocProperties.msoPropertyTypeNumber, Value)
            Else
' String
                Return CustomDocProps.Add(Name.ToString, False, Core.MsoDocProperties.msoPropertyTypeString, Value)
            End If
        End Function

        Sub Delete(Name As String)
            Dim docProp As Object = DocProperty(Name)
            If docProp IsNot Nothing Then
                docProp.delete()
            End If
        End Sub

        Public Sub New(ByVal WordDoc As Word.Document, Optional ByVal TemplateProperty As Boolean = False)
            If TemplateProperty Then
                CustomDocProps = WordDoc.AttachedTemplate.CustomDocumentProperties
            Else
                CustomDocProps = WordDoc.CustomDocumentProperties
            End If
        End Sub

    End Class



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext