Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-18-2014, 02:07 PM
Byron Polk Byron Polk is offline Update Custom Document Property in Template Windows 7 64bit Update Custom Document Property in Template Office 2010 32bit
Novice
Update Custom Document Property in Template
 
Join Date: Jul 2014
Location: Whitehouse, TX
Posts: 14
Byron Polk is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 08-18-2014, 05:54 PM
gmaxey gmaxey is offline Update Custom Document Property in Template Windows 7 32bit Update Custom Document Property in Template Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Sub AutoNew()
'ThisDocument identifies the document\template that contains the running code.
ThisDocument.CustomDocumentProperties("Telephone number").Value = "123-456-7890"
End If
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 08-18-2014, 05:59 PM
Byron Polk Byron Polk is offline Update Custom Document Property in Template Windows 7 64bit Update Custom Document Property in Template Office 2010 32bit
Novice
Update Custom Document Property in Template
 
Join Date: Jul 2014
Location: Whitehouse, TX
Posts: 14
Byron Polk is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 08-18-2014, 06:06 PM
gmaxey gmaxey is offline Update Custom Document Property in Template Windows 7 32bit Update Custom Document Property in Template Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 08-18-2014, 06:31 PM
Byron Polk Byron Polk is offline Update Custom Document Property in Template Windows 7 64bit Update Custom Document Property in Template Office 2010 32bit
Novice
Update Custom Document Property in Template
 
Join Date: Jul 2014
Location: Whitehouse, TX
Posts: 14
Byron Polk is on a distinguished road
Default

Here is the line of code I am using in the "Private Sub Document_New()" procedure.

Quote:
lngNewRandNumber = RandomNumbers(10000, 20000)
ThisDocument.CustomDocumentProperties("RandomNumbe r").Value = lngNewRandNumber
This code is not updating the custom document property "RandomNumber".

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
Reply With Quote
  #6  
Old 08-18-2014, 07:06 PM
gmaxey gmaxey is offline Update Custom Document Property in Template Windows 7 32bit Update Custom Document Property in Template Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 08-18-2014, 07:15 PM
Byron Polk Byron Polk is offline Update Custom Document Property in Template Windows 7 64bit Update Custom Document Property in Template Office 2010 32bit
Novice
Update Custom Document Property in Template
 
Join Date: Jul 2014
Location: Whitehouse, TX
Posts: 14
Byron Polk is on a distinguished road
Default

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:
ThisDocument.CustomDocumentProperties("RandomNumbe r").Value = lngNewRandNumber
line is updating the value in the property, however, when I then close the document that has been created and reopen the template file, the previous random number is still shown as the value in the "RandomNumber" property.

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
Reply With Quote
  #8  
Old 08-18-2014, 07:33 PM
gmaxey gmaxey is offline Update Custom Document Property in Template Windows 7 32bit Update Custom Document Property in Template Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 08-18-2014, 07:58 PM
Byron Polk Byron Polk is offline Update Custom Document Property in Template Windows 7 64bit Update Custom Document Property in Template Office 2010 32bit
Novice
Update Custom Document Property in Template
 
Join Date: Jul 2014
Location: Whitehouse, TX
Posts: 14
Byron Polk is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 08-19-2014, 06:15 AM
Byron Polk Byron Polk is offline Update Custom Document Property in Template Windows 7 64bit Update Custom Document Property in Template Office 2010 32bit
Novice
Update Custom Document Property in Template
 
Join Date: Jul 2014
Location: Whitehouse, TX
Posts: 14
Byron Polk is on a distinguished road
Default

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
Reply With Quote
  #11  
Old 08-19-2014, 07:19 AM
gmaxey gmaxey is offline Update Custom Document Property in Template Windows 7 32bit Update Custom Document Property in Template Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Byron,

Glad I could help. Good luck with the project.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Update Custom Document Property in Template Can you make a document automatically update changes styles template? New Daddy Word 7 02-24-2017 03:52 PM
Update Custom Document Property in Template 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
Update Custom Document Property in Template 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

Other Forums: Access Forums

All times are GMT -7. The time now is 05:38 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