Author: Tomas Rutkauskas
How can I add rows at the end of a wordtable even when I have vertically merged
cells? I always receive the error message "cannot access individual rows in this
collection because the table has vertically merged cells"! The recorded word macro
simple add a row by "selection.insertrows 1", but I have problems converting this
into a Delphi statement (defining the right selection etc.).
Answer:
I've been automating MS Word, using bookmarks. Sometimes I need to write multiple
values to one bookmark. I pass the values to the following routine as comma-text in
the AValue parameter. It works fine with D5 using the Word97 unit and MS Word 2000
executable. Hope it helps.
1 { ... }2 FMSWord := CreateComObject(CLASS_WordApplication) as WordApplication;
3 { ... }4 5 procedure TLTWordDocHandler.PopulateListBookMark(const ABookMarkName:
6 string; const AValue: Widestring);
7 var8 i: integer;
9 LBMName: OleVariant;
10 MoveUnit: OleVariant;
11 NumRows: OleVariant;
12 WorkingList: TStringList;
13 begin14 LBMName := ABookMarkName;
15 FMSWord.ActiveDocument.Bookmarks.Item(LBMName).Select;
16 if FMSWord.Selection.Tables.Count = 0 then17 raise Exception.Create(Format(sBookmarkNotInTable, [ABookmarkName]));
18 MoveUnit := wdCell;
19 NumRows := 1;
20 WorkingList := TStringList.Create;
21 try22 WorkingList.CommaText := AValue;
23 for i := 0 to WorkingList.Count - 1 do24 begin25 FMSWord.Selection.TypeText(WorkingList.Strings[i]);
26 ifnot (i = (WorkingList.Count - 1)) then27 FMSWord.Selection.MoveRight(MoveUnit, EmptyParam, EmptyParam);
28 {97 & 2000 compliant}29 end;
30 finally31 FreeAndNil(WorkingList);
32 end;
33 end;