那位大侠帮帮忙啊了

解决方案 »

  1.   

    procedure SetPassword(const DocFile, OldPass, NewPass: WideString; PassLevel: Integer);
    var nr,pl,pw:OleVariant;
        wa:TWordApplication;
        wd:TWordDocument;
        wb:OleVariant;
    begin
      if not fileexists(DocFile) then exit;
      wa:=TwordApplication.Create(nil);
      wa.ConnectTo(CreateOleObject('word.Application') as _Application);
      try
      wb:=wa.WordBasic;
      wb.FileOpen(DocFile);
      wd:=TWordDocument.Create(nil);
        try
        wd.ConnectTo(wa.ActiveDocument);
        if wd.HasPassword then begin
           try
             pw:=OldPass;
             wd.Unprotect(pw);
           except
           end;
           end;
        nr:=null;
        pw:=NewPass;
        pl:=PassLevel;
        wd.Protect(pl,nr,pw);
        wd.Save;
        finally
        wd.Close;
        wa.Quit;
        wd.Free;
        end;
      finally
      wa.Free;
      end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    SetPassword('F:\word\1.doc','','12',1);
    end;