如题?
第一层句柄和类名找得到,第二层,文件菜单找不到啊?
我想模拟手动操作,另存为,当然,也不限制,只要能控制当前打开的WORD文件另存为指定目录也行?
哪位作过类似的,帮忙一下?

解决方案 »

  1.   

    监视磁盘上多了哪些文件会来得容易些,CnPack有个单元是实现这个功能的。
      

  2.   

    Word := CreateOleObject('Word.Application');
    Doc := FWord.Documents.Add;
    ...
    Doc.SaveAs(...);
      

  3.   

    Word := CreateOleObject('Word.Application');
    Doc := Word.Documents.Add;
    ...
    Doc.SaveAs(...); 
      

  4.   

    新建工程、在属性浏览器分别双击窗体的create、show事件,然后用下列代码覆盖你的unit1:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls, ComCtrls, Spin;type
      TForm1 = class(TForm)
        procedure FormShow(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        procedure TimerTimer(Sender: TObject);
        procedure SpinEditChange(Sender: TObject);
        procedure CheckBoxClick(Sender: TObject);
      public
        { Public declarations }
      end;var
      Form1: TForm1;
    //本代码在delphi7 + windows_XP 编译运行通过,如有谬误或更好方法,还请不吝指教。谢谢!
    //中国软件研发联盟QQ群122058606 __广州佬编写
    implementation{$R *.dfm}const Minute=30;//时间间隔——分钟
          Interval=100;//用于等待保存对话出现的时间——毫秒(根据机器具体性能而定)
    var x:integer;//用于区分"另存为"的下拉框
        First:boolean;//是否第一次保存
        Timer: TTimer;
        LabeledEdit:TLabeledEdit;
        SpinEdit:TSpinEdit;
        CheckBox:TCheckBox;
        Label1:TLabel;procedure SimulationKey(key_1:word; alt:word=0; key_2:word=0);//模拟按键
    begin
      if alt>0 then
        keybd_event(alt,0,KEYEVENTF_EXTENDEDKEY or 0,0);
      keybd_event(key_1,0,KEYEVENTF_EXTENDEDKEY or 0,0);
      keybd_event(key_1,0,KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP,0);
      if key_2>0 then begin
        keybd_event(key_2,0,KEYEVENTF_EXTENDEDKEY or 0,0);
        keybd_event(key_2,0,KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP,0);
      end;
      if alt>0 then
        keybd_event(alt,0,KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP,0);
    end;function EnumChildWndProc(aHwnd:LongInt;AlParam:lParam):boolean;stdcall;//枚举窗体内组件
    var WndClassName:array[0..254] of Char;
        WndCaption:array[0..254] of Char;
    begin
      GetClassName(aHwnd,wndClassName,254);
      GetWindowText(aHwnd,WndCaption,254);
      if string(wndClassName)='RichEdit20W' then begin//控件为下拉框类型
        inc(x);
        if x=2 then begin//设置路径和文件名的下拉框
          sleep(Interval);
          SendMessage(aHwnd,WM_SETTEXT,0,LongInt(pchar('c:\'+LabeledEdit.Text+'.Doc')));//保存到 C 盘跟
          sleep(Interval);
          SimulationKey(83,VK_MENU);//保存
          result:=false;
          exit;
        end;
      end;
      result:=true;
    end;procedure TForm1.TimerTimer(Sender: TObject);
    var hCurrentWindow:HWND;
        szText:array [0..254] of char;
    begin
      x:=0;
      hCurrentWindow:=GetWindow(Handle,GW_HWNDFIRST);
      while hCurrentWindow<>0 do begin//枚举窗体
        if GetWindowText(hCurrentWindow,@szText,255)>0 then
        begin
          if pos('- Microsoft Word',strpas(@szText))>0 then begin//找到 Word 程序窗体
            SetForegroundWindow(hCurrentWindow);
            if First then begin //如果是第一次运行
              SimulationKey(70,VK_MENU,65);//打开另存为对话
              sleep(Interval);
              SimulationKey(VK_ESCAPE);//取消保存
              sleep(Interval);
              First:=false;
            end;
            SimulationKey(70,VK_MENU,65);//打开另存为对话
            sleep(Interval);
            hCurrentWindow:=FindWindow(nil,pchar('另存为'));//查找另存为的窗体
            if hCurrentWindow<>0 then begin//找到
              EnumChildWindows(hCurrentWindow,@EnumChildWndProc,0);//枚举窗体内的组件
              exit;
            end;
          end;
        end;
        hCurrentWindow:=GetWindow(hCurrentWindow,GW_HWNDNEXT);//继续查找下一个
      end;
    end;procedure TForm1.FormShow(Sender: TObject);
    var hCurrentWindow:HWND;
        szText:array [0..254] of char;
    begin
      First:=true;
      //利用枚举的方法查找 Word 程序的窗体:
      hCurrentWindow:=GetWindow(Handle,GW_HWNDFIRST);
      while hCurrentWindow<>0 do begin
        if GetWindowText(hCurrentWindow,@szText,255)>0 then begin
          if pos('- Microsoft Word',strpas(@szText))>0 then begin
            SetForegroundWindow(hCurrentWindow);
            if First then begin
              SimulationKey(70,VK_MENU,65);
              sleep(Interval);
              SimulationKey(VK_ESCAPE);
              sleep(Interval);
              First:=false;
            end;
          end;
        end;
        hCurrentWindow:=GetWindow(hCurrentWindow,GW_HWNDNEXT);
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Caption:='自动保存Word文档';
      Position:=poScreenCenter;
      Height:=203;
      Width:=283;
      LabeledEdit:=TLabeledEdit.Create(self);
      with LabeledEdit do begin
        Parent:=Form1;
        LabeledEdit.Left:=80;
        LabeledEdit.Top:=24;
        LabeledEdit.Text:='测试文件';
        LabeledEdit.EditLabel.Caption:='文件名:';
      end;
      SpinEdit:=TSpinEdit.Create(self);
      with SpinEdit do begin
        Parent:=Form1;
        Left:=80;
        Top:=80;
        Width:=121;
        Value:=Minute;
        OnChange:=SpinEditChange;
      end;
      Label1:=TLabel.Create(self);
      with Label1 do begin
        AutoSize:=false;
        Caption:='保存间隔(分钟):';
        Parent:=Form1;
        Left:=80;
        Top:=63;
        Width:=120;
      end;
      CheckBox:=TCheckBox.Create(self);
      with CheckBox do begin
        Parent:=Form1;
        Left:=80;
        Top:=110;
        Caption:='自动保存';
        OnClick:=CheckBoxClick;
      end;
      Timer:= TTimer.Create(self);
      with Timer do begin
        Enabled:=false;
        Interval:=Minute*60000;
        OnTimer:=TimerTimer;
      end;
    end;procedure TForm1.SpinEditChange(Sender: TObject);
    begin
      if SpinEdit.Text='' then exit;
      Timer.Interval:=SpinEdit.Value * 60000;
    end;procedure TForm1.CheckBoxClick(Sender: TObject);
    begin
      Timer.Enabled:=CheckBox.Checked;
      if First then TimerTimer(nil);
    end;end.