PDA

View Full Version : Set style of text dynamically as I write



sam_sam
02-20-2007, 06:44 AM
Hi,
I want to type for example the following:

"This is my text, this is italic this is not. this is Red. and this is underlined."
How can I set that?
I know that I can set style with range, but the problem here is that I need to set text while i am writing, I don't want to calculate number of characters as I type and set my range....
any idea?

thx.
Sam.

fumei
02-20-2007, 12:18 PM
Not possible. How would Word know? How could it possibly know?

I need to set text while i am writingPlease describe the business case for requiring this. What makes you NEED to have this dynamic? Why do you NEED to format text as you type?

You can do this anyway. It is called clicking the icon button.

"This is", click Bold button, "my", click the Bold button, "text, this is", click the Italics button, "italics", click the Italics button, "this is not"......

So you can, in fact, do this while you type.

If you want to, you could make character styles and hotkey them.

Say the paragraph style is MyBodyText. Put your cursor in a paragraph of that style (althhough technically this is not required).

1. Open the Style and Formatting Task Pane.
2. Click New Style
3. Change the Style type to Character.
4. Make sure the Style based on is the paragraph style you want - say myBodyText
5. Change the character format - say Bold.
6. Save it.

Make sure you give it a clear name - BodyTextBold for example.

Repeat for italics, repeat for underline, repeat for red font.

OK. Now you have character styles for the format you want.

Give each of those a keyboard hotkey. Say Alt-BB for the bold character style, Alt-BI for italics, Alt-BU for underline, Alt-BR for red.

NOTE! unlike using the icon buttons, a keyboard shortcut for a character style is NOT a toggle!! Therefore to turn OFF the character style, you need a keyboard shortcut for the underlying paragraph style - which is good thing anyway.

Say the paragraph style shortcut is Alt-B.

"This is", Alt-BB, "my", Alt-B, "text, this is", Alt-BI, "italics", Alt-B, "this is not"......

Properly speaking, all format in a Word document should use styles, both paragraph and character. In fact, this is how I work. All my format is in styles. All of it.

However, because I use styles, I write differently.

I do very little formatting of text while I type. Just the basic styles, and yes, I use keyboard shortcuts. Because I have, and use styles, I just write. Every three of four pages I will go back and select all the heading paragraphs...Alt-H1. Select the paragraphs to be bulleted list items....Alt-L1.

Etc. etc.

Bottom line? The easy way to change format while you type is to click the icon buttons for that format. That is why they are there.

sam_sam
02-20-2007, 11:48 PM
Hi,
I think my post was not clear.
I am generating a word document using visual basic, I have a table, I need to write in the first cell in my table 1 line.
This line should contain the info from my database and should display the modified data in red or bold...
"Name: XYZ; Salary : 5,000 (2,500); Marital Status: Married (Single)"
What I need to do is to be able to set style of text dynamically, so whenever my data is chaged i need to set style to bold write the value then set it back to normal.

Hope this is clear.

thanks.
Sam.

fumei
02-21-2007, 02:18 PM
No, you were NOT clear.

You still are not clear.

If you are "writing" the values (as Selection.TypeText) then you are going about it the wrong way.

As you do not state how you ARE writing the text string I am at a loss to suggest how to do it. I can not read minds, and I can not see what you are doing.

"style of text", to me means.....style. I think what you are saying is "format of text". Which is different.

I would not "write" your data at all.

I would:

1. Use formfields.
2. Use Character styles for those formfields.

I just did a test document. I made the Characters styles, just as mentioned previously - although I did not bother with keyboard shortcuts, as this was to be done with code.

In the table I put your non-changing text, and formfields. One formfield named Salary, another named MaritalStaus. I set the Character style for Salary (Bold), a different Character style for MaritalStatus (Italics).

But you could make those whatever you want. It does not matter.ActiveDocument.Formfields("Salary").Result = "2,500"
ActiveDocument.Formfields("MaritalStatus").Result = "Single"

Done. The values of the formfields can change as dynamically as you want, as much as you want with code like above. The value ( or changes of value) has nothing to with format.

The format of the formfields is handled by a Character Style.

This way, you set the styles and forget about it. You never have to bother with it again. You never have to Select anything. You never have to "type" anything. You give the formfields a new value, and that's it.

BTW: you will get much better responses if you actually describe what you are doing. In your first post you wrote:

"I want to type for example the following:"

No you don't. You want to insert text of a specifc format, at a specifc location. Are you "typing"? No, not really.

I answered you based on your post, which asked about changing format as you type. However, that is NOT the situation.

What I need to do is to be able to set style of text dynamically, so whenever my data is chaged i need to set style to bold write the value then set it back to normal.
Bottom line is...no, you don't need to set style of text dynamically. No, whenever your data is changed you do NOT need to set the style to bold, write the value, and set it back to normal.

sam_sam
02-22-2007, 12:56 AM
Hi,
I am writing data in my first cell.

Call MsWordApplication.ActiveDocument.BookMarks("BK").Range.Tables(1).Cell(1).Range.InsertAfter("Name: XYZ; Salary : 5,000")
Here I want to set the font bold
MsWordApplication.ActiveDocument.BookMarks("BK").Range.Tables(1).Cell(1).Range.InsertAfter("(2500)")
Here I want set the font back to normal
MsWordApplication.ActiveDocument.BookMarks("BK").Range.Tables(1).Cell(1).Range.InsertAfter("Marital Status: Married ")
Here I want to set the font bold
MsWordApplication.ActiveDocument.BookMarks("BK").Range.Tables(1).Cell(1).Range.InsertAfter("(Single) ")
So that I end up with the following:
"Name: XYZ; Salary : 5,000 (2,500); Marital Status: Married (Single)"
The trick here is that I cannot have my bookmarks or fields predefined with the color or bold already set, because my data may contain other information that i cannot know beforehead.
Hope the idea is more clear know.

Thanks.
Sam.

fumei
02-22-2007, 12:33 PM
Again...no it is NOT clear.

Again, my suggestion was to use separate formfields, NOT ONE bookmark.

The trick here is that I cannot have my bookmarks or fields predefined with the color or bold already set, because my data may contain other information that i cannot know beforehead.What does that mean?

Does it mean you can have:

"Name: XYZ; Salary : 5,000 (2,500); Marital Status: Married (Single)"

in that the 2,500 is NOT bold? What logic is involved? Let me make my question as clear as I can.

"Name: XYZ; Salary : 5,000 (4,000); Marital Status: Married (Divorced)"

"Name: XYZ; Salary : 5,000 (2,500); Marital Status: Married (Single)"

Are you saying that you could have VALUES that require a change in format? In the example above, if it 4,000 it is NOT bold, if it is 2,500 it IS Bold.

If this is the case, then you have to state so.

Your code is doing action on ONE bookmark - BK. It appears that this bookmark is the entire table.

What I am suggesting is this.

In the Cell you have:

"Name: XYZ; Salary : 5,000 (" then a formfield named Salary, then "); Marital Status: Married (", then a [b]formfield named MaritalStatus.

You action against the formfields. And the formfields have their format by Character styles.

I am attaching a demo document. I use a userform to get the data - obviously this is not what you are doing. It is just a way to get some data.

The userfrom shows on document open. It is modalless, so you can move it around to see the document better. The Input button takes the values of the textboxes and put them in the formfields Salary, and MaritalStatus.

The formfields are formatted by Character styles.

Press Done to close the userform.

sam_sam
02-22-2007, 12:54 PM
Ok,
I have 1 cell in my table,(I have included bookmark BK, so that i can access my table easily).
The data in that cell will be filled from many records.
first document can be:
"Name: XYZ; Salary : 5,000 (2,500); Marital Status: Married (Single)"
second one can be
"Name:XYZ; Country : Lebanon (Canada); Salary : 5,000 (2,200)"
Third one can be
"Name:XYZ; Country : Lebanon (Canada); Salary : 5,000 (2,200) ;Military Service: Completed (In process);"

The data can contain many lines, values of these lines cannot be determined by a fixed entry (bookmark or field).
I cannot control number of lines nor the data distributed on each line.
That's why I needed an engine to manipulate the format the way I need and dynamically.

thx.
Sam.

fumei
02-23-2007, 10:17 AM
Ah, there you go. It SURE does help when you actually say what the situation is.

I must say this is not very good design.

You can not do this with Range. You will have to use Selection.MsWordApplication.ActiveDocument.BookMarks("BK") _
.Range.Tables(1).Cell(1).Select
With Selection
.TypeText "Name: XYZ; Salary : 5,000 "
.Font.Bold = True
.TypeText "(2,500)"
.Font.Bold = False
.TypeText " Marital Status: Married "
.Font.Bold = True
.TypeText "(Single)"
End With

This assumes there is nothing in the cell to start with.

The data can contain many lines, values of these lines cannot be determined by a fixed entry (bookmark or field).I have no idea what you mean by that. Bookmarks or fields do not determine values. They are given values.

They can be involved with logic that can determine values.

OK, so....somewhere, SOMETHING is using logic to determine what to put in that cell.

If it is as variable as you say, then you are going to have to write it differently for each text insertion. This is poor design.

There is a lot of stuff going on behind this that is not being stated. So....

sam_sam
02-25-2007, 07:08 AM
Thanks for your follow-up this is exactly what i needed.

The data can contain many lines means that I can have many lines, so I have added ".TypeParagraph" at the end of the code.

thx.
Sam.

fumei
02-26-2007, 07:12 AM
Huh?

You totally lost me on that one. Everything you have posted seemed to indicate that this string is NOT separate paragraphs, but is ONE paragraph. It may go into two lines, but the paragraph is ONE paragraph. So I am not seeing where .TypeParagraph is solving anything.

Mind you, in the demo document I posted, I did in fact ,ake them separate paragraphs, as it seemed ugly having it one paragraph.

I am not sure exactly how this has been solved, but if it HAS:

1. Mark the thread as Solved.
2. Post your code so we can see what it is you did to solve this.

thanks.