word添加水印,.NET执行宏

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Word=Microsoft.Office.Interop.Word;
using Microsoft.Vbe.Interop;
public partial class word : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }



    protected void Button1_Click(object sender, EventArgs e)
    {

        object srcFileName = Server.MapPath("b.doc"); 
        object obj = true; 


         object Nothing = System.Reflection.Missing.Value;

         Word.ApplicationClass wordAppObj = new Word.ApplicationClass(); 
         Word.Document WordDoc = null;
         WordDoc = wordAppObj.Documents.Open(ref srcFileName, 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);



         var project = WordDoc.VBProject;
         var module = project.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);

         var script = string.Format(
         @"
          Sub 宏1()
 
    ActiveDocument.Sections(1).Range.Select
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.HeaderFooter.Shapes.AddTextEffect(  PowerPlusWaterMarkObject2859093, ""ASAP123654"", ""等线"", 1, False, False, 0,0).Select
    Selection.ShapeRange.Name = ""PowerPlusWaterMarkObject2859093""
    Selection.ShapeRange.TextEffect.NormalizedHeight = False
    Selection.ShapeRange.Line.Visible = False
    Selection.ShapeRange.Fill.Visible = True
    Selection.ShapeRange.Fill.Solid
    Selection.ShapeRange.Fill.ForeColor.RGB = RGB(192, 192, 192)
    Selection.ShapeRange.Fill.Transparency = 0.5
    Selection.ShapeRange.Rotation = 315
    Selection.ShapeRange.LockAspectRatio = True
    Selection.ShapeRange.Height = CentimetersToPoints(3.44)
    Selection.ShapeRange.Width = CentimetersToPoints(17.21)
    Selection.ShapeRange.WrapFormat.AllowOverlap = True
    Selection.ShapeRange.WrapFormat.Side = wdWrapNone
    Selection.ShapeRange.WrapFormat.Type = 3
    Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
    Selection.ShapeRange.RelativeVerticalPosition =  wdRelativeVerticalPositionMargin
    Selection.ShapeRange.Left = wdShapeCenter
    Selection.ShapeRange.Top = wdShapeCenter
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

   ActiveDocument.Save
End Sub 

");

         module.CodeModule.AddFromString(script);  
         wordAppObj.Run("宏1");
         WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
         wordAppObj.Quit(ref Nothing, ref Nothing, ref Nothing);
         Response.Write("ok");
    }






     






}

  

posted @ 2021-01-05 11:44  启明星工作室  阅读(209)  评论(0编辑  收藏  举报