给你一段我操作word文档的代码参考一下吧,版本是2003,希望有帮助
procedure TFrmEdit.btnCreateDocClick(Sender: TObject);
function Load2: boolean;
var
Blob: TStream;
begin
result := True;
if query.FieldByName('Content').IsNull then
begin
result := False;
exit;
end;
Blob := TMemoryStream.Create;
try
TBlobField(query.FieldByName('Content')).SaveToStream(Blob);
Blob.Seek(0, soFromBeginning);
if Blob.Size = 0 then
begin
result := False;
Exit;
end;
olecontainer2.LoadFromStream(Blob);
finally
Blob.Free;
end;
end;
var
i: Integer;
p,q,p1,p3: Olevariant;
m: TOleEnum;
isfirst: boolean;
begin
with query do
begin
close;
sql.Clear;
sql.Add('select * from tbCatalog where Tag<>0 order by Index ASC');
execsql;
open;
tree.Selected := nil;
olecontainer1.Close;
OleContainer1.CreateObject('Word.Document.8',False);
OleContainer1.AutoActivate := aaGetFocus;
OleContainer1.SetFocus;
OleContainer2.CreateObject('Word.Document.8',False);
isfirst := true;
while not eof do
begin
if Load2 then
begin
OleContainer2.SaveAsDocument('c:/~tmp.tmp');
p := false; q := '';
word.Selection.InsertFile('c:/~tmp.tmp',q,p,p,p);
{if isFirst then
begin
p := True; q:=''; p1:=1;p3:=3;
word.ActiveDocument.TablesOfContents.Add(word.Selection.Range,p,p1,p3,EmptyParam,EmptyParam,p,p,q,p,p,p);
isFirst := false;
end;}
end;
next;
end;
//word.Run('ActiveDocument.Protect Password:="1", NoReset:=False, Type:=wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False');
//word.Run('Selection.WholeStory');
word.ActiveDocument.TablesOfContents.Item(1).update;
m := 3; q:='!@#$%^';
word.ActiveDocument.Protect(m,p,q);
end;
{
With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:="", _
UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
True
.TablesOfContents(1).TabLeader = wdTabLeaderSpaces
.TablesOfContents.Format = wdIndexIndent
End With
}
{
ActiveDocument.Protect Password:="1", NoReset:=False, Type:= _wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False
}
{WordBasic.UpdateTableOfContents
With ActiveDocument
.ReadOnlyRecommended = False
.Password = "111"
.WritePassword = ""
.RemovePersonalInformation = False
.RemoveDateAndTime = False
End With}
{
可以把第一个放到一个wordapp变量中,然后用
wordApp.selection.EndKey(wdStory);//把光标定到最后
如果另一个是文件就:
WordApp.Selection.InsertFile(fn, '', False, false, false);
你放数据库中的,是否可以内容2放到粘贴板中,这样:
wordapp.selection.paste;
我对数据库操作work文档不太熟,呵呵
你的blob存的是word文件吧
先用ole(word)打开流1(记录1)
再用
WordApp.Selection.InsertFile(流2文件名, '', False, false, false);
}
end;
procedure TFrmEdit.LoadTmp;
var
j
levariant;
i: Integer;
pnode,node: TTreeNode;
str: String;
pagenum: string;
data: Tvar;
path: string;
IOO: IOleInPlaceObject ;
begin
screen.Cursor := crHourGlass;
if tmpName <> '' then
begin
path := tmpName;
edtPath.Text := path;
end else
begin
path := docName;
edtPath.Text := '(未保存)';
end;
ado.ConnectionString :=
'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+path+';Persist Security Info=False;Jet OLEDB
atabase Password=!@#$%^;';
ad
pen;
//OleContainer1.CreateObjectFromFile(rzedit1.Text,false);
OleContainer1.CreateObject('Word.Document.8',False);
///////OleContainer1.LoadFromFile(path);
{OleContainer1.DoVerb(ovInPlaceActivate);//(ovShow);
if OleContainer1.State in [osUIActive] then
begin
OleContainer1.OleObjectInterface.QueryInterface(IOleInPlaceObject,IOO);
IOO.ReactivateAndUndo;
IOO :=nil;
end;}
OleContainer1.AutoActivate := aaGetFocus;
OleContainer1.SetFocus;
{if word.CommandBars.ActiveMenuBar.Enabled then begin
with word.CommandBars.ActiveMenuBar do
begin
for i := 1 to Controls.Count do
Controls
.Set_Visible(true); //删除菜单,实际是隐藏
//Controls[2].Set_Visible(true);
//Controls[4].Set_Visible(true);
//Controls[5].Set_Visible(true);
// Controls.Set_Visible(true);
//Controls[Controls.Count].Set_Visible(False);
end;
end;
// if word.CommandBars.ActiveMenuBar.Enabled then begin
with word.CommandBars do
begin
for i := 1 to 5 do
begin
j := i;
Item[j].Set_Visible(True);
end;
end;
// end; }
word.ActiveWindow.View.ShowFieldCodes := False;
{pnode := tree.Items.AddChild(nil,'目录');
for i := 1 to word.ActiveDocument.Hyperlinks.Count do
begin
j := i;
str := word.ActiveDocument.Hyperlinks.Item(j).Range.Text;
pagenum := ExtractPageNum(str);
str := copy(str,1,length(str)-length(pagenum));
data := Tvar.Create;
try
data.value := word.ActiveDocument.Hyperlinks.Item(j).SubAddress;
except
data.value := '';
end;
node := tree.Items.AddChildObject(pnode,str,data);
//memo1.Lines.Add( WordApplication1.ActiveDocument.Hyperlinks.Item(j).Range.Text);
end;
tree.Items.GetFirstNode.Expanded := true; }
//读取目录树
CreateTree;
screen.Cursor := crDefault;
end;
procedure TFrmEdit.PrintView;
var
IOO: IOleInPlaceObject ;
begin
OleContainer1.DoVerb(ovShow);
if OleContainer1.State in [osUIActive] then
begin
OleContainer1.OleObjectInterface.QueryInterface(IOleInPlaceObject,IOO);
IOO.UIDeactivate;
IOO :=nil;
end;
OleContainer1.OleObject.PrintPreview;
OleContainer1.DoVerb(ovShow);
OleContainer1.Align := OleContainer1.Align;
end;