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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change font of a Word Textbox from Ms Access

Status
Not open for further replies.

ModestMan

IS-IT--Management
Jun 9, 2005
4
US
Does anyone know how to change the font of a textbox? I know how to change the font of all the text, but I am unsure how to change just part of the text's font.

For instance, let's say I have code:

Public Function CreateStateSummary()
Dim objWordApp As Word.Application
Dim objWord As Word.Document
Dim oSel As Word.Selection
Dim txtTitle As Word.Shape

Set objWordApp = CreateObject("Word.Application")
Set objWord = objWordApp.Documents.Add
Set oSel = objWord.Application.Selection

objWord.Application.Visible = False

Set txtTitle = objWordApp.ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 89, 69, 400, 25)
With txtTitle
With .TextFrame
.TextRange.Font.Name = "Arial"
.TextRange.Font.Bold = True
.TextRange.Font.Size = 18
.TextRange.Text = "TITLE Okay"
End With
End With

objWord.Application.Visible = True
End Function


"TITLE Okay" is the text in the textbox.
I want to keep "TITLE" as Arial, but change "Okay" to Times New Roman

Does anyone know how to do this? I really want to keep them both in the same textbox, otherwise I would make two different textboxes as a workaround.

Thank you all,
Modest
 
Hi Modest,

You could try ...
Code:
        :
        :
        With .TextFrame
            .TextRange.Font.Name = "Arial"
            .TextRange.Font.Bold = True
            .TextRange.Font.Size = 18
            .TextRange.Text = "TITLE Okay"
            [blue].TextRange.Words(2).Font.Name = "Times New Roman"[/blue]
        End With
        :
        :

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[
 
Hmmm, thank you very much! I'm pretty new to creating word files from Access and I appreciate your help. What should I do if the textbox was filled with:
"Hello this is my title This is the font I want to change"

Where I would want
"Hello this is my title" to be one font and
"This is the font I want to change" to be another.

Do I have to do this word by word?
Additionally, how do i change the font of a space " "


Thank you again,
ModestMan
 
Hi ModestMan,

How about this then ..
Code:
    :
    :
    With txtTitle
        With .TextFrame
            .TextRange.Font.Name = "Arial"
            .TextRange.Font.Bold = True
            .TextRange.Font.Size = 18
[blue]            .TextRange.Select
            Selection.TypeText "Hello this is my title"
            Selection.Font.Name = "Times New Roman"
            Selection.TypeText "This is the font I want to change"
[/blue]        End With
    End With
    :
    :
It would be better not to use the Selection but, at the moment, I can't get it to work without.

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[
 
I was asking because I'm unfamiliar how to reference objects in Word. I'm still learning about the variables and classes associated with the Word objects.

I appreciate your help, especially since I can't find much information online... though I haven't done any detailed searches yet.

I assume another way to do this (if I wanted to avoid using a selection) would be to use a count controlled loop. But I would still be unsure about how to change the " " (space) in between each word.



Thank you again for replying yet again.
 
Well, yes and no!

You can only set the font attributes on a Range so you'll need to make sure you have the Range which is each character rather than the text itself. In the body of a document it's all fairly straightforward but, when I was looking at this last night, I couldn't get Word to play ball at all inside the textbox which is why I, somewhat reluctantly, posted the solution using the Selection.

Whatever you do I wouldn't worry overmuch about spaces - they don't vary much from font to font.

The Word object model is not the easiest thing in the world to work with and there is far less information about it on the web than, say, Excel but persevere and you'll get there [smile]

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[
 
I was surprised to see this much control. It very much surprised me. I have a feeling I'm going to need to look into msdn in a little greater detail.

Ahh, fun times.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top