• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1846
  • Last Modified:

Inserting an image into a word document table

How do i insert an image into a word document using WordApplication component?
0
kilocoder
Asked:
kilocoder
  • 2
2 Solutions
 
LiveBootlegCommented:
Try this:

procedure TForm1.Button1Click(Sender: TObject);
var
  DocFileName: OleVariant;
  PicFileName: OleVariant;
  WordVisible: OleVariant;
begin
  WordApp.Connect;

  if OpenDialog1.Execute then
  begin
    DocFileName := OpenDialog1.FileName;
    WordVisible := True;

    WordApp.Documents.Open(DocFileName, EmptyParam, EmptyParam, EmptyParam,
                           EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                           EmptyParam, EmptyParam, EmptyParam, WordVisible,
                           EmptyParam, EmptyParam, EmptyParam);

    WordDoc.ConnectTo(WordApp.ActiveDocument);

    if OpenPictureDialog1.Execute then
    begin
      PicFileName := OpenPictureDialog1.FileName;
     
      WordDoc.Shapes.AddPicture(PicFileName, EmptyParam, EmptyParam, EmptyParam,
                                EmptyParam, EmptyParam, EmptyParam, EmptyParam);
    end;
  end;

  WordApp.Visible := True;
end;


I have opened an existing document, and the code just inserts an image from a file in the beginning of the document. If you want to play around with the settings of the image you have just inserted, open the MS Word Type Library.

In Delphi: Project -> Import Type Library. Scroll down to Microsoft Word 11 Object Library (This is for Word 2003 - you might have a different version, depending on the version of your Office Suite), and click on create unit.

Search for the AddPicture unit. You should find it as part of the Shapes interface. Look at the different arguments for the method and you should be able to do what you want...
0
 
calinutzCommented:
//place a WordApplication and a WordDocument on the form and uses:
add to uses.... OleServer,ClipBrd,JPEG,ComObj,Word2000,Word97,WordXP

//connect to word like this:

procedure TReportFil.prepWord;
var
  Docs, ItemIndex: OleVariant;
 IIndex:OleVariant;
begin
ItemIndex := 1;
      try
      Wordapplication.Connect;
      except
      Abort;
      end;
    Wordapplication.Visible := false;
    WordApplication.Caption := 'Report client';
    Docs := WordApplication.Documents;
    Docs.Add(EmptyParam);
      {Assign WordDocument component}
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
IIndex:=1;
end;


//For inserting an image you just need to load it in the Clipboard and paste it afterwards in the selection like tis:

        WordApplication.Selection.InsertParagraphAfter;
        s:='c:\YourPicture.jpg';
        Image1.Picture.LoadFromFile(s);
        ClipBoard.Assign(Image1.Picture);
        WordApplication.Selection.Paste;
        logu.Lines.Add('.. Am inserat graficul vz lunare pe perioada in Word');
        WordApplication.Selection.TypeParagraph;


// Success
0
 
LiveBootlegCommented:
Both answers should do the trick...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now