具体问题是这样的。 我想把word嵌入在Delphi的窗体里。然后可以在word里面打字。 同时能够打印。 在打印的时候我希望自动把一张图片(其实就是一个印章)打印在右下角的签名那里。 而且印章图片下面的签名也要能看到,效果就像是一个文件上盖了一个公章一样。现在的问题是我怎么用Delphi控制word插入图片呢?

解决方案 »

  1.   

    to: ok999ok(ok999ok) 我看delphi里的例子是用复制粘贴的方法。不过我想在一个指定的位置上插入图片。 我找了,没找到。 能帮我一下吗? 谢了
      

  2.   

    你打算用的office版本是多少?offcie2000 ? 还是不限制版本?也就是说你delphi用什么方式操作的 word ?
      

  3.   

    -----以下言论转自大富翁论坛,所以希望大家问问题的时候可以先查查资料,
    网址http://www.delphibbs.com/delphibbs/dispq.asp?lid=1990538以下是我在我的例子上加的,你看看,也许会有帮助
    就是这句了:
    worddoc.tables.item(1).cell(1,2).range.InlineShapes.AddPicture('E:\heartsong\0003.gif',False,True);//这就是加放入图片的语句
     cell(1,2)表示第一行第2列的地方,根据你的就是cell(1,4)了,呵呵unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure InsertLines(LineNum:Integer);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        wordApp,wordDoc:Variant;
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses ComObj;{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    var
      StrAdd:String;
      i:Integer;
      wrdSelection:Variant;
    begin
      //创建一个word对象
      wordApp:=CreateOleObject('Word.Application');
      wordApp.Visible:=true;
      //创建一个word文档
      wordDoc:=wordApp.Documents.Add();
      wordDoc.Select;  wrdSelection:=wordApp.selection;  //在文档中插入内容
      StrAdd:='插入一张图片';
      wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
      wrdSelection.font.bold:=true;
      wrdSelection.font.size:=15;
      wrdSelection.font.Underline:=1;
      wrdSelection.TypeText(StrAdd);  wrdSelection.font.Underline:=0;
      wrdSelection.font.bold:=false;
      wrdSelection.font.size:=11;
      InsertLines(1);  //在文档中插入一个4行4列的表格,并格式化
      wordDoc.Tables.Add(wrdSelection.Range,4,4,2,0);
      wordDoc.Tables.Item(1).Borders.Item(1).LineStyle:=7;
      wordDoc.Tables.Item(1).Borders.Item(2).LineStyle:=7;
      wordDoc.Tables.Item(1).Borders.Item(3).LineStyle:=7;
      wordDoc.Tables.Item(1).Borders.Item(4).LineStyle:=7;
      for i:=1 to 4 do
      begin
        wordDoc.Tables.Item(1).Cell(i,1).Range.Bold:=true;
        wordDoc.Tables.Item(1).Cell(i,3).Range.Bold:=true;
        wordDoc.Tables.Item(1).Rows.Item(i).Range.Paragraphs.Alignment:=wdAlignParagraphCenter;
      end;
      //在第一个表格中插入内容
      wordDoc.Tables.Item(1).Cell(1,1).Range.text:='岗位名称';
      worddoc.tables.item(1).cell(1,2).range.InlineShapes.AddPicture('E:\heartsong\0003.gif',False,True);//这就是加放入图片的语句
      wordDoc.Tables.Item(1).Cell(2,1).Range.InsertAfter('岗位级别');
      wordDoc.Tables.Item(1).Cell(3,1).Range.InsertAfter('隶属部门');
      wordDoc.Tables.Item(1).Cell(4,1).Range.InsertAfter('直接上级');
      wordDoc.Tables.Item(1).Cell(1,3).Range.InsertAfter('岗位编号');
      wordDoc.Tables.Item(1).Cell(2,3).Range.InsertAfter('现任职者');
      wordDoc.Tables.Item(1).Cell(3,3).Range.InsertAfter('分支机构');
      wordDoc.Tables.Item(1).Cell(4,3).Range.InsertAfter('直接下级'); //保存文档......
    end;end.
      

  4.   

    to: angle097113(深思不解) 你的例子不错。 但是为什么我执行不了? 我查看了
    worddoc.tables.item(1).cell(1,2).range.InlineShapes.AddPicture('E:\heartsong\0003.gif',False,True);AddPicture方法的参数,一共是四个。
    AddPicture(fileName:wideString; linkToFile, saveWithDocument, range: olevariant)但是你给的调用怎么是三个参数? 我试过,不好使啊。还请兄弟指教。
      

  5.   

    另外,我要控制的word版是2000就行。 如果有通用的办法更好了。
      

  6.   

    下面是我写的代码,
    procedure TFrmMain.addPic(fileName:String);
    var
        wFileName : WideString;
        linkToFile, saveWithDocument, left, top, width, height, anchor : OleVariant;
    begin
        wFileName := WideString(fileName);
        linkToFile := false;
        saveWithDocument := true;
        left := 0;
        top := 0;
        width := 100;
        height := 100;
        self.WordApplication.ActiveDocument.Shapes.AddPicture(wFileName, linkToFile, saveWithDocument, left, top, width, height, anchor);
    end;执行到 addPicture 那句时报错。 错误信息是: Project Word.exe raised exception class EOleException with message '服务器出现意外情况。' Process stopped Use Step or Run to Continue.这是怎么回事?
      

  7.   

    Shapes.AddPicture改为 InlineShapes.AddPicture
      

  8.   

    我现在已经能插入图片了。
        myPic := WordApp.ActiveDocument.Shapes.AddPicture('c:\abc.bmp', False, True);不过我想在光标的位置插入图片。AddPicture方法还有一个,参数是 
    AddPicture(fileName,top,left,width,height),其中top,left是以象素为单位的。我要是能得到光标目前所在位置的象素也行。 我该怎么在光标的位置上插入一个图片?