MS Office Forum / Word / Programming / May 2006
custom fields
|
|
Thread rating:  |
Jan Kucera - 07 May 2006 13:20 GMT Hi, can I define my own fields or is there any field which will call VBA function and display result when actualized?
Thanks, Jan.
Jonathan West - 07 May 2006 13:31 GMT > Hi, > can I define my own fields or is there any field which will call VBA > function and display result when actualized? > > Thanks, Jan. Unfortunately not.
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Jan Kucera - 07 May 2006 13:38 GMT Well... then how can I include some piece of text with "readonly" content in the document?
>> Hi, >> can I define my own fields or is there any field which will call VBA [quoted text clipped - 3 lines] > > Unfortunately not. Jonathan West - 07 May 2006 13:47 GMT > Well... then how can I include some piece of text with "readonly" content > in the document? How readonly do you want it? Proof against accidental change, or proof against malicious modification?
Also which version of Office are you using/supporting?
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Jan Kucera - 07 May 2006 14:05 GMT As a replacement of my field. If I eg. choose {#!myfieldname!#} or whatever you want I can then replace the text by macro myself, behaving as field.
Supporting what needed. If it can't be done in older than 2003, then 2003. However all code I have now works in 2000, havn't tried 97 yet.
Thanks.
>> Well... then how can I include some piece of text with "readonly" content >> in the document? [quoted text clipped - 3 lines] > > Also which version of Office are you using/supporting? Jonathan West - 07 May 2006 14:14 GMT It sounds as if you could use a DOCPROPERTY or DOCVARIABLE field, and then change the value of the property or document variable that the field refers to. But that doesn't prevent the user from deleting the field or replacing it with plain text.
If that isn't the solution you need, could you explain a bit more about what you are trying to achieve. Forget about how you are trying to achieve, just explain the overall objective. There may be an entirely different approach that would work better
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
> As a replacement of my field. If I eg. choose {#!myfieldname!#} or > whatever you want I can then replace the text by macro myself, behaving as [quoted text clipped - 12 lines] >> >> Also which version of Office are you using/supporting? Jan Kucera - 07 May 2006 14:39 GMT I'm afraid docproperty or docvariable aren't as flexilble as I need. Well, I'll try to explain.
I have custom menu, which lists all of my fields, which can be included into the document. This can't be changed, I was said to let access them through menu.
User is creating template - If user clicks an item in the menu, say myfield1, some type of "field" is expected to be inserted into the document at cursor position. This should behave as regular field - however in the template there is no logic in evaluating it.
Then the template is stored on the server. User can now create documents, using this template. When he creates new document based on the template, all fields need to be evaluated with the context of current server state.
Whether the fields will be evaluated on the server side and sent as a plain text to the client or whether the user should still have the possibility to include additional fields (now automatically evaluated) into the document depends how complicated will be to implement it, so no requirements on this.
If it will be the simpler solution, I just need the user to work with my fields as with regular fields - as one piece, to be not able to edit the contents of them.
> It sounds as if you could use a DOCPROPERTY or DOCVARIABLE field, and then > change the value of the property or document variable that the field [quoted text clipped - 22 lines] >>> >>> Also which version of Office are you using/supporting? Jonathan West - 07 May 2006 14:58 GMT > I'm afraid docproperty or docvariable aren't as flexilble as I need. Well, > I'll try to explain. [quoted text clipped - 8 lines] > behave as regular field - however in the template there is no logic in > evaluating it. What (if anything) should be displayed at that point?
> Then the template is stored on the server. User can now create documents, > using this template. When he creates new document based on the template, > all fields need to be evaluated with the context of current server state. What aspects of the current server state are evaluated?
What means does the user's PC have for obtaining the information about the current server state?
> Whether the fields will be evaluated on the server side and sent as a > plain text to the client or whether the user should still have the [quoted text clipped - 5 lines] > fields as with regular fields - as one piece, to be not able to edit the > contents of them. That brings me back to my earlier question - how secure do you want this to be. Do you want it to be proof against accidental changes or against deliberate attempts by the user to change or remove the field?
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Jan Kucera - 07 May 2006 15:33 GMT >> -however in the template there is no logic in evaluating it. > What (if anything) should be displayed at that point? As in the Word, the field code. Like {myfield1}. In the document (in case the more complicated solution when user can add more fields not included by template), it should be switchable between field code and value. In the template, switching to value could return empty strings or leave the field code displayed, no requirements on this.
>> all fields need to be evaluated with the context of current server state. > What aspects of the current server state are evaluated? Actually session dependent. Eg. opened project in the browser. The server will determine which answer to say and if the caller is authorized to ask.
> What means does the user's PC have for obtaining the information about the > current server state? You wanted me to not write any concrete solutions, but I'm planning this by HTTP GET request to the server. I can include something like "session id" and caller authentication into the parameters of request. I'm already using Microsoft XML 2.0 reference in the VBA project.so the field actualization from the client side can start with xmldoc.load("http://server/getitem.ashx?fieldid=myfield1&userid=callerid") and answer in the xml format... well actually it could be maybe even a webservice.
> That brings me back to my earlier question - how secure do you want this > to be. Do you want it to be proof against accidental changes or against > deliberate attempts by the user to change or remove the field? User is free to remove the field as regular Word field. It would be nice to be proof against accidental changes, and moveable by user using mouse without need to carefully select all characters. If someone deliberatery tries to change the field it will be later or sooner successful, so invalid fields are to be handled by the server. They can be for example left unattended.
Thank you for your help. Jan.
Jan Kucera - 07 May 2006 15:53 GMT Actually it seems the document is going to be in the XML format so if I can for example use my own xml tags unknown to word format and they will be displayed at proper locations it will perhaps help a lot.
>>> -however in the template there is no logic in evaluating it. >> What (if anything) should be displayed at that point? [quoted text clipped - 37 lines] > Thank you for your help. > Jan. Jonathan West - 07 May 2006 16:00 GMT >>> -however in the template there is no logic in evaluating it. >> What (if anything) should be displayed at that point? [quoted text clipped - 34 lines] > sooner successful, so invalid fields are to be handled by the server. They > can be for example left unattended. OK, I think we have the outline of a solution here.
Have your menu create either a document variable or a custom document property (whichever you prefer) whose content is the same as its name insert also a matching DOCPROPERTY or DOCVARIABLE field. Because the content is the same as the name, the name will display at this point.
When the user creates a new document based on the template, it inherits both the document property and the DOCPROPERTY field. For each of the properties in the document, VBA code in an AutoNew macro in the template cycles through the document properties, for each one makes the appropriate query to the server and puts the information returned into the property. It then updates all the fields in the document so that the latest content of the properties is visible.
The fields can be deleted, but if the user just tries to edit the content without deleting the whole field, a further update to the field restores the original content.
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Jan Kucera - 07 May 2006 16:12 GMT Hi Jonathan, that's sounds very good, thank you very much. ;-)
Do you prefer properties or variables? What's the difference? Can you either of them view or edit using Word UI? (and how can I access properties? I found only ThisDocument.Variables)
Thanks, Jan.
Jonathan West - 07 May 2006 16:40 GMT > Hi Jonathan, > that's sounds very good, thank you very much. ;-) > > Do you prefer properties or variables? What's the difference? Can you > either of them view or edit using Word UI? > (and how can I access properties? I found only ThisDocument.Variables) Properties can be accessed through the CustomDocumentProperties collection of a Document object, in much the same way that document variables can be accessed through the Variables collection.
I have a suspicion you are getting mixed up between ThisDocument and ActiveDocument. They aren't the same thing. In this case I think you need to use ActiveDocument for the VBA code in the AutoNew macro. ActiveDocument is the document whose editing window is currently active.
Custom document properties can be accessed in the Word UI by going to File, Properties and then clicking the Custom tab. Document variables can only be accessed through VBA.
If avoiding access through the UI is important, I would go for variables. If you want to be able to quickly read the contents of the properties/variables without opening the document in Word, then I would use custom properties, which can be read by code which uses dsofile.dll to read & write properties. Document variables can only be strings, Custom properties can be String, Date, Longs, Double or Boolean.
I've used both properties and variables in different projects. I use whichever seems more appropriate by these criteria.
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Jan Kucera - 07 May 2006 16:59 GMT Okay, it seems I would choose variables. Since going over xml only strings are needed and lack of UI access is maybe worse at development time, but can be safe during usage. Well thank you for the ActiveDocument hint, if I've catched it, in the new document based on template, ThisDocument refers to template and ActiveDocument to the new document? But by using ActiveDocument, the fields could be inserted into documents I don't have a control over, couldn't be?
I am propably leaving templates and packing the code into the document itself, since I need the document to be saved into its original location.
Just a silly question, how can I find out if the document already contains the variable?
Jan.
Jan Kucera - 07 May 2006 17:13 GMT Can I switch the fields to the code/value view using VBA? I found only ToogleShowCodes, but no way to get current state or set the codes explicitly on or off.
Thanks, Jan.
Jonathan West - 07 May 2006 18:01 GMT > Can I switch the fields to the code/value view using VBA? > I found only ToogleShowCodes, but no way to get current state or set the > codes explicitly on or off. Take a look at the ActiveWindow.View.ShowFieldCodes property.
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Jonathan West - 07 May 2006 17:59 GMT > Okay, it seems I would choose variables. Since going over xml only strings > are needed and lack of UI access is maybe worse at development time, but > can be safe during usage. > Well thank you for the ActiveDocument hint, if I've catched it, in the new > document based on template, ThisDocument refers to template and > ActiveDocument to the new document? Correct.
> But by using ActiveDocument, the fields could be inserted into documents I > don't have a control over, couldn't be? ActiveDocument is just the current document. You can insert fields into any open document, as every document is a member of the Documents collection.
> I am propably leaving templates and packing the code into the document > itself, since I need the document to be saved into its original location. > > Just a silly question, how can I find out if the document already contains > the variable? You can cycle through the Variables collection and check the Name of each Variable. Alternatively, try read the value of the Variable, and trap the error that will arise if the Variable doesn't exist.
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Jan Kucera - 10 May 2006 17:27 GMT > You can cycle through the Variables collection and check the Name of each > Variable. Alternatively, try read the value of the Variable, and trap the > error that will arise if the Variable doesn't exist. Uh, what a wonderful world of Collection.Contains...... :-) Okay, is there any way to catch when the user deletes the field in the document? (I would like to delete the document variable as well then....)
Jan.
Jonathan West - 10 May 2006 20:44 GMT >> You can cycle through the Variables collection and check the Name of each >> Variable. Alternatively, try read the value of the Variable, and trap the [quoted text clipped - 3 lines] > Okay, is there any way to catch when the user deletes the field in the > document? (I would like to delete the document variable as well then....) You could put code into the Document_Save event that checks to see if there is at least one DOCVARIABLE field for each variable, and delete the variable before saving.
You can make use of *another* collection for this - the Fields collection. Each Field in the Fields collection of the document has a Code property which returns the text of the field code. Comparing this against the names of the variables will enable you to check if the variable is shown using a field.
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
|
|
|