#1
|
|||
|
|||
Update Custom Document Property in Template
I am having problems finding just where to put code that will update the value in a Custom Document Property in a template file when that template file is being used to create a new document.
I have tried opening the template file with VBA code, modifying the value in the custom document property and saving the changes to the template file but that did not work. I have code that will write to the property of the template when I step through it in the template file, but when the code is run when the new document is being created, it does not update the value in the custom property. It also does not cause an error. Any assistance is appreciated. Byron |
#2
|
|||
|
|||
Sub AutoNew()
'ThisDocument identifies the document\template that contains the running code. ThisDocument.CustomDocumentProperties("Telephone number").Value = "123-456-7890" End If |
#3
|
|||
|
|||
Greg,
Thanks for the reply. I see you used the AutoNew() procedure for placement of your code. Was there some specific reason for you choosing this procedure? Is is correct to assume that as long as the code is referring to "ThisDocument" it should write the value to the Cusstom Document Property? Byron |
#4
|
|||
|
|||
I you could use another procedure if you want. I was just trying to show that ThisDocument refers to the document (or template) that contains the code.
For example: Debug.Print ThisDocument.Name is going to return the name of your template. Not the name of the actual document you may be doing a process on. |
#5
|
|||
|
|||
Here is the line of code I am using in the "Private Sub Document_New()" procedure.
Quote:
Is there some way that I can stop the code as it is being run when creating the new document so I can step through the code? Byron |
#6
|
|||
|
|||
Is this really the name of the property:
"RandomNumbe r" The following creates a new document based on the template and places a new random number in both the template and the new document just created. Code:
Sub AutoNew() Dim lngNewRandNumber Randomize lngNewRandNumber = Int((1000 * Rnd) + 1) 'Updates the template property. ThisDocument.CustomDocumentProperties("RandomNumber").Value = lngNewRandNumber 'Updates the document you have just created. ActiveDocument.CustomDocumentProperties("RandomNumber").Value = lngNewRandNumber End Sub |
#7
|
|||
|
|||
No, Greg, that is just old man typeo. As you figured out the property name is "RandomNumber".
I have been able to test a little farther and I have found the the Quote:
I have even tried adding a ThisDocument.Save to the code and that did not help. I will give your code a try. Can you tell me when, in relation to the Document_New procedure when the AutoNew procedure is run? Byron |
#8
|
|||
|
|||
Ok, I see what you are talking about. But, I really don't know what your end goal is.
AutoNew fires before document_new. You can see that your self by putting different message boxes in each. Try this: Code:
Sub AutoNew() Dim lngNewRandNumber Dim oDoc As Document MsgBox "AN" Randomize lngNewRandNumber = Int((1000 * Rnd) + 1) MsgBox ThisDocument.CustomDocumentProperties("RandomNumber").Value ThisDocument.CustomDocumentProperties("RandomNumber").Value = lngNewRandNumber Set oDoc = ActiveDocument.AttachedTemplate.OpenAsDocument oDoc.CustomDocumentProperties("RandomNumber").Value = lngNewRandNumber oDoc.Close wdSaveChanges ActiveDocument.CustomDocumentProperties("RandomNumber").Value = lngNewRandNumber MsgBox ThisDocument.CustomDocumentProperties("RandomNumber").Value End Sub |
#9
|
|||
|
|||
Yes, I did that. I am still going to continue to play around with this. I am just not able to get the new random number to remain as the value in the property when the template file is opened again.
Thanks for all your help. Byron |
#10
|
|||
|
|||
Greg,
I just wanted to get back with you and let you know how much I really appreciate you assistance. I was able to use some of your code to accomplish exactly what I wanted to do. Just FYI, my goal was to be able to protect the use of a very custom template that I have been developing for a client. This client wanted to be able to allow multiple users to use this template but not to allow any user to be able to share or give the template away to anyone else. The process works like this; a unique (incrementing) serial number is assigned to each copy of the template and a random number is then generated that is also assigned to the template. These two unique values are written to custom document properties in the template and also written to a table in a hosted SQL database in the cloud. Each time the user opens or uses the template, code in the templated immediately checks to see if the serial number stored in the custom document property can be found. If the matching serial number cannot be found the user is informed that the current copy of the template cannot be verified as a valid registered copy of the template and will be closed. The template closes without creating a new document. If the serial number is found to be valid, the code then checks to see if the unique random number stored in the custom document property is equal to the random number saved to the cloud database record. Again, if the random numbers are found and match, all is just fine, but if the random numberes do not match, the user is informed and the template will not create the new document. Because the unigue random number is created and updated in the template and the database each time the template is used the sharing of the template is prevented because the two random numbers will not match. If the template is found to be registered, not shared and actually creates the new document the code immediately creates a new random number for that copy of the template, updates the custom document property in the template file and updates the field in the SQL database in the cloud with the same random number so the entire process can be performed again the next time the template is used. If a registered user shares their copy of the template and the individual they gave it to uses it before the registered user uses it again, the registered user's copy will not longer work when they try to use it because the rendom numbers do not work. The copy they gave away will continue to work. But this process will at least cause only one copy to be a working copy. By working with the registered user we can reactivate their copy to be the registered copy and at that point the copy they gave away will no longer work. We can also let them know that they are not allowed to give a copy to anyone else. Just thought I would share what I have done to protect the use of the template. Thanks again for your assistance. Byron |
#11
|
|||
|
|||
Byron,
Glad I could help. Good luck with the project. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Can you make a document automatically update changes styles template? | New Daddy | Word | 7 | 02-24-2017 03:52 PM |
Insert image based on document custom property | anandyrh | Word | 1 | 08-14-2013 12:08 AM |
Custom Document Property Lost From Template | bhaughey | Word | 8 | 03-05-2013 02:11 PM |
QuickParts - custom document property | untttt | Word | 2 | 06-09-2011 05:24 PM |
Update Document Template when Saving the Document File | bjbercaw | Word | 3 | 11-16-2010 02:03 PM |