0

I am trying to open an existing word file and insert image into it (image name with path being passed to the function ).

Code logic is working fine but challenge is every time the new image file is inserting / place on the top of the last one.mean the latest image is displaying on very first page of the word file and the rest are accordingly. But my objective is the oldest one will be on top.Or you can say the 1st one will palace 1st and the after that 2nd ,3rd .....

Here is my code sample.

using WordC = Microsoft.Office.Interop.Word;

public void insertImage(string docFileName, string imgFilename)
{
    WordC.Application wordApp = new WordC.Application();
    //  create Word document object
    WordC.Document aDoc = null;
    object readOnly = false;
     object isVisible = false;
     wordApp.Visible = false;
    //  wordApp.DisplayAlerts = false;
    aDoc = wordApp.Documents.Open(docFileName, Type.Missing, ref readOnly, 
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing, ref isVisible, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing);
     aDoc.Activate();
    // WordC.Document doc = WordApp.Documents.Open(docFileName);
    // now add the picture in active document reference
    aDoc.InlineShapes.AddPicture(imgFilename, Type.Missing, Type.Missing, Type.Missing);

    aDoc.Save();
    aDoc.Close(Type.Missing, Type.Missing, Type.Missing);
    wordApp.Quit(Type.Missing, Type.Missing, Type.Missing);
}
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Arnab
  • 271
  • 2
  • 7
  • 18

2 Answers2

1

I have modified the code with below.I am passing range object which solving my problem as of now.Still looking for better solution if there any.

object o_CollapseEnd = WordC.WdCollapseDirection.wdCollapseEnd;
                 WordC.Range imgrng = aDoc.Content;
                 imgrng.Collapse(ref o_CollapseEnd);
                 imgrng.InlineShapes.AddPicture(imgFilename, Type.Missing, Type.Missing,imgrng);
Arnab
  • 271
  • 2
  • 7
  • 18
0

I have used this word to PDF code which will convert all your images in word document to PDF document.

May be you may find it useful !!!

Community
  • 1
  • 1
subi_speedrunner
  • 901
  • 3
  • 9
  • 17