Excel表的内容批量生成个人加水印的Word文档

news/2024/7/21 7:28:01 标签: excel, c#

Excel表的内容批量生成个人加水印的Word文档

以下代码可以直接复制到docm文件里使用


Sub 宏1()

Dim MyDialog As FileDialog
    Dim GetStr As String, Adoc As String
    Dim PsDoc As Document
    Application.ScreenUpdating = False
    Set MyDialog = Application.FileDialog(msoFileDialogFolderPicker)
        If MyDialog.Show Then GetStr = MyDialog.SelectedItems(1) Else Exit Sub
        Adoc = Dir(GetStr & "\*.doc*")
        Do While Adoc <> ""    '如果是文件夹,或者没有此文件,则会返回""
            Set PsDoc = Documents.Open(GetStr & "\" & Adoc) '打开指定文档
            On Error Resume Next
                ActiveDocument.Sections(1).Range.Select
                ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader  '插入水印前需更改视图样式为页眉视图
                Selection.HeaderFooter.Shapes("PowerPlusWaterMarkObject1019930437").Select      '选中当前水印
                Selection.Delete         '删除旧水印
            '设置插入水印,(预设文字效果, 文字内容, 字体名, 字体大小, 是否粗体, 是否斜体, 左侧位置, 顶部位置)
                Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject1019930437, _
                Split(Split(ActiveDocument.Name, ".")(0), "-")(1), "宋体", 36, False, False, 0, 0).Select
                Selection.Style = ActiveDocument.Styles("正文")
            With Selection.ShapeRange
                    .Name = "PowerPlusWaterMarkObject1019930437"            '形状类名
                    .TextEffect.NormalizedHeight = False            '文字文字效果
                    .Line.Visible = False           '线条是否可见
                    .Fill.Visible = True                '填充是否可见
                    .Fill.Solid         '填充类型(本例为纯色)
                    .Fill.ForeColor.RGB = RGB(255, 0, 0)                     '设定填充的颜色RGB值
                    .Fill.Transparency = 0.5           '设置透明度50%
                    .Rotation = 315                  '设置旋转角度
                    .LockAspectRatio = True              '锁定纵横比
                    .Height = CentimetersToPoints(10.33)             '高度
                    .Width = CentimetersToPoints(10.33)                 '宽度
                    .WrapFormat.AllowOverlap = True                 '是否允许重叠
                    .WrapFormat.Side = wdWrapNone               '是否设置文字环绕
                    .WrapFormat.Type = 3                '设置折回样式(本例设为不折回)
                    .RelativeHorizontalPosition = wdRelativeVerticalPositionMargin              '设置水平位置与纵向页边距关联
                    .RelativeVerticalPosition = wdRelativeVerticalPositionMargin                 '设置垂直位置与横向页边距关联
                    .Left = wdShapeCenter           '水平居中
                    .Top = wdShapeCenter            '垂直居中
            End With
                       '去除页眉上的横线
              ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
              ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
              Selection.Style = ActiveDocument.Styles("正文")
              
              ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
              PsDoc.Close True
            Adoc = Dir()
        Loop
    Application.ScreenUpdating = True
End Sub



以下代码是excel运行的代码

Option Explicit

Sub a()
Dim i%
Dim s$
Dim MyFile As Object
Set MyFile = CreateObject("Scripting.FileSystemObject")
MkDir ThisWorkbook.Path & "\生成文件"           '创建文件夹
s = Application.GetOpenFilename(FileFilter:="word文件,*.doc*", MultiSelect:=False)
If s = "False" Then Exit Sub
For i = 2 To Range("a" & Rows.Count).End(xlUp).Row
    FileCopy s, _
    ThisWorkbook.Path & "\生成文件\" & MyFile.GetBaseName(s) & "-" & Range("A" & i) & ".docx"
Next
MsgBox "OK"
End Sub




http://www.niftyadmin.cn/n/5383535.html

相关文章

CVE-2016-3088(ActiveMQ任意文件写入漏洞)

漏洞描述 1、漏洞编号&#xff1a;CVE-2016-3088 2、影响版本&#xff1a;Apache ActiveMQ 5.x~5.13.0 在 Apache ActiveMQ 5.12.x~5.13.x 版本中&#xff0c;默认关闭了 fileserver 这个应用&#xff08;不过&#xff0c;可以在conf/jetty.xml 中开启&#xff09;&#xff1b;…

使用VBA将多个txt批量转换成excel表并保存

VBA将多个txt批量转换成excel表并保存 Sub ykcbf() Set fso CreateObject("scripting.filesystemobject")Application.ScreenUpdating FalseApplication.DisplayAlerts Falsep ThisWorkbook.Path & ""On Error Resume NextFor Each f In fso.GetF…

C++从入门到精通 第八章(类和对象)【下】

四、动态内存分配的相关问题 1、为对象分配动态存储空间 &#xff08;1&#xff09;到目前为止&#xff0c;在为程序中定义的对象分配内存空间时采用的都是“静态存储方式”&#xff0c;在编译时就确定了所占存储空间的大小&#xff0c;而与之相对的动态存储分配技术则可以保…

java中容易被忽视的toString()方法

之前一直认为toString就是将数据转换成字符类型&#xff0c;直到最近写出了一个bug才对toString有了新的认识 不同数据类型&#xff0c;toString() 有不同的操作 定义一个student类&#xff0c;包含姓名 String类型、性别 String类型、年龄 int 类型、分数列表 String类型的li…

线程相关整理

一 常用的线程手段 1 synchronized 有方法&#xff0c;和方法块&#xff0c;新的java版本 内部实现已经包含了多种方案&#xff0c;并动态调整方案&#xff0c;因此不是特别需求的化&#xff0c;建议直接用此方式 2 reentrantlock 可以构造公平锁 以上两种 代码片段应该是没得选…

scss配置主题

$themes: (light: (font_color: #fff,),dark: (font_color: #000,), );mixin themeify {each $theme-name, $theme-map in $themes {//!global 把局部变量强升为全局变量$theme-map: $theme-map !global;//判断html的data-theme的属性值 #{}是sass的插值表达式//& sass嵌套…

【Java中23种设计模式-单例模式2--懒汉式线程不安全】

加油&#xff0c;新时代打工人&#xff01; 今天&#xff0c;重新回顾一下设计模式&#xff0c;我们一起变强&#xff0c;变秃。哈哈。 23种设计模式定义介绍 Java中23种设计模式-单例模式 package mode;/*** author wenhao* date 2024/02/19 09:16* description 单例模式--懒…

LeetCode每日一题590. N-ary Tree Postorder Traversal

文章目录 一、题目二、题解 一、题目 Given the root of an n-ary tree, return the postorder traversal of its nodes’ values. Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (…