Inserting an image into a word document table

How do i insert an image into a word document using WordApplication component?
kilocoderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.