1. Welcome to Chandoo.org Forums. Short message for you

    Hi Guest,

    Thanks for joining Chandoo.org forums. We are here to make you awesome in Excel. Before you post your first question, please read this short introduction guide. When posting or responding to questions please remember our values at Chandoo.org are: Humility, Passion, Fun, Awesomeness, Simplicity, Sharing Remember that we have people here for whom English is not there first language and we need to allow for this in our dealings.

  2. 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


  3. 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

Discussion in 'VBA Macros' started by ThrottleWorks, Feb 13, 2019.

  1. ThrottleWorks

    ThrottleWorks Excel Ninja


    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 (vb):

    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", _
        With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
            .ParagraphFormat.Alignment = wdAlignParagraphCenter
            .Font.Size = 10 'size 10 font
       End With
    End Sub
    Last edited: Feb 13, 2019
  2. Chihiro

    Chihiro Excel Ninja

    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).

    Code (vb):
    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
    ThrottleWorks likes this.
  3. macropod

    macropod New Member

    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.
    ThrottleWorks likes this.
  4. ThrottleWorks

    ThrottleWorks Excel Ninja

    Hi @Chihiro sir, thanks a lot for the help.
    Am checking this and will revert with details.

    Have a nice day ahead. :)
  5. ThrottleWorks

    ThrottleWorks Excel Ninja

    Hi @macropod thanks a lot for the help.
    Have a nice day ahead. :)

Share This Page