Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-09-2021, 10:49 PM
NicoleJones NicoleJones is offline Adding Custom Document Properties Windows 10 Adding Custom Document Properties Office 2016
Novice
Adding Custom Document Properties
 
Join Date: Jan 2020
Posts: 9
NicoleJones is on a distinguished road
Default Adding Custom Document Properties

Hi



I am trying to add new document properties a new document is created off of a template, however I get run-time error "Invalid procedure call or argument". Just wondering if anyone knows where I am going wrong

Code:
Dim APS_ContractNumber As Boolean
Dim APS_DocumentCode As Boolean
Dim APS_DocumentNumber As Boolean
Dim APS_DocumentTitle As Boolean
Dim APS_DocumentType As Boolean
Dim APS_Revision As Boolean
Dim APS_RevisionDate As Boolean
Dim APS_SubjectCode As Boolean
Dim APS_UniqueIdentifier As Boolean


Private Sub Document_New()
    With ActiveDocument.CustomDocumentProperties
        .Add Name:="APS_ContractNumber", _
            LinkToContent:=True, _
            Type:=msoPropertyTypeString, _
            Value:="[Contract Number]"
        .Add Name:="APS_DocumentCode", _
            LinkToContent:=False, _
            Type:=msoPropertyTypeNumber, _
            Value:="[Document Code]"
        .Add Name:="APS_DocumentNumber", _
            LinkToContent:=False, _
            Type:=msoPropertyTypeString, _
            Value:="[Document Number]"
        .Add Name:="APS_DocumentTitle", _
            LinkToContent:=False, _
            Type:=msoPropertyTypeString, _
            Value:="[Document Title]"
        .Add Name:="APS_DocumentType", _
            LinkToContent:=False, _
            Type:=msoPropertyTypeString, _
            Value:="[DDD]"
        .Add Name:="APS_Revision", _
            LinkToContent:=False, _
            Type:=msoPropertyTypeString, _
            Value:="[RR]"
         .Add Name:="APS_RevisionDate", _
            LinkToContent:=False, _
            Type:=msoPropertyTypeDate, _
            Value:="[01/01/2000]"
        .Add Name:="APS_SubjectCode", _
            LinkToContent:=False, _
            Type:=msoPropertyTypeString, _
            Value:="[Subject Code]"
         .Add Name:="APS_UniqueIdentifier", _
            LinkToContent:=False, _
            Type:=msoPropertyTypeString, _
            Value:="[XXX]"
    End With

End Sub
Reply With Quote
  #2  
Old 03-09-2021, 11:23 PM
Guessed's Avatar
Guessed Guessed is offline Adding Custom Document Properties Windows 10 Adding Custom Document Properties Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The first one fails because you can't link to content without telling it which content. Change that to False. It may also fail if the CustomDocumentProperty already exists so you should probably be testing that before adding.

The second one will fail because you told it the value would be a number but you provided a string.

You will also find the date format won't work because you provided a string rather than a valid date. For that one, try Value:=#1/1/2000#
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 03-09-2021, 11:46 PM
gmayor's Avatar
gmayor gmayor is offline Adding Custom Document Properties Windows 10 Adding Custom Document Properties Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The DIM statements are not required for the macro. As for the rest,you can't put a date in a date field that is not a date, nor a number in a number field that is not a number.
I would suggest using error trapping to avoid crashes while testing the code.

Code:
Option Explicit

Private Sub Document_New()
    AddProp "APS_ContractNumber", False, 4, "[Contract Number]"
    AddProp "APS_DocumentCode", False, 4, "[Document Code]"
    AddProp "APS_DocumentNumber", False, 4, "[Document Number]"
    AddProp "APS_DocumentTitle", False, 4, "[Document Title]"
    AddProp "APS_DocumentType", False, 4, "[Document Type]"
    AddProp "APS_Revision", False, 4, "[RR]"
    AddProp "APS_RevisionDate", False, 3, "01/01/2000"
    AddProp "APS_SubjectCode", False, 4, "[Subject Code]"
    AddProp "APS_UniqueIdentifier", False, 4, "[XXX]"
End Sub

Private Sub AddProp(sName As String, bLink As Boolean, lType As Long, sValue As String)
Dim oProp As DocumentProperty
    With ActiveDocument
        For Each oProp In .CustomDocumentProperties
            If oProp.Name = sName Then
                oProp.Value = sValue
                GoTo lbl_Exit
            End If
        Next oProp
        .CustomDocumentProperties.Add _
                Name:=sName, _
                LinkToContent:=bLink, _
                Type:=lType, _
                Value:=sValue
    End With
lbl_Exit:
    Set oProp = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 03-10-2021, 08:43 PM
NicoleJones NicoleJones is offline Adding Custom Document Properties Windows 10 Adding Custom Document Properties Office 2016
Novice
Adding Custom Document Properties
 
Join Date: Jan 2020
Posts: 9
NicoleJones is on a distinguished road
Default

Thank you gmayor

Its all fixed now thanks to your help.

Cheers
Nicole
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Custom Document Properties Create Custom Document Properties with Content Control kschmidt Word VBA 7 02-04-2019 03:09 PM
Add custom doc properties in "Doc properties control" eazysnatch Word 2 09-13-2017 08:08 PM
Adding Custom Document Properties Updating Document Properties without using advanced properties dialogue thedr9wningman Word VBA 3 01-20-2014 05:56 PM
Custom Properties b-baker Word 1 03-01-2012 01:15 AM
Adding Custom Document Properties Add custom document properties into document NicBodkin Word 8 05-05-2011 09:09 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:07 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft