Adding different style text to the same range

C

caveatRob

Hi all,

I'm trying to create a new document, add a range, then add heading 1
text followed by normal text for each of several sections I'm trying
to insert.

The code for the insertion of the headers is as follows:

Set listDoc = Documents.Add
sourceDoc.Activate
Dim o As Range


For Each i In colStyles

listDoc.Range.InsertAfter CStr(i) & vbCr
listDoc.Range.Style = "Heading 1"

ExtractByStyle CStr(i), listDoc.Range



Next i


The ExtractByStyle code is here:

Sub ExtractByStyle(sStyle As String, ByRef myRange As Range)


myRange.Collapse wdCollapseEnd

If StyleExists(sStyle) Then
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting


Selection.Find.Style = sStyle

Do While Selection.Find.Execute(findText:="",
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True)

myRange.InsertAfter Selection.Range & vbCr


Selection.Collapse wdCollapseEnd

Loop

Else
myRange.InsertAfter "Style " & sStyle & " doesn't exist!" & vbCr

End If

myRange.InsertAfter vbCr
myRange.Style = wdStyleNormal

End Sub

The first heading and all following text comes out as Heading 1; the
second heading seems to work. It is Heading 1, and subsequent text is
normal.

Help!
 
J

Jean-Guy Marcil

Hi all,

I'm trying to create a new document, add a range, then add heading 1
text followed by normal text for each of several sections I'm trying
to insert.

The code for the insertion of the headers is as follows:

Set listDoc = Documents.Add
sourceDoc.Activate
Dim o As Range


For Each i In colStyles

listDoc.Range.InsertAfter CStr(i) & vbCr
listDoc.Range.Style = "Heading 1"

ExtractByStyle CStr(i), listDoc.Range



Next i


The ExtractByStyle code is here:

Sub ExtractByStyle(sStyle As String, ByRef myRange As Range)


myRange.Collapse wdCollapseEnd

If StyleExists(sStyle) Then
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting


Selection.Find.Style = sStyle

Do While Selection.Find.Execute(findText:="",
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True)

myRange.InsertAfter Selection.Range & vbCr


Selection.Collapse wdCollapseEnd

Loop

Else
myRange.InsertAfter "Style " & sStyle & " doesn't exist!" & vbCr

End If

myRange.InsertAfter vbCr
myRange.Style = wdStyleNormal

End Sub

The first heading and all following text comes out as Heading 1; the
second heading seems to work. It is Heading 1, and subsequent text is
normal.

I don't really understand what you are trying to do... Especially the
"ExtractByStyle" sub...
It does not help that you posted partial code... (the first Sub).

However, I might be able to give you a few pointers that will help you solve
your problem.

1) If you are not doing so already, do use "Option Explicit" at the top of
your module. This will force you to declare all your variable, and while youa
re at it, do type your variables when you declare them "Dim strNew As String).
2) Why are you using: "sourceDoc.Activate" You do not need to activate
anything if you are working with ranges.
3) Work only with the Range object. You should modify your "ExtractByStyle"
sub so that it does not rely on the slow, unstable and unpredictable
Selection object.
4) The way your "ExtractByStyle" sub is written, it will always find only
the frist instance of sStyle.
5) What is "colStyles"
 
C

caveatRob

Okay- let's try this simplified example without all of my extraneous
code:

Hi all,

I've simplified my range/style example.

I'd like to insert text into a new document (created with
documents.add), making some of the text a "Heading 1" style and some a
"Normal style" -- and then repeating for as many headings as I have.

Given the following snippet, what changes do I have to make?

Dim listDoc As Document

Set listDoc = Documents.Add


listDoc.Range.InsertAfter "My first heading" & vbCr
listDoc.Range.Style = "Heading 1"

listDoc.Range.InsertAfter "My first normal text" & vbCr
listDoc.Range.Style = "Normal"


listDoc.Range.InsertAfter "My next heading" & vbCr
listDoc.Range.Style = "Heading 1"

listDoc.Range.InsertAfter "My second normal text" & vbCr
listDoc.Range.Style = "Normal"

listDoc.Activate
 
J

Jean-Guy Marcil

Okay- let's try this simplified example without all of my extraneous
code:

Hi all,

I've simplified my range/style example.

I'd like to insert text into a new document (created with
documents.add), making some of the text a "Heading 1" style and some a
"Normal style" -- and then repeating for as many headings as I have.

Given the following snippet, what changes do I have to make?

Dim listDoc As Document

Set listDoc = Documents.Add


listDoc.Range.InsertAfter "My first heading" & vbCr
listDoc.Range.Style = "Heading 1"

listDoc.Range.InsertAfter "My first normal text" & vbCr
listDoc.Range.Style = "Normal"


listDoc.Range.InsertAfter "My next heading" & vbCr
listDoc.Range.Style = "Heading 1"

listDoc.Range.InsertAfter "My second normal text" & vbCr
listDoc.Range.Style = "Normal"

listDoc.Activate

Try this:

Dim listDoc As Document
Dim rngAdd As Range

Set listDoc = Documents.Add
Set rngAdd = listDoc.Range

With rngAdd
.Collapse wdCollapseEnd
.InsertAfter "My first heading" & vbCr
.Style = "Heading 1"
.Collapse wdCollapseEnd
.InsertAfter "My first normal text" & vbCr
.Style = "Normal"
.Collapse wdCollapseEnd
.InsertAfter "My next heading" & vbCr
.Style = "Heading 1"
.Collapse wdCollapseEnd
.InsertAfter "My second normal text" & vbCr
.Style = "Normal"
.Collapse wdCollapseEnd
End With
 
C

caveatRob

Awesome! That works perfectly. I was able to adapt my previous code to
function properly!

Thanks, guys!
 

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