在开发过程中,我遇到了一个问题。代码的主要思想是使用MS Word将.doc和.rtf转换为.pdf文件格式。代码适用于exe-application。但是在服务应用程序中使用此代码会导致出现问题:调用ExportAsFixedFormat方法会引发异常"无效的变量操作"。我没有想法,如何解决这个问题。有人有吗?
function DocToPDFByWord (sFileName,sPDFFileName :string;) : byte;
const
wdExportFormatPDF = 17;
wdDoNotSaveChanges = 0;
var
vOLEApp ,vDocument : OleVariant;
begin
Result := 0;
CoInitialize (nil);
vOLEApp := CreateOLEObject ('Word.Application');
if VarIsNull(vOLEApp) then
begin
Result := 1;
Exit;
end;
vOLEApp.Visible:= False;
try
vDocument := vOLEApp.Documents.Open(FileName:= sFileName, ConfirmConversions:= False, ReadOnly:= True, AddToRecentFiles:= False, PasswordDocument:= 'dummy', PasswordTemplate:= 'dummy', Revert:= True, Visible:= False, OpenAndRepair:= False, NoEncodingDialog:= True);
if VarIsNull(vDocument) then
Result := 2
else
try
vDocument.ExportAsFixedFormat (sPDFFileName ,wdExportFormatPDF);
except
Result := 3;
end;
except
Result:= 4;
end;
vOLEApp.Quit (wdDoNotSaveChanges);
vOLEApp := Unassigned;
CoUnInitialize;
end;