Write multiple values to a bookmark in Word (Views: 2143)
Problem/Question/Abstract: 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. { ... } FMSWord := CreateComObject(CLASS_WordApplication) as WordApplication; { ... } procedure TLTWordDocHandler.PopulateListBookMark(const ABookMarkName: string; const AValue: Widestring); var i: integer; LBMName: OleVariant; MoveUnit: OleVariant; NumRows: OleVariant; WorkingList: TStringList; begin LBMName := ABookMarkName; FMSWord.ActiveDocument.Bookmarks.Item(LBMName).Select; if FMSWord.Selection.Tables.Count = 0 then raise Exception.Create(Format(sBookmarkNotInTable, [ABookmarkName])); MoveUnit := wdCell; NumRows := 1; WorkingList := TStringList.Create; try WorkingList.CommaText := AValue; for i := 0 to WorkingList.Count - 1 do begin FMSWord.Selection.TypeText(WorkingList.Strings[i]); if not (i = (WorkingList.Count - 1)) then FMSWord.Selection.MoveRight(MoveUnit, EmptyParam, EmptyParam); {97 & 2000 compliant} end; finally FreeAndNil(WorkingList); end; end; |