Setting paragraph (without CR) as Range 
Author Message
 Setting paragraph (without CR) as Range

Hi,

I'm not very familiar with VB, but I'm trying to create a macro which
scans the selected paragraphs for a specific style and adds HTML tags for
this style to the paragraph.

The problem I'm having is that, in Word, the paragraph includes the <CR>
character at the end, so when I do an .InsertAfter, it inserts the closing
tag after the return char. I'd like to insert the closing tag before. But
I can't seem to be able to set a range going from the beginning of the
para to the last character before the <CR>

Here's what I have:

Sub paraStyleToTag(myStyle, myTag)
If Selection.Type <> wdSelectionIP Then
For i = 1 To Selection.Paragraphs.Count
If Selection.Paragraphs(i).Style = myStyle Then
Set myPara = Selection.Paragraphs(i).Range
Set myChars = myPara.Characters
Set myCount = myChars.Count
myChars.SetRange Start:=myChars.Start, End:=myChars.Characters(myCount - 1)
With myChars
    .InsertBefore Text:=("<" & myTag & ">")
    .InsertAfter Text:=("</" & myTag & ">")
End With
End If
Next i
Else
MsgBox "No selection."
End If
End Sub

It tells me there's a "type mismatch" in the Set myCount = myChars.Count line.

What's wrong?

TIA

Pierre



Sun, 22 Jun 2003 23:49:46 GMT  
 Setting paragraph (without CR) as Range
Hi Pierre,

After this line

    Set myPara = Selection.Paragraphs(i).Range

add this

    myPara.MoveEnd Unit:=wdCharacter, Count:=-1

This will exclude the paragraph mark from the range.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email


Quote:
> Hi,

> I'm not very familiar with VB, but I'm trying to create a macro which
> scans the selected paragraphs for a specific style and adds HTML tags for
> this style to the paragraph.

> The problem I'm having is that, in Word, the paragraph includes the <CR>
> character at the end, so when I do an .InsertAfter, it inserts the closing
> tag after the return char. I'd like to insert the closing tag before. But
> I can't seem to be able to set a range going from the beginning of the
> para to the last character before the <CR>

> Here's what I have:

> Sub paraStyleToTag(myStyle, myTag)
> If Selection.Type <> wdSelectionIP Then
> For i = 1 To Selection.Paragraphs.Count
> If Selection.Paragraphs(i).Style = myStyle Then
> Set myPara = Selection.Paragraphs(i).Range
> Set myChars = myPara.Characters
> Set myCount = myChars.Count
> myChars.SetRange Start:=myChars.Start, End:=myChars.Characters(myCount -
1)
> With myChars
>     .InsertBefore Text:=("<" & myTag & ">")
>     .InsertAfter Text:=("</" & myTag & ">")
> End With
> End If
> Next i
> Else
> MsgBox "No selection."
> End If
> End Sub

> It tells me there's a "type mismatch" in the Set myCount = myChars.Count
line.

> What's wrong?

> TIA

> Pierre



Mon, 23 Jun 2003 00:58:09 GMT  
 Setting paragraph (without CR) as Range
It works. Thanks a lot!

I'm still having a hard time understanding what's wrong in my script, though.

Pierre


Quote:

> Hi Pierre,

> After this line

>     Set myPara = Selection.Paragraphs(i).Range

> add this

>     myPara.MoveEnd Unit:=wdCharacter, Count:=-1

> This will exclude the paragraph mark from the range.

> --
> Regards
> Jonathan West - Word MVP
> MultiLinker - Automated generation of hyperlinks in Word
> Conversion to PDF & HTML
> http://www.multilinker.com
> Word FAQs at http://www.multilinker.com/wordfaq
> Please post any follow-up in the newsgroup. I do not reply to Word questions
> by email



> > Hi,

> > I'm not very familiar with VB, but I'm trying to create a macro which
> > scans the selected paragraphs for a specific style and adds HTML tags for
> > this style to the paragraph.

> > The problem I'm having is that, in Word, the paragraph includes the <CR>
> > character at the end, so when I do an .InsertAfter, it inserts the closing
> > tag after the return char. I'd like to insert the closing tag before. But
> > I can't seem to be able to set a range going from the beginning of the
> > para to the last character before the <CR>

> > Here's what I have:

> > Sub paraStyleToTag(myStyle, myTag)
> > If Selection.Type <> wdSelectionIP Then
> > For i = 1 To Selection.Paragraphs.Count
> > If Selection.Paragraphs(i).Style = myStyle Then
> > Set myPara = Selection.Paragraphs(i).Range
> > Set myChars = myPara.Characters
> > Set myCount = myChars.Count
> > myChars.SetRange Start:=myChars.Start, End:=myChars.Characters(myCount -
> 1)
> > With myChars
> >     .InsertBefore Text:=("<" & myTag & ">")
> >     .InsertAfter Text:=("</" & myTag & ">")
> > End With
> > End If
> > Next i
> > Else
> > MsgBox "No selection."
> > End If
> > End Sub

> > It tells me there's a "type mismatch" in the Set myCount = myChars.Count
> line.

> > What's wrong?

> > TIA

> > Pierre



Mon, 23 Jun 2003 05:25:37 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Delete range of paragraphs based on start text of paragraphs

2. How to set a range within a paragraph

3. Range...Words...paragraphs...HELP

4. Return ONLY selected paragraphs within a range that includes a table

5. Paragraph.Range.Footnotes Count/Item Mismatch

6. How to convert word paragraph or range into RTF format

7. printing CR without from program without viewing

8. pasting a word doc into an email without losing paragraphing

9. preview range parameter in CR.net

10. Date range problem in CR 6

11. Saving Excel Range to Image without using Sendkeys

12. How to set starting paragraph number?

 

 
Powered by phpBB® Forum Software