-2

I would like to insert an image, then a new line and then another image. The process has to be repeated until all the images are inserted. This is my code as of now which inserts images, but not blank lines

using System.IO;
using Word = Microsoft.Office.Interop.Word;
namespace Snapper
{
    class WordDocumentGenerator
    {
        public void CreateWordDocument(string fileName)
        {
            string originalPath = Directory.GetCurrentDirectory();
            string path = originalPath;
            path += @"\snapshots";
            object oMissing = System.Reflection.Missing.Value;

            //Create a new Word Application
            Word._Application wordApp = new Word.Application();
            wordApp.Visible = false;
            try
            {
                //Create a new blank document
                Word._Document doc = wordApp.Documents.Add(ref oMissing, ref oMissing, 
                                                           ref oMissing, ref oMissing);
                string[] images = Directory.GetFiles(path);

                //Create a range
                object myTrue = true;
                object myFalse = false;
                object endOfDoc = "\\endofdoc";
                object myRange; 


                foreach (var image in images)
                {
                    myRange = doc.Bookmarks.get_Item(ref endOfDoc).Range;
                    //Add images to the document                    
                    doc.InlineShapes.AddPicture(image, ref myFalse, ref myTrue, ref myRange);
                    //Add a blank line
                    //doc.Content.Text = "\n";
                }


                path = originalPath;
                path += @"\documents";

                DirectoryInfo docDir = new DirectoryInfo(path);
                if (!docDir.Exists)
                {
                    docDir.Create();
                }

                object savePath = path + @"\" + fileName + ".doc";

                doc.SaveAs(ref savePath,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing,
                    ref oMissing
                   );
                doc.Save();
            }            
            finally
            {
                wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
            }                                                                          
        }

    }
}

I need some help in doing it.

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
kunaguvarun
  • 703
  • 1
  • 10
  • 29

1 Answers1

1

Thanks all, but a search showed this question which has been asked already. The answer pretty much solved my problem.

How to add items one at a time to to a new line a word document using word interop

Community
  • 1
  • 1
kunaguvarun
  • 703
  • 1
  • 10
  • 29