H
hsb0307
Unregistered / Unconfirmed
GUEST, unregistred user!
RichEdit的内容全部复制到一个word新文档中去并保存为一个doc文件,
相当于将RichEdit的内容到处为一个word文档,第一次执行没问题,第二次执行就出错,
“服务器出现意外”或“RPC...”
请指正。
代码如下:
procedure TForm1.Button2Click(Sender: TObject);
var
Template, NewTemplate,DocType,DocVisible : OleVariant;
ItemIndex,Format,FileName: OleVariant;
SaveChanges: Olevariant;
begin
Editor.SelectAll;
Editor.CopyToClipboard;
Editor.SelStart := Editor.SelLength;
Template := EmptyParam;
NewTemplate := EmptyParam;
DocType := EmptyParam;
DocVisible := False;//EmptyParam;
WordApp.Connect; // WordApp : TWordApplication
WordApp.Visible := False;
WordApp.Documents.Add(Template,NewTemplate,DocType,DocVisible);
ItemIndex := 1;
WordDoc.ConnectTo(WordApp.Documents.Item(ItemIndex));
//WordApp.Selection.Paste;
//WordDoc.Content.Paste;
WordDoc.Range.Paste;//[blue]此句在第二次执行时出问题。[/blue]
//WordDoc.Sentences.Last.Paste;
Format := wdFormatDocument;//wdFormatRTF;
if OpenDialog1.Execute then
FileName:=OpenDialog1.FileName;
if FileName='' then Exit;
WordDoc.SaveAs(FileName,Format);
WordDoc.Close;//[red]此句有没有用?[/red]
WordDoc.Disconnect;//[blue]此句有没有用?[/blue]
SaveChanges := wdDoNotSaveChanges;//wdSaveChanges;
try
WordApp.Quit(SaveChanges);
WordApp.Disconnect;
except
on e: exception do
begin
ShowMessage(e.message);
WordApp.Disconnect;
end;
end;
end;
请大家帮我看看这段代码:
用下面的代码,也是第一次执行没问题,第二次执行就出错,
“服务器出现意外”或“RPC...”
难道WordApp.Free;没执行?
function EditExportDoc(FileName:Olevariant;Editor:TJvCustomRichEdit):Boolean;
var
WordApp: TWordApplication;
WordDoc: TWordDocument;
Template, NewTemplate,DocType,DocVisible : OleVariant;
ItemIndex,Format: OleVariant; // ,FileName
SaveChanges: Olevariant;
begin
Result:=False;
Editor.SelectAll;
Editor.CopyToClipboard;
Editor.SelStart := Editor.SelLength;
Template := EmptyParam;
NewTemplate := EmptyParam;
DocType := EmptyParam;
DocVisible := False;//EmptyParam;
WordApp:=TWordApplication.Create(nil);
try
WordApp.Connect;
except
ShowMessage('您的电脑没装Word!');
Abort;
end;
WordApp.Visible := False;
WordApp.Documents.Add(Template,NewTemplate,DocType,DocVisible);
WordDoc:=TWordDocument.Create(nil);
ItemIndex := 1;
WordDoc.ConnectTo(WordApp.Documents.Item(ItemIndex));//ActiveDocument
//WordDoc.ConnectTo(WordApp.ActiveDocument);//ActiveDocument
WordDoc.Range.Paste;
Format := wdFormatDocument;//wdFormatRTF;
WordDoc.SaveAs(FileName,Format);
WordDoc.Close;
WordDoc.Disconnect;
WordDoc.Free;
SaveChanges := wdDoNotSaveChanges;
try
WordApp.Quit(SaveChanges);
WordApp.Disconnect;
except
on e: exception do
begin
ShowMessage(e.message);
WordApp.Disconnect;
end;
end;
WordApp.Free;
Result:=True;
end;
相当于将RichEdit的内容到处为一个word文档,第一次执行没问题,第二次执行就出错,
“服务器出现意外”或“RPC...”
请指正。
代码如下:
procedure TForm1.Button2Click(Sender: TObject);
var
Template, NewTemplate,DocType,DocVisible : OleVariant;
ItemIndex,Format,FileName: OleVariant;
SaveChanges: Olevariant;
begin
Editor.SelectAll;
Editor.CopyToClipboard;
Editor.SelStart := Editor.SelLength;
Template := EmptyParam;
NewTemplate := EmptyParam;
DocType := EmptyParam;
DocVisible := False;//EmptyParam;
WordApp.Connect; // WordApp : TWordApplication
WordApp.Visible := False;
WordApp.Documents.Add(Template,NewTemplate,DocType,DocVisible);
ItemIndex := 1;
WordDoc.ConnectTo(WordApp.Documents.Item(ItemIndex));
//WordApp.Selection.Paste;
//WordDoc.Content.Paste;
WordDoc.Range.Paste;//[blue]此句在第二次执行时出问题。[/blue]
//WordDoc.Sentences.Last.Paste;
Format := wdFormatDocument;//wdFormatRTF;
if OpenDialog1.Execute then
FileName:=OpenDialog1.FileName;
if FileName='' then Exit;
WordDoc.SaveAs(FileName,Format);
WordDoc.Close;//[red]此句有没有用?[/red]
WordDoc.Disconnect;//[blue]此句有没有用?[/blue]
SaveChanges := wdDoNotSaveChanges;//wdSaveChanges;
try
WordApp.Quit(SaveChanges);
WordApp.Disconnect;
except
on e: exception do
begin
ShowMessage(e.message);
WordApp.Disconnect;
end;
end;
end;
请大家帮我看看这段代码:
用下面的代码,也是第一次执行没问题,第二次执行就出错,
“服务器出现意外”或“RPC...”
难道WordApp.Free;没执行?
function EditExportDoc(FileName:Olevariant;Editor:TJvCustomRichEdit):Boolean;
var
WordApp: TWordApplication;
WordDoc: TWordDocument;
Template, NewTemplate,DocType,DocVisible : OleVariant;
ItemIndex,Format: OleVariant; // ,FileName
SaveChanges: Olevariant;
begin
Result:=False;
Editor.SelectAll;
Editor.CopyToClipboard;
Editor.SelStart := Editor.SelLength;
Template := EmptyParam;
NewTemplate := EmptyParam;
DocType := EmptyParam;
DocVisible := False;//EmptyParam;
WordApp:=TWordApplication.Create(nil);
try
WordApp.Connect;
except
ShowMessage('您的电脑没装Word!');
Abort;
end;
WordApp.Visible := False;
WordApp.Documents.Add(Template,NewTemplate,DocType,DocVisible);
WordDoc:=TWordDocument.Create(nil);
ItemIndex := 1;
WordDoc.ConnectTo(WordApp.Documents.Item(ItemIndex));//ActiveDocument
//WordDoc.ConnectTo(WordApp.ActiveDocument);//ActiveDocument
WordDoc.Range.Paste;
Format := wdFormatDocument;//wdFormatRTF;
WordDoc.SaveAs(FileName,Format);
WordDoc.Close;
WordDoc.Disconnect;
WordDoc.Free;
SaveChanges := wdDoNotSaveChanges;
try
WordApp.Quit(SaveChanges);
WordApp.Disconnect;
except
on e: exception do
begin
ShowMessage(e.message);
WordApp.Disconnect;
end;
end;
WordApp.Free;
Result:=True;
end;