uses Word2010, ComObj,
procedure TForm8.Button1Click(Sender: TObject);
begin
try
WordApp := TWOrdApplication.Create(Self);
except
end;
WordApp.visible := true;
WordApp.DisplayAlerts := wdAlertsNone;
WordApp.OnQuit := WordAppQuit;
WordApp.OnDocumentBeforeClose := WordDocumentBeforeClose;
WordApp.OnDocumentBeforeSave := WordDocumentBeforeSave;
Document := WordApp.Documents.Open('c:\temp\test.docx', EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam);
end;
procedure TForm8.WordDocumentBeforeClose(ASender: TObject;
const Doc: WordDocument; var Cancel: WordBool);
var
cFileName: String;
oHandle: THandle;
begin
Cancel := False;
if not Doc.Saved then
begin
// Haal applicatie naar voorgrond
TWordApplication(ASender).Visible := False;
oHandle := Application.Handle;
try
if oHandle > 0 then
SetForegroundWindow(oHandle);
finally
// CloseHandle(oHandle);
end;
if MessageDlg('Opslaan? ', mtInformation, [mbYes, mbNo], 0) = mrYes then
begin
// Doc.Save;
Doc.SaveAs('c:\temp\test1.docx', wdFormatDocumentDefault, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam);
Doc.Saved := True;
end
else
begin
Doc.Saved := True;
end;
end
else
showmessage('doc not saved');
end;
procedure TForm8.WordDocumentBeforeSave(ASender: TObject;
const Doc: WordDocument; var ShowUI, Cancel: WordBool);
begin
ShowUI := False;
Cancel := False;
end;
procedure TForm8.WordAppQuit(Sender: TObject);
begin
WordApp.Disconnect;
// Deze breekt het opslaan van grotere documenten voortijdig af
// KillTask('WinWord');
end;
function TForm8.KillTask(ExeFileName: string): Integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
Bookmarks