Outlook doesn't recognize ActiveDocument. So, you need a Word.Document
object; the article I suggested earlier shows how to get that from a
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"
"Joel Allen" <firstname.lastname@example.org> wrote in message
> 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
> 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:= _
> 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
> objTable.Add Range=objSel.Range, NumRows=1, NumColumns= 5
> 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,
> "Sue Mosher [MVP]" <email@example.com> wrote in message
> > Outlook doesn't know anything about any Selection object that comes from
> > a Word document, so you have to add the "missing links" that indicate
> > where that Selection is coming from. See
> > http://turtleflock-ol2007.spaces.live.com/blog/cns!C1013F1F9A99E3D8!579.entry
> > for an example.
>> > > Sue Mosher
> > >> >> >
>> "Joel Allen" <firstname.lastname@example.org> wrote in message
> > news:uGzXCTDIKHA.4316@TK2MSFTNGP04.phx.gbl...
> >> 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
>>> "Sue Mosher [MVP]" <email@example.com> wrote in message
> >> news:%23Jvc$1CIKHA.4500@TK2MSFTNGP02.phx.gbl...
> >>> 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.
>>>> "Joel Allen" <firstname.lastname@example.org> wrote in message
> >>> news:uA3AdqBIKHA.1376@TK2MSFTNGP02.phx.gbl...
> >>>> Outlook 2003 SP3
>>>>> 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.