急:如何转换以下VBA代码为DELPHI代码? (300分)

  • 主题发起人 主题发起人 MichaelZhu
  • 开始时间 开始时间
M

MichaelZhu

Unregistered / Unconfirmed
GUEST, unregistred user!
各位请教个问题,怎么才能将以下的VBA代码转换为DELPHI代码?
或者是有没有别的办法,在DELPHI中检测有没有指定的WORD文件打开?

Public Function checkDocFileOpenAndClose(ByVal strDocFileName As String)

  On Error Resume Next
  Dim docAPP As Object
  Dim doc As Object
  Set docAPP = GetObject(, "Word.Application")
  If Err.Number <> 0 Then Exit Function
  For Each doc In docAPP.documents
    'If InStr(1, doc.Name, strdocFileName, 1) Then
    If doc.Name = strDocFileName Then
      doc.Close   '关闭文档
      ' MyXL.Quit  '退出excel
      Exit For
    End If
  Next doc
  Set doc = Nothing
  Set docAPP = Nothing

 
没人感兴趣?还是分不够?
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComObj;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure CheckDocFileOpenAndClose(DocFileName: string);
var
Word: OleVariant;
Documents: OleVariant;
Document: OleVariant;
i: Integer;
begin
Word := GetActiveOleObject('Word.Application');
try
Documents := Word.Documents;
for i := 1 to Documents.Count do
begin
Document := Documents.Item(i);
if CompareText(Document.Name, DocFileName) = 0 then
Document.Close;
end;
Document := Unassigned;
Documents := Unassigned;
Word := Unassigned;
except
ShowMessage('Word没有运行。');
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
CheckDocFileOpenAndClose('111.doc');
end;
end.
没有更多的时间研究。其他的自己研究吧。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
928
SUNSTONE的Delphi笔记
S
后退
顶部