C#_Word_通过书签替换文字、图片等

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_36267404/article/details/96108028

通过Word书签替换文字,图片 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param> Word地址
        /// <param name="oldStr"></param> 书签
        /// <param name="newStr"></param>  新文字或者图片地址
        /// <param name="type"></param>  类型
        public void testimg(string filePath,string oldStr,string newStr,string type)
        {
            

            object Nothing = System.Reflection.Missing.Value;
            //创建一个名为wordApp的组件对象
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();

            //word文档位置
            object filename = filePath;

            //定义该插入图片是否为外部链接
            object linkToFile = true;

            //定义插入图片是否随word文档一起保存
            object saveWithDocument = true;

            //打开word文档
            Microsoft.Office.Interop.Word.Document doc = wordApp.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);
            try
            {
                //书签
                object bookMark = oldStr;
                //图片或文字
                string replacePic = newStr;
                //书签是否存在
                if (doc.Bookmarks.Exists(Convert.ToString(bookMark)) == true)
                {
                    //查找书签
                    doc.Bookmarks.get_Item(ref bookMark).Select();
                    //是图片还是文字
                    if (type.Equals("img"))
                    {
                        //设置图片位置
                        wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
                        //在书签的位置添加图片
                        InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture(replacePic, ref linkToFile, ref saveWithDocument, ref Nothing);
                        wordApp.Selection.InlineShapes.AddPicture(replacePic, ref linkToFile, ref saveWithDocument, ref Nothing);
                        //设置图片大小
                        inlineShape.Width = 300;
                        inlineShape.Height = 300;
                    }
                    else 
                    {
                        //文字就直接替换
                        wordApp.Selection.Text = replacePic;
                    }

                    doc.Save();
                    doc.Close(ref Nothing, ref Nothing, ref Nothing);
                    wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                }
                else
                {
                    //word文档中不存在该书签,关闭文档
                    doc.Close(ref Nothing, ref Nothing, ref Nothing);
                }
            }
            catch
            {
            }
        }

 

展开阅读全文

没有更多推荐了,返回首页