2019-09-06
Delphi 10.3 (v26.0) (2019)
(下圖) Word 2016 用手動執行和開啟文件,只會啟動一個 WinWord.exe
(下圖) 但若使用程式去啟動或調用 Word 2016,或者 Word 當掉,這種情況下會不只一個 WinWord.exe 被啟動。此程式只是偵測某一個 WinWord.exe 開啟的文件數。
(下圖) 網路上有文章用此種方式判斷電腦中使否有 Word 被啟動,這是不準的,因為 WinWord.exe 可能有被啟動,但沒有開啟文件,結果會 0個。
下面 Word 已經被啟動,但尚未開啟任何文件,此時會算出 0 個文件。
(下圖) 尚未編輯的空白文件,也算1個文件。
另外,不同版本 Word 軟體,情況可能不同,不保證。
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
USES ComObj;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
oWord: OLEVariant;
begin
oWord := GetActiveOleObject('Word.Application');
Label1.Caption := IntToStr(oWord.Documents.Count);
for i := 1 to oWord.Documents.Count do
begin
// if oWord.Documents.Item(i).Name = 'MyDocument.doc' then
// oWord.Documents.Item(i).Select;
end;
// oWord.ActiveDocument.SaveAs('C:\MyDocument.doc');
// oWord.Quit;
oWord := Unassigned;
end;
end.
|
(完)




沒有留言:
張貼留言