贴几段让你参考一下:
procedure TForm1.btnOpenClick(Sender: TObject);
var iLoop: integer;
vLoop: Olevariant;
s: String;
begin
if OpenFile.Execute Then
begin
s:= OpenFile.FileName;
if (Rightstr(OpenFile.FileName,3)='doc') or
(Rightstr(OpenFile.FileName,3)='dot') then 检查文件是否Word文档
begin
OleContainer1.Enabled:=true;
OleContainer1.CreateObjectFromFile(OpenFile.FileName,false);
OleContainer1.DoVerb(ovShow); //运行此句,才可以启动Word进程,才能让WordApp连上
WordApp.Connect;
WordApp.Tag:=9595; //作一个标记
WordDoc.ConnectTo(WordApp.ActiveDocument);
WordApp.CommandBars['Standard'].Set_Enabled(false); //将工具栏设为不可见
wordApp.CommandBars['Formatting'].Set_Enabled(false);
OleContainer1.DoVerb(ovShow); //运行此句让工具栏不再出现
btnOpen.Enabled:=false;
Form1.OnResize:=FormRizeDo;
for iLoop:=1 to Worddoc.Bookmarks.Count do //将标签加到ComboBox1中
begin
vLoop:=iLoop;
ComboBox1.Items.Add(Worddoc.Bookmarks.item(vLoop).name);
end;
btnChange.Enabled:=true;
btnEdit.Enabled:=true;
btnSave.Enabled:=true;
btnDone.Enabled:=true;
btnOk.Enabled:=false;
btnModify.Enabled:=false;
end;
end;
end;
procedure TForm1.btnOkClick(Sender: TObject);//建立对应的XML文件
var iLoop: integer;
InsertNode: IXMLNode;
rowNode: IXMLNode;
sItem: TListItem;
AttrValue: OleVariant;
begin
if ListView1.MultiSelect=true Then //ListView是数据表的Fieldname;
begin
if ListView1.SelCount>0 Then
begin
InsertNode:=XMLFormWork.CreateElement('插入表','');
AttrValue:=ComboBox1.Text;
InsertNode.SetAttribute('BMarkName',AttrValue);
AttrValue:=ComboBox2.Text;
InsertNode.SetAttribute('TableName',AttrValue);
sItem:=ListView1.Selected;
while sItem<>nil Do
begin
RowNode:=XMLFormWork.CreateElement('列','');
AttrValue:=sItem.Caption;
RowNode.SetAttribute('FieldName',AttrValue);
InsertNode.ChildNodes.Add(RowNode);
sItem:=ListView1.GetNextItem(sItem, sdAll, [isSelected]);
end;
if Not FindByAttr('BMarkName',ComboBox1.Text,XmlFormWork.ChildNodes.First.NextSibling) Then
XmlFormWork.ChildNodes.First.NextSibling.ChildNodes.Add(InsertNode);
end;
end else
begin
if ListView1.SelCount>0 Then
begin
InsertNode:=XMLFormWork.CreateElement('插入点','');
AttrValue:=ComboBox1.Text;
InsertNode.SetAttribute('BMarkName',AttrValue);
AttrValue:=ComboBox2.Text;
InsertNode.SetAttribute('TableName',AttrValue);
sItem:=ListView1.Selected;
AttrValue:=sItem.Caption;
InsertNode.SetAttribute('FieldName',AttrValue);
if Not FindByAttr('BMarkName',ComboBox1.Text,XmlFormWork.ChildNodes.First.NextSibling) Then
XmlFormWork.ChildNodes.First.NextSibling.ChildNodes.Add(InsertNode);
end;
end;
if XMLFormWork.ChildNodes.First.NextSibling.ChildNodes.Count = ComboBox1.Items.Count Then
Begin
if SaveFile.Execute Then XmlFormWork.SaveToFile(SaveFile.FileName);
end;
btnOk.Enabled:=false;
btnInsert.Enabled:=false;
btnInsertTable.Enabled:=false;
ComboBox1.Enabled:=true;
ListView1.Enabled:=false;
end;