custom fields

J

Jan Kucera

Hi,
can I define my own fields or is there any field which will call VBA
function and display result when actualized?

Thanks, Jan.
 
J

Jan Kucera

Well... then how can I include some piece of text with "readonly" content in
the document?
 
J

Jonathan West

Jan Kucera said:
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?


--
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
 
J

Jan Kucera

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

Jonathan West

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

--
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
 
J

Jan Kucera

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

Jonathan West

Jan Kucera said:
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.

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

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?


--
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
 
J

Jan Kucera

Jonathan West said:
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.
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.
 
J

Jan Kucera

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

Jonathan West

Jan Kucera said:
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.


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.

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.

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.

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.


--
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
 
J

Jan Kucera

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

Jonathan West

Jan Kucera said:
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.


--
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
 
J

Jan Kucera

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

Jan Kucera

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

Jonathan West

Jan Kucera said:
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.

--
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
 
J

Jonathan West

Jan Kucera said:
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.


--
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
 
J

Jan Kucera

Jonathan West said:
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.
 
J

Jonathan West

Jan Kucera said:
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....)

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.


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top