What is wrong with this range?

?

?yvind Saltvik

Sub MakeHTML()
'
' Makro1 Makro
' Makro registrert 24.04.2004 av Ø S
'
Dim apara As Paragraph, prange As Range

For Each apara In ActiveDocument.Paragraphs

Dim rS As Range
Set rS = apara.Range

Dim rE As Range
Set rE = apara.Range

rS.MoveStart wdCharacter, 0
rS.InsertBefore ("<p class=""" & apara.Range.Style & """>")

For Each myChar In rS.Characters
' Next line applies to the entire paragraph , it should not
myChar.Style = ActiveDocument.Styles("Normal")
Next myChar

rE.MoveEnd wdCharacter, -1
rE.InsertAfter ("</p>")

For Each myChar In rE.Characters
myChar.Style = ActiveDocument.Styles("Normal")
Next myChar

Next apara

End Sub
 
P

Peter Hewett

Hi ?yvind Saltvik

It would help us to provide you with an answer if you tell us:

1. What is it exactly that you're trying to do?
2. What is it that you code is, or is not doing that it should?


All I can see is that you are applying a style to *all* characters in the Range "rS". Buy
the way the statement:
rS.MoveStart wdCharacter, 0

does absolutely nothing, since you telling work to move the start of the range zero
characters!

HTH + Cheers - Peter


(e-mail address removed) (?yvind Saltvik), said:
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < ?yvind Saltvik > écrivait :
In this message, < ?yvind Saltvik > wrote:

|| Sub MakeHTML()
|| '
|| ' Makro1 Makro
|| ' Makro registrert 24.04.2004 av Ø S
|| '
|| Dim apara As Paragraph, prange As Range
||
|| For Each apara In ActiveDocument.Paragraphs
||
|| Dim rS As Range
|| Set rS = apara.Range
||
|| Dim rE As Range
|| Set rE = apara.Range
||
|| rS.MoveStart wdCharacter, 0
|| rS.InsertBefore ("<p class=""" & apara.Range.Style & """>")
||
|| For Each myChar In rS.Characters
|| ' Next line applies to the entire paragraph , it should not
|| myChar.Style = ActiveDocument.Styles("Normal")
|| Next myChar
||
|| rE.MoveEnd wdCharacter, -1
|| rE.InsertAfter ("</p>")
||
|| For Each myChar In rE.Characters
|| myChar.Style = ActiveDocument.Styles("Normal")
|| Next myChar
||
|| Next apara
||
|| End Sub

And to add to Peter's comments, I do believe that with the line
|| myChar.Style = ActiveDocument.Styles("Normal")
you are trying to apply a style to a character. Normally, unless "Normal" is
a character style, which is doubtful considering its name, a style applies
to a whole paragraph.
So your code is unnecessarily cycling through every character in the
paragraph, because it is the same as grabbing the first one only, or as
doing:
rS.Style = ActiveDocument.Styles("Normal")


Then, later in your code,
|| For Each myChar In rE.Characters
|| myChar.Style = ActiveDocument.Styles("Normal")
You apply again the style Normal to the exact same paragraph, again
unnecessarily grabbing each character one by one... A bit overkill, no?

So, as Peter said, unless you tell us what were your expectations and why
you thought the code was necessary, it is difficult to offer alternatives...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
?

?yvind Saltvik

rS.MoveStart wdCharacter, 0

' Trying to make the range only apply to the text in rS.InsertBefore
' Is there a right/better way

rS.InsertBefore ("<p class=""" & apara.Range.Style & """>")

For Each myChar In rS.Characters

' Next line applies to the entire paragraph , it should not
myChar.Style = ActiveDocument.Styles("Normal")

Next myChar

Øyvind
 
P

Peter Hewett

Hi ?yvind Saltvik

I thinks this is what you want:

Sub MakeHTML()
Dim apara As Word.Paragraph
Dim rE As Range
Dim rS As Range

' Add p tags to each Word paragraph that contains something
For Each apara In ActiveDocument.Paragraphs

' Must be more than just a paragraph mark
If Len(apara.Range.Text) > 1 Then
Set rS = apara.Range.Duplicate

' Start p tag
rS.Collapse wdCollapseStart
rS.Text = "<p class=""" & apara.Range.Style & """>"

' Update style
rS.Style = ActiveDocument.Styles("Normal")

' Terminate p tag
Set rE = apara.Range
rE.MoveEnd wdCharacter, -1
rE.Collapse wdCollapseEnd
rE.Text = "</p>"
rE.Style = ActiveDocument.Styles("Normal")
End If
Next
End Sub

HTH + Cheers - Peter


(e-mail address removed) (?yvind Saltvik), said:
 
Top