Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Fields & Variables with formatting 1

Status
Not open for further replies.

sheila11

Programmer
Dec 27, 2000
251
US
Hi all,

I am trying to use fields and variables to toggle content.
I have a DocVariable field (named FL1), and a variable (named RFL1) in the document.

I store the value of the field FL1 in the variable RFL1.
Here is my code:

Sub HideWithVariable()
ActiveDocument.Variables("RFL1").Value = ActiveDocument.Variables("FL1").Value
ActiveDocument.Variables("FL1").Value = ""
ActiveDocument.Range.Fields.Update
End Sub

Sub ShowWithVariable()
ActiveDocument.Variables("FL1").Value = ActiveDocument.Variables("RFL1").Value
ActiveDocument.Range.Fields.Update
End Sub

This work, but the problem is that the formatting is lost.

Is there a way to retain the formatting in the variable?

Or else, is there a field type better suited for this?

TIA,
Sheila

PS: I can select the field, and use font.hidden = true/false
But that method works very slowly when I have hundreds of fields in a document. I am trying to use Fields.Update
 
Formatting is generally for displaying data rather than storing it. If you can store the data natively, you can use the Format() function in the recordsource of a text box or in VBA to return the data as you wish to view it.

"Don't be irreplaceable. If you can't be replaced, you can't be promoted."
 
I'm pretty sure the variable does not have any formatting associated with it (that is, the variable object does not have a format property). Rather, the field has the formatting associated with the paragraph where it is found. I'm not sure but the field's parent property may refer to the paragraph. Anyway, you'll need another variable.

_________________
Bob Rashkin
 
But I can't reproduce your reported behavior.

As Bong says, the format of a field is associated with the text or paragraph that the field is in.
 
I am sorry, I should have been clearer about the word "Formatting". What I mean is the style of the text.. the words being bold, italic, superscripted etc.

I do understand that neither the variables nor any of the fields are able to hold the content with such "formatting". But I have seen applications doing this. (e.g. ActiveDocs) Those were using REF fields, without any external files, probably in combination with other fields or variables.
So I guess there must be some way to do it.
 
I took a document with DocVariable fields.

I changed the value of a variable and updated the fields. The format of what was displayed in the document was unchanged.

I changed the formatting (color and font).

I changed the values and updated. The format stayed as changed.
 
Sorry for the delay, Sheila, I missed your reply.

I know what formatting means; I asked *how* you had applied it. It can sometimes be a bit fiddly and if the format is character formatting applied just to the text, it can be deleted along with the text, but if the field is formatted, the formatting should stick. Alternatively, you can use the \*Charformat switch and apply the formatting you want to the field code.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
mintjukep, Tony,

Thanks for your responses.

I need any type of content (paragraphs, tables, etc) to be stored in a field or a variable. And that content can be displayed or hidden based on certain conditions.

In the first function I have shown (above), I am trying to store the contents od FL1 field inside RFL1 variable, and set FL1 content to nothing. In the second, I am trying to re-set the original content back into the FL1 field. FL1 field was a DocVariable field, and the content was formatted by user.

Weather I use the \* Charformat or \* MERGEFORMAT, the field looses the formatting because the variable RFL1 had only stored the text of the content, and not the formatting info.
 
and the content was formatted by user. "

And THAT is the issue.

You could have explicit styles - MyFieldStyleHide and MyFieldStyleShow. Then explicitly use them any time you change anything. Then if the user changes the format, applying the style back again will do just that.

faq219-2884

Gerry
My paintings and sculpture
 
Gerry,

Are you assuming that the entire content of a field / variable is necessarily going to be formatted with just one style? That's an incorrect assumption. The content can be one or more paragraphs, with some words bold, some underlined, some with a different font than others.

Sheila
 
Hi Sheila,

I had made that same assumption as, I think, we probably all did.

If the text may have varied formatting you cannot use document variables to store it. The only place to store formatted text within a document is within the document itself but you could, perhaps, use Autotexts in the document's template as a way round the problem.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
True, yes I was assuming that. Yes, if there is multiple formatting, then AutoText is a possibility.

I would add that using an INCLUDETEXT field (rather than DOCVARIABLE) could be another option.

In any case, even if there is "some words bold, some underlined, some with a different font than others", every one of those differences should be an explicit character style.


faq219-2884

Gerry
My paintings and sculpture
 
hi Sheila,

As an alternative, you could store your variables inside SET fields located, for example, in the header area. SET fields are used to create bookmarks and the text within them can have whatever formatting you like, including multiple paragraphs with different styles. You could then use IF tests with REF fields to conditionally retrieve the contents of the various SET fields.

Cheers

[MS MVP - Word]
 
macropod,

Could you give me an example, or direct me to a URL where I can find one please?

TIA,
Sheila
 
Hi Sheila,

If you take a look at my Date Calc 'tutorial', at:
or
You'll find examples with multiple paragraphs inside a field. In particular, look at examples under the heading 'Use Date (And/Or Time) Comparisons To Vary Text'.

None of those examples use a SET field to create a bookmark (one example uses an ASK field to set a bookmark), but the principle is the same.

Cheers

[MS MVP - Word]
 
macropod, and others,

Thank you very much for all your inputs. I have a working solution now.

I am using an IF Field, as you have shown in your examples, macropad, and am able to retain the various formatting done by the user.

Thanks again,
Sheila
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top