将图片或者文字插入Word指定位置

来源:互联网 发布:淘宝违规商品重新上架 编辑:程序博客网 时间:2024/04/30 13:57

        /// <summary>
        /// 将图片插入word文档指定位置
        /// </summary>
        /// <param name="wordPath">word文档路径</param>
        /// <param name="dicImg">Dictionary 存放word书签名称以及所对应的图片</param>
        /// <param name="dicWeek">Dictionary 存放word书签名称以及所对应的文字</param>
        public static void SetWord(string wordPath, Dictionary<string,string> dicImg,Dictionary<string,string> dicWeek)
        {
            WriteLog("进入SetWord");
            WriteLog("wordPath====" + wordPath);
            try
            {
                try
                {
                    Process[] processes0;
                    processes0 = Process.GetProcessesByName("WINWORD");
                    foreach (Process process in processes0)
                    {
                        process.Kill();
                    }
                }
                catch(Exception err)
                {
                    WriteLog("SetWord111===="+err.Message);
                }
                WriteLog("Count:" + dicImg.Count.ToString() + "," + dicWeek.Count.ToString());
                object filename = wordPath;//文件名    
                WriteLog("aaaaaaaaaa");
                Microsoft.Office.Interop.Word.Application a = new Microsoft.Office.Interop.Word.ApplicationClass();//建立一个Word程序对像    
                WriteLog("bbbbbbbbbb");
                object Nothing = System.Reflection.Missing.Value;//空值  
                Microsoft.Office.Interop.Word.Document b=null;
                try
                {
                    b = a.Documents.Open(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref   Nothing, ref   Nothing, ref   Nothing, ref   Nothing, ref   Nothing, ref   Nothing, ref   Nothing, ref   Nothing, ref   Nothing, ref   Nothing, ref   Nothing);//建立一个Word文档对像                            
                }
                catch(Exception er)
                {
                    WriteLog("er==="+er.Message);
                }
                 WriteLog("mmmmmmmmmmmmmmmmmmmmmm");
                foreach (KeyValuePair<string, string> item in dicImg)
                {
                    object bkmC = item.Value;
                    if (a.ActiveDocument.Bookmarks.Exists(bkmC.ToString()) == true)
                    {
                        a.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
                    }
                    object Anchor = a.Selection.Range;

                    object LinkToFile = false;
                    object SaveWithDocument = true;
                    a.ActiveDocument.InlineShapes.AddPicture(item.Key, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                }
                foreach (KeyValuePair<string, string> item in dicWeek)
                {
                    object bkmC = item.Key;
                    if (a.ActiveDocument.Bookmarks.Exists(bkmC.ToString()) == true)
                    {
                        a.ActiveDocument.Bookmarks.get_Item(ref bkmC).Range.Text = item.Value;
                    }
                }

                b.Close(ref   Nothing, ref   Nothing, ref   Nothing);//关闭Word文档    
                a.Quit(ref   Nothing, ref   Nothing, ref   Nothing);//退出Word程序
              }
            catch (Exception e)
            {
                WriteLog("setWord," + e.Message);
            }
           
        }