|
|
|
date: Wed, 26 Mar 2008 14:49:01 -0700 (PDT),
group: microsoft.public.word.vba.beginners
back
applying different font styles
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
date: Wed, 26 Mar 2008 14:49:01 -0700 (PDT)
author: unknown
RE: applying different font styles
"tborthwick@gmail.com" wrote:
> 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!
date: Fri, 28 Mar 2008 11:56:00 -0700
author: Jean-Guy Marcil
Re: applying different font styles
Access my profile Details in:
http://al-bernardes.sites.uol.com.br/
http://www.linkedin.com/in/andrebernardes
http://andrebernardes.myplaxo.com/
André Luiz Bernardes
On 28 mar, 15:56, Jean-Guy Marcil
wrote:
> "tborthw...@gmail.com" wrote:
> > 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 theVBAeditor.
> 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 theVBAhelp on the Range object and some of its associated
> properties/methods such as ".InsertAfter."
>
> Good luck!
date: Fri, 28 Mar 2008 12:19:14 -0700 (PDT)
author: InAnyPlace
|
|