• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Not able to add “CustomDocumentProperties.Add Name’ in word document with Excel VBA

ThrottleWorks

Excel Ninja
Hi,

I am using below mentioned code to add “CustomDocumentProperties.Add Name”.

My code is skipping “objWordApp.ActiveDocument.CustomDocumentProperties.Add Name:="YourName", LinkToContent:=False, Value:="thename", _
Type:=msoPropertyTypeString” line.

As soon as code hits this line, macro exits this procedure and returns to parent procedure.
Till this line macro is working fine.
Word document is open.
I can see the word file name by using Control + G.


I am not able to understand why this is happening.
'InsertConfidentialityFooter' is called by another module.
'objWordApp' is defined as public variable in parent procedure.

Can anyone please help me in this.

Code:
Sub InsertConfidentialityFooter()
    'SOURCE: www.TheSpreadsheetGuru.com
    Dim flag As Variant
    Dim flag1 As Variant
    Dim prop As Variant
    Dim Author As Variant
    Dim Sht As Worksheet
    flag = False
    flag1 = False
    For Each prop In objWordApp.ActiveDocument.CustomDocumentProperties
        If prop.Name = "DummyText1" Then
            flag = True
        End If
        If prop.Name = "DummyText1" Then
            flag1 = True
        End If
    Next prop
    objWordApp.ActiveDocument.CustomDocumentProperties.Add Name:="YourName", LinkToContent:=False, Value:="thename", _
    Type:=msoPropertyTypeString
    With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
        .Text = "CONFIDENTIAL - INTERNAL USE ONLY"
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .Font.Size = 10 'size 10 font
    End With
End Sub
 
Last edited:
I don't see where objWordApp is declared in your code.

Make sure you declare it as Object and also Set it to MS Word instance (typically should check if it's running already or not).

Ex:
Code:
Dim objWordApp As Object
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
    Set objWordApp = CreateObject("Word.Application")
    objWordApp.Visible = True
End If
'Do something with objWordApp
 
If the property concerned has already been added, trying to add it again will cause an error. In any event, if you use a template with the property already defined, you won't need to add it with code when you create a new document from it.
 
Back
Top