帮忙看一下这段代码 ( 积分: 100 )

  • 主题发起人 主题发起人 espp
  • 开始时间 开始时间
E

espp

Unregistered / Unconfirmed
GUEST, unregistred user!
function TForm1.opendoc(filename: OleVariant): boolean;
var
ItemIndex: OleVariant;
ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible1: OleVariant;
begin
{Opendo
cument}
result := true;
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := '';
PasswordTemplate := '';
Revert := True;
WritePasswordDocument := '';
WritePasswordTemplate := '';
Format := wdOpenFormatDocument;
try
WordApplication.Disconnect;
WordApplication.Connect;
WordDocument.ConnectTo(WordApplication.Documents.Open(FileName, ConfirmConversions, ReadOnly,
AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible1));
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;

except
result := false;
end;
end;

function TForm1.replacedoc(text, rtext: string): boolean;
var
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
MatchAllWordForms, Forward1, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics,
MatchAlefHamza, MatchControl: OleVariant;
begin
FindText := text;
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward1 := True;
Wrap := wdFindContinue;
Format := False;
ReplaceWith := rtext;
Replace := True;
result := WordDocument.Range.Find.Execute(FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward1,
Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics,
MatchAlefHamza, MatchControl);
end;

procedure TForm1.closedoc;
var
SaveChanges, OriginalFormat, RouteDocument: OleVariant;
begin
SaveChanges := WdDoNotSaveChanges;
//wdSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
WordApplication.Quit(SaveChanges);
WordApplication.Disconnect;
except
// on E: Exceptiondo
// begin
// Showmessage(E.Message);
WordApplication.Disconnect;
// end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
FileName1: OleVariant;
i: integer;
begin
ADOQUERY1.First;
if opendoc('D:/测试程序/word打印/建设工程测量通知书.doc') = false then
begin
closedoc;
end;
filename1 := 'D:/测试程序/word打印/TEST.DOC';
replacedoc('jsdw', ADOQUERY1.fieldbyname('dwmc').AsString);
replacedoc('BJBH', ADOQUERY1.fieldbyname('BJBH').AsString);
replacedoc('XMMC', ADOQUERY1.fieldbyname('XMMC').AsString);
replacedoc('XMDZ', ADOQUERY1.fieldbyname('XMDZ').AsString);
try
WordDocument.SaveAs(FILENAME1);
WORDDOCUMENT.Printout;//[red]为什么不能打印这个文件?[/red]
finally
closedoc;
application.ProcessMessages;
end;
end;
还有问题就是怎么控制显示word打印的预览?
怎么控制用哪个打印机打印?
 
function TForm1.opendoc(filename: OleVariant): boolean;
var
ItemIndex: OleVariant;
ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible1: OleVariant;
begin
{Opendo
cument}
result := true;
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := '';
PasswordTemplate := '';
Revert := True;
WritePasswordDocument := '';
WritePasswordTemplate := '';
Format := wdOpenFormatDocument;
try
WordApplication.Disconnect;
WordApplication.Connect;
WordDocument.ConnectTo(WordApplication.Documents.Open(FileName, ConfirmConversions, ReadOnly,
AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible1));
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;

except
result := false;
end;
end;

function TForm1.replacedoc(text, rtext: string): boolean;
var
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
MatchAllWordForms, Forward1, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics,
MatchAlefHamza, MatchControl: OleVariant;
begin
FindText := text;
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward1 := True;
Wrap := wdFindContinue;
Format := False;
ReplaceWith := rtext;
Replace := True;
result := WordDocument.Range.Find.Execute(FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward1,
Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics,
MatchAlefHamza, MatchControl);
end;

procedure TForm1.closedoc;
var
SaveChanges, OriginalFormat, RouteDocument: OleVariant;
begin
SaveChanges := WdDoNotSaveChanges;
//wdSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
WordApplication.Quit(SaveChanges);
WordApplication.Disconnect;
except
// on E: Exceptiondo
// begin
// Showmessage(E.Message);
WordApplication.Disconnect;
// end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
FileName1: OleVariant;
i: integer;
begin
ADOQUERY1.First;
if opendoc('D:/测试程序/word打印/建设工程测量通知书.doc') = false then
begin
closedoc;
end;
filename1 := 'D:/测试程序/word打印/TEST.DOC';
replacedoc('jsdw', ADOQUERY1.fieldbyname('dwmc').AsString);
replacedoc('BJBH', ADOQUERY1.fieldbyname('BJBH').AsString);
replacedoc('XMMC', ADOQUERY1.fieldbyname('XMMC').AsString);
replacedoc('XMDZ', ADOQUERY1.fieldbyname('XMDZ').AsString);
try
WordDocument.SaveAs(FILENAME1);
WORDDOCUMENT.Printout;//[red]为什么不能打印这个文件?[/red]
finally
closedoc;
application.ProcessMessages;
end;
end;
还有问题就是怎么控制显示word打印的预览?
怎么控制用哪个打印机打印?
 
解决了一个问题,
try
WordDocument.SaveAs(FILENAME1);
WORDDOCUMENT.Printout;//为什么不能打印这个文件?
finally
closedoc;
application.ProcessMessages;
end;
closedoc速度太快,导致上面的打印任务未发送就关闭了,请问如何解决这个问题?
 
试试:
try
WordDocument.SaveAs(FILENAME1);
WORDDOCUMENT.Printout;//为什么不能打印这个文件?
finally
Sleep(500);
closedoc;
application.ProcessMessages;
end;
 
好象没什么效果啊,sleep(5000)都没用,难道不是这个问题,但是如果设置断点调试,在打印后暂时不执行closedoc就能打印出来啊
 
接受答案了.
 
后退
顶部