-2

我想插入一个图像,然后是一个新行,然后是另一个图像。必须重复该过程,直到插入所有图像。这是我现在插入图像的代码,但不是空行

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);
            }                                                                          
        }

    }
}

我需要一些帮助来做这件事。

4

1 回答 1

1

谢谢大家,但是搜索显示了这个已经被问过的问题。答案几乎解决了我的问题。

如何使用 word interop 一次将一个项目添加到 word 文档的新行

于 2013-02-10T16:59:29.447 回答