截取当前窗体放到Word打印

 来源:http://topic.csdn.net/u/20100311/08/9c287fa6-a670-48f9-b149-9f4656a6cadb.html

uses

    ComObj, OleServer, WordXP

procedure TfrmVSScheduler.btnPrintClick(Sender: TObject);
var
  sFileName: string;
  Bmp: TBitmap;
  DC: HDC;
begin
  sFileName := ExtractFilePath(Application.ExeName) + 'FileData\Scheduler.bmp';
  self.BringToFront;
  DC := GetDC(GetDesktopWindow);
  Bmp := TBitmap.Create;
  try
    Bmp.Width := self.Width;
    Bmp.Height := self.Height;
    BitBlt(Bmp.Canvas.Handle, 0, 0,
      Bmp.Width, Bmp.Height, DC,
      self.Left, self.Top, SRCCOPY);
  finally
    ReleaseDC(GetDesktopWindow, DC);
  end;
  Bmp.SaveToFile(sFileName);
  Bmp.Free;
  printtoword(sFileName);
end;

procedure TfrmVSScheduler.Printtoword(sFileName: string);
var
  LinkToFile, SaveWithDocument: OleVariant;
  WordApplication1: OleVariant;
  WordDocument1: OleVariant;
begin
  try
    WordApplication1:=GetActiveOleObject('Word.Application');
  except
    try
      WordApplication1:=CreateOleObject('Word.Application');
    except
      MessageBox(handle,'无法链接,请检查是否安装Microsoft Word.','连接出错', MB_Ok or MB_ICONERROR);
      Abort;
    end;
  end;
  try
    WordApplication1.Visible := True;
    WordDocument1 := WordApplication1.Documents.Add(EmptyParam, false, EmptyParam, true);

    //横向
    WordDocument1.PageSetup.Orientation := wdOrientLandscape;
    //纵向
    //WordDocument1.PageSetup.Orientation := wdOrientPortrait;

    LinkToFile := False;
    SaveWithDocument := True;
    If FileExists(sFileName) Then
      WordApplication1.Selection.InlineShapes.AddPicture(sFileName, LinkToFile, SaveWithDocument, EmptyParam);
    WordApplication1.Activate;
    //预览
    //WordApplication1.PrintPreview := true;
  except on E: Exception do
    MessageBox(handle,'无法链接,请检查是否安装Microsoft Word.','连接出错', MB_Ok or MB_ICONERROR);
  end;
end;

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值