我的解决方法跟Tsir222所述完全一样,但是还有两个问题,一个就是跟手工启动的word之间有冲突,第二就是我想获得关于控制word的更多信息,我现在需要自动启动文档保护功能,还不知道如何写
下面为具体步骤:
新建ActiveForm名为AF
添加接口:LocalFileName, DownloadURL, UploadURL, Operator,并在AF中创建同名成员变量,对应赋值
在AF中加入panel,onclick事件中写如下代码
if Panel1.Tag = 1 then Exit;
MainForm := TMainForm.Create(Application);
try
MainForm.LocalFileName := LocalFileName;
MainForm.Operator := Operator;
MainForm.DownloadURL := DownloadURL;
MainForm.UploadURL := UploadURL;
MainForm.Caption := ExtractFileName(LocalFileName) + ' [' + Operator + ']';
MainForm.ShowModal;
finally
MainForm.Free;
Panel1.Caption := '谢谢使用';
Panel1.Tag := 1;
end;
然后新建一个Form名为MF,放一个菜单,放一个TOleContainer,一个TIdHTTP
菜单里面添加,File->Reload, Update, Exit
Reload的onclick中
var
Stream: TStream;
begin
Stream := TFileStream.Create(FLocalFileName, fmCreate);
try
HTTP.Get(FDownloadURL, Stream);
finally
Stream.Free;
end;
OLEC.CreateObjectFromFile(FLocalFileName, True);
OLEC.DoVerb(ovShow);
FOperatorOrg := OLEC.OleObject.Application.UserName;
OLEC.OleObject.Application.UserName := FOperator;
end;
Update的onclick中
var
Request : TMemoryStream;
FileData : TMemoryStream;
Response : TStringStream;
tempArray : array[0..10000] of char;
tempString : String;
FileName : String;
const
NewLine = chr(13) + chr(10);
begin
Screen.Cursor := crHourGlass;
FileName := FLocalFileName+'.doc';
OLEC.SaveAsDocument(FileName);
// Set the properties for HTTP
HTTP.Request.ContentEncoding := 'multipart/form-data'
//'application/x-www-form-urlencoded';
try
// create streams
Response := TStringStream.Create('');
Request := TMemoryStream.Create;
FileData := TMemoryStream.Create;
// load file data
FileData.LoadFromFile( FileName );
FileData.Seek( 0, soFromBeginning );
// copy form data
TempString := '';
TempString := TempString + '-----------------------------7d18914904ee' + NewLine;
TempString := TempString + 'Content-Disposition: form-data
name="myapp"
filename="' + FileName + '"' + NewLine;
TempString := TempString + 'Content-Type: application/file' + NewLine;
TempString := TempString + '' + NewLine;
// Add data to post
FillChar( temparray, SizeOf( temparray ), #0 );
strpcopy( temparray, tempstring );
Request.Write( temparray, length(tempstring) );
Request.Seek( 0, soFromEnd );
Request.CopyFrom( FileData, FileData.Size );
// copy form data
TempString := '';
TempString := TempString + '' + NewLine;
TempString := TempString + '-----------------------------7d18914904ee' + NewLine;
TempString := TempString + 'Content-Disposition: form-data
name="Submit"' + NewLine;
TempString := TempString + '' + NewLine;
TempString := TempString + 'Submit' + NewLine;
TempString := TempString + '-----------------------------7d18914904ee--' + NewLine;
// Add data to post
FillChar( temparray, SizeOf( temparray ), #0 );
strpcopy( temparray, tempstring );
Request.Write( temparray, length(tempstring) );
// post file
try
HTTP.Post( FUploadURL, Request, Response);
MessageDlg('保存成功!', mtInformation, [mbOK], 0);
except
MessageDlg('保存失败!', mtError, [mbOK], 0);
end;
finally
Response.Free;
Request.Free;
FileData.Free;
Screen.Cursor := crDefault;
end;
end;
Exit的onclick中
begin
Close;
end;
MF的onclose中
begin
OLEC.OleObject.Application.UserName := FOperatorOrg;
OLEC.Close;
OLEC.DestroyObject;
if FileExists(FLocalFileName) then DeleteFile(FLocalFileName);
if FileExists(FLocalFileName + '.doc') then DeleteFile(FLocalFileName + '.doc');
end;
然后从project->Web deploy发布。。。ok了