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

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

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

python 复制代码
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运行的代码

csharp 复制代码
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
相关推荐
办公解码器6 小时前
Excel怎么批量快速修改批注?
excel
我是小邵7 小时前
主流数据分析工具全景对比:Excel / Python / R / Power BI / Tableau / Qlik / Snowflake
python·数据分析·excel
SunnyDays10118 小时前
如何使用 C# 将 CSV 数据轻松转换为 PDF
c#·csv转pdf
Big_潘大师8 小时前
C# 六自由度机械臂正反解计算
数学建模·机器人·c#·六自由度机械臂
乘风!8 小时前
前端Jquery,后端Java实现预览Word、Excel、PPT,pdf等文档
pdf·word·excel·jquery
我不是程序猿儿8 小时前
【C#】XtraMessageBox(DevExpress)与MessageBox(WinForms 标准库)的区别
开发语言·c#
偶尔的鼠标人9 小时前
Avalonia/WPF 打开子窗口,并且跨页面传值
c#·wpf·mvvm·avalonia
玖笙&9 小时前
✨WPF编程进阶【6.1】:图形原则(附源码)
c++·c#·wpf·visual studio
huoshan123459 小时前
给旧版 .NET 也开一扇“私有之门”——ILAccess.Fody 实现原理与设计
c#·.net·fody·il·mono.cecil
纸照片9 小时前
WPF中为Button设置IsMouseOver和IsPressed事件中改变背景颜色不起作用
c#·.net·wpf