PDA

View Full Version : Adding VBA to my Working Group List template in Word



TrippyTom
12-01-2005, 01:23 PM
Hi everyone,

I have made a "template" in word that has a basic format with tables to show a Working Group List (call list with addresses and phone numbers). Currently, I paste/special as text from whatever source I'm getting the data from (Excel, website, another Word doc, etc). What I would like to do is somehow automate this.

I think I will need vba for this because it's more advanced than just using a style. Certain words need to go into cells in a table. For instance:

The basic layout of a table would be 3 columns:

Name & Title go into the left cell
Tel, Fax, email go into the middle cell
Any home info goes into the right cell (Address, Tel, Cell, email)
Would it be worth trying to VBA this process? Or am I just wishing for too much?

mdmackillop
12-01-2005, 02:13 PM
Hi
Have a look at this KB for some methods of adding data into word. Its not clear if you want to populate 3 cells or 3 columns.
http://vbaexpress.com/kb/getarticle.php?kb_id=184

TrippyTom
12-01-2005, 03:03 PM
That's clever.. and I think I will probably utilize that method in the future, but I've been thinking about my problem and because my data comes from so many different sources, I don't think I will be able to get away from the cut/paste method to get the data IN there.

I just need to write a macro to format the text automatically once the data is in the tables. This would assume, for example, the data on the first line in column 1 should be TimesNewRoman, 9pt, bold (Name). The 2nd line would be TimesNewRoman, 9pt, italic (the person's title). Or can I define styles and use the style's name in the macro?

I'm used to VBA in Excel.. but Word is totally different. :think:

mdmackillop
12-01-2005, 03:40 PM
Here's some code to play around with. You can use styles or code as desired.

Sub Formats()
Dim Rw As Long, Col As Long
With ActiveDocument.Tables(1)
Rw = .Rows.Count
Col = .Columns.Count

.Cell(1, 1).Select
Selection.Style = ActiveDocument.Styles("Heading 1")
For j = 2 To Col
.Cell(1, j).Select
MyFont
Bold
Centre
Next

For i = 2 To Rw
For j = 1 To Col
.Cell(i, j).Select
MyFont
Next
Next
End With
End Sub
Sub Bold()
With Selection.Font
.Name = "Times New Roman"
.Size = 12
.Bold = True
.Italic = True
End With
End Sub
Sub Centre()
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End Sub
Sub MyFont()
With Selection.Font
.Name = "Courier"
.Size = 11
End With
End Sub

TrippyTom
12-02-2005, 12:24 PM
I thank you for that code to play with. Eventually I decided to go with simple text entry macros Tel:[tab](, Fax:[tab](, Cell:[tab]( combined with more styles in Word: header title, division title, myName, myTitle, etc.

It seems to be working ok so far. Still, I appreciate the code you provided because it gave me some insite into how code looks in Word. Thanks again! :thumb