word替换字符串为文本或图片

using System;
using System.IO;
using Word = Microsoft.Office.Interop.Word;
 
 
namespace SavePicture
{
    public class CreateTemplate
    {
        /// <summary>
        /// 替换成指定字符串
        /// </summary>
        /// <param name="wordPath">要替换的word全路径</param>
        /// <param name="oldText">替换的文字</param>
        /// <param name="newText">替换成的文字</param>
        /// <returns>是否替换成功</returns>
        public static bool ReplaceWord(string wordPath, string oldText, string newText)
        {
            try
            {
                object nothing = System.Reflection.Missing.Value;
                object format = Word.WdSaveFormat.wdFormatDocument;
                object srcFileName = wordPath;
                object wrap = Word.WdFindWrap.wdFindContinue;
 
                Word.ApplicationClass appWord = new Word.ApplicationClass();
                Word.Document wordDocument = appWord.Documents.Open(ref srcFileName, ref format, 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);
                appWord.Selection.Find.ClearFormatting();
 
                appWord.Selection.Find.Replacement.ClearFormatting();
 
 
 
                object objReplace = Word.WdReplace.wdReplaceAll;
                appWord.Selection.Find.ClearFormatting();
                appWord.Selection.Find.Replacement.ClearFormatting();
 
 
                appWord.Selection.Find.Text = oldText;
                appWord.Selection.Find.Replacement.Text = newText;
                appWord.Selection.Find.Execute(ref nothing, ref nothing, ref nothing,
                                               ref nothing, ref nothing, ref nothing,
                                               ref nothing, ref wrap, ref nothing,
                                               ref nothing, ref objReplace, ref nothing,
                                               ref nothing, ref nothing, ref nothing);
 
 
                wordDocument.Save();
                wordDocument.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
 
            }
        }
 
        /// <summary>
        /// 替换word中指定文本为图片
        /// </summary>
        /// <param name="wordPath">word全路径</param>
        /// <param name="textLabel">替换字符串</param>
        /// <param name="imagePath">图片全路径</param>
        /// <returns>是否替换成功</returns>
        public static bool ReplaceImage(string wordPath, string textLabel, string imagePath)
        {
            try
            {
                object nothing = System.Reflection.Missing.Value;
                object missing = Type.Missing;
                object SaveWithDocument = true;
                object LinkToFile = false;
                object format = Word.WdSaveFormat.wdFormatDocument;
                object link = false;
                object confirmConversion = false;
                object objReplace = Word.WdReplace.wdReplaceOne;
                object wrap = Word.WdFindWrap.wdFindContinue;
                object srcFileName = wordPath;
 
                Word.ApplicationClass appWord = new Word.ApplicationClass();
                Word.Document wordDocument = appWord.Documents.Open(ref srcFileName, ref format, 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);
                //替换图片过程
             
                appWord.Selection.Find.ClearFormatting();
                appWord.Selection.Find.Replacement.ClearFormatting();
                bool found;
                appWord.Selection.Find.Text = textLabel;
                appWord.Selection.Find.Replacement.Text = "";
 
                if (File.Exists(imagePath))
                {
                    //替换成图片
                    do
                    {
                        found = appWord.Selection.Find.Execute(ref nothing, ref nothing, ref nothing,
                                                               ref nothing, ref nothing, ref nothing,
                                                               ref nothing, ref wrap, ref nothing,
                                                               ref nothing, ref objReplace, ref nothing,
                                                               ref nothing, ref nothing, ref nothing);
                        if (found)
                        {
                            appWord.Selection.InlineShapes.AddPicture(imagePath, ref LinkToFile, ref SaveWithDocument, ref missing);
                        }
                    } while (appWord.Selection.Find.Found == true);
                }
                wordDocument.Save();
                wordDocument.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
 
            }
        }
 
 
 
    }
}

转载于:https://www.cnblogs.com/y279336671/archive/2012/07/20/2600820.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值