applying different font styles

T

tborthwick

Hello,

I have a script that reads several items in a loop and creates a
header then a table for each. The header and the table text should
have different styles, but all the text gets the style I first set for
the header. In the script below you can see I try to set the font size
to 10 before adding a table, but the size stays at 12, which is set
earlier. Is it because I'm using the same range? I tried using two
range variable, one for the header and one for the table, but it
didn't change anything. Any help would be appreciated.


Thanks,

Tom


Dim attr As AttributeObj
Dim myRange As Object

Sub Main

Dim Word As Object
Dim Docs As Object
Dim WordBasic As Object
Dim ActiveDoc As Object

Dim diag As Diagram
Dim mdl As Model
Dim sm As SubModel
Dim so As SelectedObject
Dim id As Integer
Dim ent As Entity
Dim attr As AttributeObj


Set Word = CreateObject("Word.Application")
Word.Visible = True
Word.Options.CheckGrammarAsYouType = False
Word.Options.CheckSpellingAsYouType = False

Set ActiveDoc = Word.Documents.Add()

Set diag = DiagramManager.ActiveDiagram
Set mdl = diag.ActiveModel
Set sm = mdl.ActiveSubModel

For Each so In sm.SelectedObjects
If so.Type = 1 Then
id = so.ID
Set ent = mdl.Entities.Item(id)

'Write out header
Set myRange = ActiveDoc.Range
myRange.Font.Size = 12
myRange.Collapse 0
myRange.InsertAfter ent.EntityName & vbCrLf

If ent.Note <> "" Then
Set myRange = ActiveDoc.Range
myRange.Collapse 0
myRange.Font.Size = 10

myRange.InsertAfter ent.Note & vbCrLf
End If

Set myRange = ActiveDoc.Range
myRange.Font.Size = 10
myRange.Collapse 0
Set objTable = myRange.Tables.Add(Range:=myRange,
NumRows:=ent.Attributes.Count, NumColumns:=3)

Dim curRow As Integer
curRow = 1

'Add column headings
objTable.Cell(curRow, 1).Range.Text = "Column name"
objTable.Cell(curRow, 2).Range.Text = "Datatype"
objTable.Cell(curRow, 3).Range.Text = "Notes"
curRow = curRow + 1

For Each attr In ent.Attributes
objTable.Cell(curRow, 1).Range.Text = attr.ColumnName
objTable.Cell(curRow, 2).Range.Text = Datatype(attr.Datatype,
attr.DataLength)
objTable.Cell(curRow, 3).Range.Text = attr.Notes

curRow = curRow + 1
Next

'4, 27, 36
objTable.autoFormat(36)
objTable.Columns(1).Width = 135
objTable.Columns(2).Width = 80
objTable.Columns(3).Width = 235


Set myRange = ActiveDoc.Range
myRange.Collapse 0
myRange.InsertAfter vbCrLf

End If
Next
End Sub
 
J

Jean-Guy Marcil

Hello,

I have a script that reads several items in a loop and creates a
header then a table for each. The header and the table text should
have different styles, but all the text gets the style I first set for
the header. In the script below you can see I try to set the font size
to 10 before adding a table, but the size stays at 12, which is set
earlier. Is it because I'm using the same range? I tried using two
range variable, one for the header and one for the table, but it
didn't change anything. Any help would be appreciated.


Thanks,

Tom


Dim attr As AttributeObj
Dim myRange As Object

Sub Main

Dim Word As Object
Dim Docs As Object
Dim WordBasic As Object
Dim ActiveDoc As Object

Dim diag As Diagram
Dim mdl As Model
Dim sm As SubModel
Dim so As SelectedObject
Dim id As Integer
Dim ent As Entity
Dim attr As AttributeObj


Set Word = CreateObject("Word.Application")
Word.Visible = True
Word.Options.CheckGrammarAsYouType = False
Word.Options.CheckSpellingAsYouType = False

Set ActiveDoc = Word.Documents.Add()

Set diag = DiagramManager.ActiveDiagram
Set mdl = diag.ActiveModel
Set sm = mdl.ActiveSubModel

For Each so In sm.SelectedObjects
If so.Type = 1 Then
id = so.ID
Set ent = mdl.Entities.Item(id)

'Write out header
Set myRange = ActiveDoc.Range
myRange.Font.Size = 12
myRange.Collapse 0
myRange.InsertAfter ent.EntityName & vbCrLf

If ent.Note <> "" Then
Set myRange = ActiveDoc.Range
myRange.Collapse 0
myRange.Font.Size = 10

myRange.InsertAfter ent.Note & vbCrLf
End If

Set myRange = ActiveDoc.Range
myRange.Font.Size = 10
myRange.Collapse 0
Set objTable = myRange.Tables.Add(Range:=myRange,
NumRows:=ent.Attributes.Count, NumColumns:=3)

Dim curRow As Integer
curRow = 1

'Add column headings
objTable.Cell(curRow, 1).Range.Text = "Column name"
objTable.Cell(curRow, 2).Range.Text = "Datatype"
objTable.Cell(curRow, 3).Range.Text = "Notes"
curRow = curRow + 1

For Each attr In ent.Attributes
objTable.Cell(curRow, 1).Range.Text = attr.ColumnName
objTable.Cell(curRow, 2).Range.Text = Datatype(attr.Datatype,
attr.DataLength)
objTable.Cell(curRow, 3).Range.Text = attr.Notes

curRow = curRow + 1
Next

'4, 27, 36
objTable.autoFormat(36)
objTable.Columns(1).Width = 135
objTable.Columns(2).Width = 80
objTable.Columns(3).Width = 235


Set myRange = ActiveDoc.Range
myRange.Collapse 0
myRange.InsertAfter vbCrLf

End If
Next
End Sub

I do not have time to study you code in details right now, but if you will
allow me, let me make one observation and one recommendation which might help
you solve your problem on your own.

Observation:

I see that you are using the range object, which is excellent.
But, you use:
'Write out header
Set myRange = ActiveDoc.Range
myRange.Font.Size = 12
(...)

and a few lines lower, you use:
Set myRange = ActiveDoc.Range
myRange.Font.Size = 10

You are effectively setting the whole document to a font size of 12, and
then the whole document again to a font of 10...

Recommendation:
Sprinkle a few
myRange.Select statement
in your code; and/or use the local window in the VBA editor.
Then, debug your code step by step with F8 key and see how your Range object
changes. You have to code so that the Range object points only to part you
want before executing any properties or methods.
Also, read the VBA help on the Range object and some of its associated
properties/methods such as ".InsertAfter."

Good luck!
 

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