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!
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!