zoukankan      html  css  js  c++  java
  • Word的替换文字与图片

    using System;

    using System.Collections;

    using System.Configuration;

    using System.Data;

    using System.Linq;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.HtmlControls;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Xml.Linq;

    using Word = Microsoft.Office.Interop.Word;

     

    public partial class Tgfb : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            object oMissing = System.Reflection.Missing.Value;

            Word._Application oWord;

            Word._Document oDoc;

            oWord = new Word.Application();

     

            object fileName = @"d:/websitepf/docs/2.doc";

     

            oWord.Visible = true;

            oDoc = oWord.Documents.Open(ref fileName,

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

     

            object replaceAll = Word.WdReplace.wdReplaceAll;

     

            oWord.Selection.Find.ClearFormatting();

            oWord.Selection.Find.Text = "[[ReportCode]]";

     

            oWord.Selection.Find.Replacement.ClearFormatting();

            oWord.Selection.Find.Replacement.Text = "WT-121221-21212";

     

     

     

     

            oWord.Selection.Find.Execute(

                                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                                            ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

     

     

     

            oWord.Selection.Find.ClearFormatting();

     

            oWord.Selection.Find.Text = "[[SampleName]]";

            oWord.Selection.Find.Replacement.ClearFormatting();

     

            oWord.Selection.Find.Replacement.Text = "罗的草";

     

     

     

            oWord.Selection.Find.Execute(

                                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                                            ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

     

     

     

     

     

            object LinkToFile = false ;

            object SaveWithDocument = true ;

     

            string picfileName = @"d:/websitepf/images/cm.png";

     

     

     

            oWord.Selection.Find.ClearFormatting();

     

            oWord.Selection.Find.Text = "[[pic1]]";

     

            oWord.Selection.Find.Execute(

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

     

     

            object Anchor = oWord.Selection.Range;

            //oDoc.InlineShapes.AddPicture(picfileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);

            object left = 0;

            object top = 0;

            object width = 100;

            object height =200;

            Anchor = oWord.Selection.Range;

            oDoc.Shapes.AddPicture(picfileName, ref LinkToFile, ref SaveWithDocument, ref left, ref top, ref width, ref height, ref  Anchor);

     

            oWord.Selection.Find.Replacement.ClearFormatting();

     

            oWord.Selection.Find.Replacement.Text = "";

     

            oWord.Selection.Find.Execute(

                                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                                            ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

          

     

            picfileName = @"d:/websitepf/images/headerbk.png";

     

     

     

                    object dummy=System.Reflection.Missing.Value;

     

                    object count=1;         

     

                    object Unit=Word.WdUnits.wdCharacter;

     

                    oWord.Selection.MoveRight (ref Unit,ref count,ref dummy); 

     

    ///-----------------------------------------------------------------

     

            oWord.Selection.Find.ClearFormatting();

     

            oWord.Selection.Find.Text = "[[pic2]]";

            oWord.Selection.Find.Execute(

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

     

            left = 0;

    top = 0;

     width = 300;

     height = 300;

            Anchor = oWord.Selection.Range;

            oDoc.Shapes .AddPicture (picfileName, ref LinkToFile, ref SaveWithDocument, ref left,ref top,ref width,ref height ,ref  Anchor);

            //oDoc.InlineShapes.AddPicture(picfileName, ref LinkToFile, ref SaveWithDocument, ref  Anchor);

     

     

             oWord.Selection.Find.Replacement.ClearFormatting();

     

            oWord.Selection.Find.Replacement.Text = "";

     

            oWord.Selection.Find.Execute(

                                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                                            ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

     

     

           

           

     

        }

    }

  • 相关阅读:
    《Effective C#》读书笔记——条目22:通过定义并实现接口替代继承<使用C#表达设计>
    《Effective C#》读书笔记——条目23:理解接口方法和虚方法的区别<使用C#表达设计>
    《Effective C#》读书笔记——条目27:让类型支持序列化<使用C#表达设计>
    《Effective C#》读书笔记——条目28:提供粗粒度的互联网API<使用C#表达设计>
    《Effective C#》读书笔记——条目21:限制类型的可见性<使用C#表达设计>
    《Effective C#》读书笔记——条目24:用委托实现回调<使用C#表达设计>
    C# 温故知新 基础篇(1) C#概述
    《Effective C#》读书笔记——条目26:避免返回对内部类对象的引用<使用C#表达设计>
    《有关程序员的性别、年龄、个性、编程方法的话题》(2010/01/31)
    《程序员应该掌握的实用编程技能2》(2010/01/23)
  • 原文地址:https://www.cnblogs.com/huanglexian/p/3567017.html
Copyright © 2011-2022 走看看