Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change font in Word with VBA 1

Status
Not open for further replies.

andols

Programmer
Aug 30, 2005
12
SE
Hi,

I am using VBA to open a new Word document from another aplication.

I am allso adding some text, and I want to change font, bold, italic, alignment and so on.

I can change the font-name and -size but not if it Bold or the alignemnt. I am also trying to underline a row and add a table, but I can bet i to work.

Code:
Set wdApp = CreateObject("Word.Application")
'Add a document.
Set wdDoc = wdApp.Documents.Add
    wdApp.Visible = True
    wdApp.Selection.Font.size = 16 'this works
    wdApp.Selection.Font.Name = "Arial"
    wdApp.Selection.Font.Name = "Arial" 'this works
    wdApp.Font.Bold = wdToggle 'this dosent work
    wdApp.Selection.ParagraphFormat.Alignment = _ wdAlignParagraphCenter 'this dosent work

'This doesn't work
    wdApp.Selection.Borders(wdBorderBottom).LineStyle = Options.DefaultBorderLineStyle
    wdApp.Selection.Borders(wdBorderBottom).LineWidth = Options.DefaultBorderLineWidth
    wdApp.Selection.Borders(wdBorderBottom).Color = Options.DefaultBorderColor

    'this doesnt work
    wdApp.ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:= _
        4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed

Maybe it cant be done from the program I am in?

//Anders Olsson
 
Hi Anders,

All you are doing is sending commands to Word; it shouldn't make any difference where they are coming from.

I think your problem is probably that you don't have the Word object library referenced.

Try using the numeric values instead of the Word constants ..
Code:
wdApp.Font.Bold = 9999998
[green]' or[/green]
wdApp.Font.Bold = True
Code:
wdApp.Selection.ParagraphFormat.Alignment = [red]1[/red]
Code:
wdApp.ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:= _
        4, DefaultTableBehavior:=[red]1[/red], AutoFitBehavior:=[red]0[/red]

Finally try using
Code:
[i](border bits)[/i] = [red]wdApp.[/red]Options.etc

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Thanx

The font and the underline works but not the table.

//Anders
 
Oops, missed that one, sorry.

Try changing to
Code:
wdApp.ActiveDocument.Tables.Add Range:=[red]wdApp.[/red]Selection.Range, NumRows:=4, NumColumns:= _
        4, DefaultTableBehavior:=1, AutoFitBehavior:=0

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top