Word automation - selections/formating during automation

  • Thread starter Thread starter Cheese_whiz
  • Start date Start date
C

Cheese_whiz

Hi all,

How do I apply formatting (bold, for example) to text created during a sub
in access that automates word.

For example, this code will open word, create a new document, add the date
(right aligned), start a new paragraph (left aligned). How would I bold the
date from this sub? Is there a way to refer to current sentence or current
paragraph without knowing sentence or paragraph numbers (like using count
property or something)?

Sub OpenWordDoc()

Dim objWord As Word.Application
Dim objDoc As Word.Document

Set objWord = New Word.Application
objWord.Visible = True
Set objDoc = objWord.Documents.Add
objWord.Activate

objWord.Selection.TypeParagraph
objWord.Selection.InsertDateTime
objWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

objWord.Selection.TypeParagraph
objWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
objWord.Selection.TypeText ""

End Sub

Thanks for any help,
CW
 
Hi all,

How do I apply formatting (bold, for example) to text created during a sub
in access that automates word.  

For example, this code will open word, create a new document, add the date
(right aligned), start a new paragraph (left aligned).  How would I boldthe
date from this sub?  Is there a way to refer to current sentence or current
paragraph without knowing sentence or paragraph numbers (like using count
property or something)?

Sub OpenWordDoc()

Dim objWord As Word.Application
Dim objDoc As Word.Document

Set objWord = New Word.Application
objWord.Visible = True
Set objDoc = objWord.Documents.Add
objWord.Activate

objWord.Selection.TypeParagraph
objWord.Selection.InsertDateTime
objWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

objWord.Selection.TypeParagraph
objWord.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
objWord.Selection.TypeText ""

End Sub

Thanks for any help,
CW

should be something like Selection.Format=Bold '<--or whatever the
named constant is in Word. (create a macro, and it'll do the VBA for
you.)
 
Hi pietlinden,
Thanks for the reply.

Well, first I needed to get the selection made, but I actually figured that
out since I posted (sometimes posting spurs thought I guess).

Anyway, now all I need is to be able to move the cursor/selection to just
past the last character in the selection/paragraph. After that I at least
can accomplish what I need for now.

Thanks again,
CW
 
Well, here's what I ended up with though I have no idea if there's a better
way. This seems to work....

Sub OpenWordDoc()

Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim intParaCount As Integer

Set objWord = New Word.Application
objWord.Visible = True
Set objDoc = objWord.Documents.Add
objWord.Activate

With objWord.Selection

.TypeParagraph
.InsertDateTime
.ParagraphFormat.Alignment = wdAlignParagraphRight


.Paragraphs(ActiveDocument.Paragraphs.Count).Range.Select
.Font.Bold = True
.EndKey
.Font.Bold = False


.TypeParagraph
.ParagraphFormat.Alignment = wdAlignParagraphLeft
.TypeText ""

End With

End Sub

Any pointers on how to improve it are welcome, but otherwise I think I am ok
now.

Thanks,
CW
 
Back
Top