Automating Word from script

J

Joel Allen

Outlook 2003 SP3

Hi,

I currently automate a Word document via my script from my custom outlook
task. I use bookmarks and it works great.

However, now I have a need now to insert tables, change font size, etc.....
I'm having trouble with the basics like trying to insert text into a certain
cell then tabbing to the next cell. I tried vbTab but no luck.

Does anybody have a good reference to learn from? I'm looking for sample
code and maybe sample Word files and perhaps a list of vb commmands. I'm
only aware of a few like vbTab and vbCrLf.

Thank you very much,
Joel
 
K

Ken Slovak - [MVP - Outlook]

A good Word programming book might help. The Word MVPs have a Web site that
has a lot of information and code samples at http://word.mvps.org/ that
might also be helpful to you.
 
S

Sue Mosher [MVP]

The easiest way to get sample code is to turn on Word's macro recorder as
you perform the tasks you want to accomplish. Then, look at the macro, see
what objects are are involved, check out their methods, etc.
 
J

Joel Allen

Thank you Sue. I think this will help a lot.

Now, I started simple with this:

Selection.Font.Name = "Calibri"
and
objDoc.Selection.Font.Size = 11

Neither work. I think I need to set some variables or something. Do you
have some sample code of that? I think if I can get that far, I can take it
on my own.

Again, thanks always for your help,
Joel
 
J

Joel Allen

I made some progress using your blog and the macro recorder in Word. I got
selection to work with a few minor tweaks.

Now I'm trying to insert tables and format them and having really hard
time. I have to guess at the formatting. For instance, this is what the
Word macro shows:

--------------------
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
------------------------

then I'm just trying to start simple by adding just one row of a table by
doing this in my task code:

---------------
objTable.Add Range:=objSel.Range, NumRows:=1, NumColumns:= 5

or

objTable.Add Range:=objSel.Range, NumRows:=1, NumColumns:= 5

objTable.Rows.Add()
objSel.Range, NumRows=1, NumColumns=5

or
 
J

Joel Allen

I made some progress using your blog and the macro recorder in Word. I got
selection to work with a few minor tweaks. Works great.

Now, I'm trying to insert tables and format them and am having a really hard
time. I have to guess at the formatting. For instance, this is what the
Word macro shows:

--------------------
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
------------------------

Then I'm just trying to start simple by adding just one row of a table by
doing this in my task code:

---------------
objTable.Add Range:=objSel.Range, NumRows:=1, NumColumns:= 5

or

objTable.Add Range=objSel.Range, NumRows=1, NumColumns= 5

or

objTable.Add Range
objSel.Range, NumRows=1, NumColumns= 5
---------------

None of these work. It's hard to figure the conversion. Is there somewhere
where I can look at the proper format?

Thank you Sue,
Joel
 
S

Sue Mosher [MVP]

Outlook doesn't recognize ActiveDocument. So, you need a Word.Document
object; the article I suggested earlier shows how to get that from a
message.

For the sake of discussion, let's say you have a Word.Document object
variable named objDoc. As your recorded macro suggests, any Document has a
Tables collection. As with other Office objects that have an Add method,
adding a table returns the newly created Table object, e.g.:

Set newTable = objDoc.Tables.Add 'etc., using parameters like those in
your code below

Once you have the newTable object, then you can manipulate it using the
Style, ApplyStyleHeadingRows, and other properties that the macro recorder
helped you find.

Remember to use the object browser (F2 in VBA) to look up any properties and
methods you don't understand and read about them in Help, where you'll
probably find more complete examples to supplement what the macro recorder
gives you. For example, since you want to add a row to the table, the object
browser can show you that the Table object has a Rows property, which in
turn has an Add method. So you can add a new row simply with:

Set newRow = newTable.Rows.Add

Inserting text in a cell in that row looks like this:

newRow.Cells(1).Range.InsertAfter "some text"
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
J

Joel Allen

Thanks Sue. This was really helpful. Thanks for taking the time to give me
that explanation.

Joel
 

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