PDA

View Full Version : Solved: How to set color for character range in sentence?



h_nut
01-26-2008, 12:21 PM
I'm am running a vba macro in Excel and part of it is to change the color on a range of characters in a sentence. The code is shown below and I use bold instead of color. I can't seem to get it to work. Any suggestions or help would be appreciated.
Thanks.:bow:

Dim wrdApp As Object
Dim wrdDoc As Object
Dim rngDoc As Range
Set wrdApp = CreateObject("Word.Application")
'wrdApp.Visible = False
Set wrdDoc = wrdApp.Documents.Add
With wrdDoc
.Content.InsertAfter transfer_txt & w + 1
'Set rngDoc = ActiveDocument.Sentences(1).Range(Start:=1, End:=3)
..Sentences(1).Font.Bold = True

'rngDoc.Font.ColorIndex = 3
'ActiveDocument.Bold = True

..SaveAs (Input_filenme & "testWord.doc")
..Close
End With
'Set rngDoc = ActiveDocument.Range(Start:=1, End:=3)

'ActiveDocument.Range(Start:=1, End:=3).Bold = True
''wrdApp.Quit

TonyJollans
01-27-2008, 06:10 AM
I'm afraid I'm not following what you want, or what doesn't work. What are the two dots meant to represent?

One point worth making is that you must qualify your Word objects properly. ActiveDocumnet, for example, in Excel VBA won't work - it must be (Word).ActiveDocument,

h_nut
01-27-2008, 09:57 AM
The "two dots" should just be one dot (they must have gotten duplicated when I copied the code - sorry). I included all the lines I had tried and the one I think you're talking about is commented out - I will try it with the (Word) prefix.

To better explain what I'm trying to do:
From vba within Excel, I want to look at each row or sentence in a Word document - in each row or sentence I want to be able to highlight only certain characters. For example, say row/sentence 1 is: Have a nice day.
I may want to highlight only the 3rd and 6th characters (v and a). On the next sentence I may want to highlight different characters based on other variables. Hope that helps.
Thanks for the suggestion.

TonyJollans
01-27-2008, 11:48 AM
There is a problem with terminology in Word. You may think you are saying something perfectly reasonable in English but many words have a specific meaning within Word that isn't always immediately obvious. I suspect neither Row nor Sentence is (technically) what you want; however that isn't really the issue at the moment.

If you want to do something with the third and sixth characters you will have to do it in two steps - Word VBA does not have any capacity for Ranges containing several Areas in the same way as Excel. You should, though, be able to do something like this:


With wrdDoc
.Characters(3).Font.Color=wdColorBlue
.Characters(6).Font.Color=wdColorBlue


Are you saying that doesn't work?

Perhaps where you're coming unstuck is in specifying the Ranges. Documents have a Range Property that takes start and end positions and returns a Word Range Object. Range Objects cannot be used as you have tried - The Sentences Collection contains Range Objects that do not have a Range Property.

I'm not sure of the best way for you to go because I don't know how you are going to decide that the characters you want are the 'v' and the 'a' in your example.

h_nut
01-27-2008, 02:55 PM
Tony,
Thanks so much for your patience. I think I get the drift of what you're saying regarding the row / sentence - it it true it's not immediately obvious and I haven't work with vba in Word. Let me try what you provided, and I'll see if I can manipulate it into what I need.
Thanks.

fumei
01-28-2008, 12:46 AM
Yes, you need to clarify exactly what you want, and try and get a handle on how Word thinks of things.

"This a sentence. And this next sentence is in the
same PARAGRAPH. As is this one."

Th eabove is ONE paragraph. Three Sentences. As, as it happens...two "rows", or "lines". But what if it was - the same text:

"This a sentence. And this next sentence is in
the same PARAGRAPH.
As is this one."

Is is possible that the above still is ONE paragraph, three sentences, but now three "rows".

Or, it could be two paragraphs. In any case, Word is very very very very particular about these things.

Now, according to your initial code:

Set wrdDoc = wrdApp.Documents.Add
With wrdDoc
.Content.InsertAfter transfer_txt & w + 1

You seem to be inserting some text into a new document. That's fine, but you do not need to to do InsertAfter, for one thing. It is a new blank document. There is nothing there to be after.

transfer_txt & w + 1

is very mysterious. What is "w", and why are you adding a number to text?

In any case, as the inserted text does not have a paragraph mark, mostly you are, in fact, dealing with one paragraph. As far as your inserted text goes.

However, you are mentioning "rows" as if they are equivalent to sentences. Maybe...maybe not.

For example, say row/sentence 1 is: Have a nice day.

I may want to highlight only the 3rd and 6th characters (v and a). On the next sentence I may want to highlight different characters based on other variables. Hope that helps.

Next sentence may, or may not, be in the same paragraph. So you may, or may not, be getting the right range. You can certainly do whatever you want, to whatever characters you want, where ever you want. Word can handle it.

The issue is telling Word where - exactly - those characters are.

h_nut
01-28-2008, 05:34 PM
Fumei,
Thanks for the information.
I was able to use the information Tony provided to resolve the problem today, and therefore was just now logged in and marked this as solved. I appreciate your time and helpful goodies throughout the forum.
Being new in this forum, I hope I concluded this thread properly.