Change colour to some words in a text string -vba Word 2003

S

spenningzahl

If a condition is met I want to set a bookmark with text consisting of
for instance 3 parts.

Eg. FirstPartofSentence & SecondPartofSentence & ThirdPartofSentence

I want SecondPartofSentence to be shown in red. Does anybody know how
I can fix this?

Thanks,
Karin
 
S

spenningzahl

Hi,

Define 'SecondPartofSentence'.

--
Cheers
macropod
[MVP - Microsoft Word]



If a condition is met I want to set a bookmark with text consisting of
for instance 3 parts.
Eg. FirstPartofSentence & SecondPartofSentence & ThirdPartofSentence
I want SecondPartofSentence to be shown in red. Does anybody know how
I can fix this?
Thanks,
Karin– Skjul sitert tekst –

– Vis sitert tekst –

Thanks, but could you please give me an example of Define. Most
greatful for this help.
Karin
 
G

Greg Maxey

A definition is a statement of the meaning of a word or phrase.

I guess what Paul is asking is for you to provide a statement fo the meaning
SecondPartOfSentence (i.e., what is it that sets Part 2 apart from Part 1
and Part 3 of the complete string).

Sub ScratchMacro()
Dim oRng As Word.Range
If Selection.Sentences(1).Words.Count > 9 Then 'The condition
Set oRng = Selection.Sentences(1)
ActiveDocument.Bookmarks.Add "Name", oRng
'Assumes that part 1 is words 1 through 3 and part 3 is words 7 throghh
the end of the string.
oRng.Start = oRng.Words(3).Start
oRng.End = oRng.Words(6).End
oRng.Font.Color = wdColorRed
End If
End Sub


Hi,

Define 'SecondPartofSentence'.

--
Cheers
macropod
[MVP - Microsoft Word]



If a condition is met I want to set a bookmark with text consisting
of for instance 3 parts.
Eg. FirstPartofSentence & SecondPartofSentence & ThirdPartofSentence
I want SecondPartofSentence to be shown in red. Does anybody know
how I can fix this?
Thanks,
Karin– Skjul sitert tekst –

– Vis sitert tekst –

Thanks, but could you please give me an example of Define. Most
greatful for this help.
Karin
 
S

spenningzahl

A definition is a statement of the meaning of a word or phrase.

I guess what Paul is asking is for you to provide a statement fo the meaning
SecondPartOfSentence (i.e., what is it that sets Part 2 apart from Part 1
and Part 3 of the complete string).

Sub ScratchMacro()
Dim oRng As Word.Range
If Selection.Sentences(1).Words.Count > 9 Then 'The condition
  Set oRng = Selection.Sentences(1)
  ActiveDocument.Bookmarks.Add "Name", oRng
  'Assumes that part 1 is words 1 through 3 and part 3 is words 7 throghh
the end of the string.
  oRng.Start = oRng.Words(3).Start
  oRng.End = oRng.Words(6).End
  oRng.Font.Color = wdColorRed
End If
End Sub





Hi,
Define 'SecondPartofSentence'.
--
Cheers
macropod
[MVP - Microsoft Word]
messageIf a condition is met I want to set a bookmark with text consisting
of for instance 3 parts.
Eg. FirstPartofSentence & SecondPartofSentence & ThirdPartofSentence
I want SecondPartofSentence to be shown in red. Does anybody know
how I can fix this?
Thanks,
Karin– Skjul sitert tekst –
– Vis sitert tekst –
Thanks, but could you please give me an example of Define. Most
greatful for this help.
Karin

--
Greg Maxey -  Word MVP

My web sitehttp://gregmaxey.mvps.org
Word MVP web sitehttp://word.mvps.org– Skjul sitert tekst –

– Vis sitert tekst –

OK - thanks

I will give an example

I have this variable; TekstOverJa = ForOverJa & Aar & EtterOverJa
where I want ForOverJato be written in red.


Dim ForOverNei As String, EtterOverNei As String, TekstOverNei As
String, ForOverJa As String, EtterOverJa As String, TekstOverJa As
String

ForOverJa = "[Overvurdering/Undervurdering] er vurdert vesentlig for
regnskapsavleggelsen for "
EtterOverNei = " og krever omtale i revisjonsberetningen. Vi ber om at
ledelsen for fremtiden anvender bedre og mer kvantitative metoder ved
estimeringen av verdi av vurderingsposter i regnskapet."
Aar=txtAar.Text
TekstOverJa = ForOverJa & Aar & EtterOverJa

If Opt2_4_Ja = Value Then
ActiveDocument.Bookmarks("bm2_4_1").Select
Selection.TypeText Text:=TekstJaA
Selection.TypeParagraph

End If
 
G

Greg Maxey

Well I don't speak your language but it appears that ForOverJa is the first
part not the second
TekstOverJa = ForOverJa & Aar & EtterOverJa

Regardless, AFAIK you can't set the color of a string variable using VBA.
All I can suggest doing is to put the string in the bookmark, then find the
substring and change its color. Something like this:

Sub ScratchMacro()
Dim myStr As String, str1 As String, str2 As String, str3 As String
Dim oRng As Word.Range
Dim bProcess As Boolean
str1 = "FirstPart"
str2 = "SecondPart"
str3 = "ThirdPart"
myStr = str1 + str2 + str3
bProcess = True 'The Condition
If bProcess Then
Set oRng = ActiveDocument.Bookmarks("NamedBookmark").Range
oRng.Text = myStr
ActiveDocument.Bookmarks.Add "NamedBookmark", oRng
With oRng.Find
.Text = str1
While .Execute
oRng.Font.Color = wdColorRed
oRng.Collapse wdCollapseEnd
Wend
End With
End If
End Sub



A definition is a statement of the meaning of a word or phrase.

I guess what Paul is asking is for you to provide a statement fo the
meaning SecondPartOfSentence (i.e., what is it that sets Part 2
apart from Part 1 and Part 3 of the complete string).

Sub ScratchMacro()
Dim oRng As Word.Range
If Selection.Sentences(1).Words.Count > 9 Then 'The condition
Set oRng = Selection.Sentences(1)
ActiveDocument.Bookmarks.Add "Name", oRng
'Assumes that part 1 is words 1 through 3 and part 3 is words 7
throghh
the end of the string.
oRng.Start = oRng.Words(3).Start
oRng.End = oRng.Words(6).End
oRng.Font.Color = wdColorRed
End If
End Sub














--
Greg Maxey - Word MVP

My web sitehttp://gregmaxey.mvps.org
Word MVP web sitehttp://word.mvps.org– Skjul sitert tekst –

– Vis sitert tekst –

OK - thanks

I will give an example

I have this variable; TekstOverJa = ForOverJa & Aar & EtterOverJa
where I want ForOverJato be written in red.


Dim ForOverNei As String, EtterOverNei As String, TekstOverNei As
String, ForOverJa As String, EtterOverJa As String, TekstOverJa As
String

ForOverJa = "[Overvurdering/Undervurdering] er vurdert vesentlig for
regnskapsavleggelsen for "
EtterOverNei = " og krever omtale i revisjonsberetningen. Vi ber om at
ledelsen for fremtiden anvender bedre og mer kvantitative metoder ved
estimeringen av verdi av vurderingsposter i regnskapet."
Aar=txtAar.Text
TekstOverJa = ForOverJa & Aar & EtterOverJa

If Opt2_4_Ja = Value Then
ActiveDocument.Bookmarks("bm2_4_1").Select
Selection.TypeText Text:=TekstJaA
Selection.TypeParagraph

End If
 
H

Helmut Weber

Hi,
I have this variable; TekstOverJa = ForOverJa & Aar & EtterOverJa
where I want ForOverJato be written in red.

something along these lines, maybe.

Sub Test664()
Dim s As String ' redundant
Dim a As String
Dim b As String
Dim c As String
a = "start"
b = " middle "
c = "end"
s = a & b & c ' redundant

selection.Font.Color = wdColorAutomatic
selection.TypeText a
selection.Font.Color = wdColorRed
selection.TypeText b
selection.Font.Color = wdColorAutomatic
selection.TypeText c
selection.Font.Color = wdColorAutomatic
End Sub
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
S

spenningzahl

Hi,


something along these lines, maybe.

Sub Test664()
Dim s AsString' redundant
Dim a AsString
Dim b AsString
Dim c AsString
a = "start"
b = " middle "
c = "end"
s = a & b & c ' redundant

selection.Font.Color = wdColorAutomatic
selection.TypeText a
selection.Font.Color = wdColorRed
selection.TypeText b
selection.Font.Color = wdColorAutomatic
selection.TypeText c
selection.Font.Color = wdColorAutomatic
End Sub
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

Super, thanks a lot for your answers. This will help me.
Karin
 
G

Greg Maxey

Hi Helmut,

Where have you been? Yes, when I get passed my aversion to using selection
that would appear to work just as well ;-)
 
H

Helmut Weber

Hi submariner,

I've been around all the time.
Its just that there were maybe not enough questions for me.

Maybe someone should write an article about:
"Use range instead of selection exeptions"
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Greg Maxey

I thought that someone had. Maybe I just saw a post with something like
"Top 10 reasons for using Selection over Range" (For everything else USE
RANGE). It was really interesting and I thought I kept it for reference but
I can't find it now.

I'd be happy to post an article on my website but I would need some help
determining the top 10 (or however many there are). I'll ping the private
group and see what surfaces.
 

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