VBA的excel逐行替换到word模板及打印还原

滥竽充数砖家,有量没有质的罗列...

vbscript 复制代码
Option Explicit
' 需要在工具->引用库中找到Microsoft word x.xx library勾选上。

Sub ReplaceAndPrint()

    Dim WrkSht As Worksheet
    Set WrkSht = ActiveSheet
    
    Dim RowNumber As Integer
    Dim ColNumber As Integer
    RowNumber = WrkSht.Range("A1").CurrentRegion.Rows.Count
    ColNumber = WrkSht.Range("A1").CurrentRegion.Columns.Count
    
    Dim wordApp As Object
    Dim wordDoc As Object
    Dim excelPath As String
    Dim wordFilePath As String
    Dim j As Integer
    Dim k As Integer
    Dim i As Integer
    Dim colSate As Long, colSime As Long, colLocation As Long, colNumc As Long '用来定位指定列
    ' 查找列位置
    colSate = 0: colSime = 0: colLocation = 0: colNumc = 0
    
    For j = 1 To ColNumber
        Select Case WrkSht.Cells(1, j).Value '第一行的列名循环
            Case "Sate"
                colSate = j
            Case "Sime"
                colSime = j
            Case "Queue Number"
                colLocation = j
            Case "Number"
                colNumc = j
        End Select
    Next j
       
    
    ' 定义替换对照表
    Dim originWords() As String
    Dim replaceWords() As String
    
    ReDim originWords(1 To 4)
    ReDim replaceWords(1 To 4)
    
    originWords(1) = "dd/mm/yyyy"
    originWords(2) = "hh:ss"
    originWords(3) = "queque"
    originWords(4) = "01-B-2111240011"
    
    replaceWords(1) = "pp/pp/mmmm"
    replaceWords(2) = "zz:yy"
    replaceWords(3) = "yueyue"
    replaceWords(4) = "01-B-2111240088"
    

    ' 获取文件路径
    excelPath = ThisWorkbook.Path
    wordFilePath = excelPath & "\猪脚饭1.docx"
    
    ' 检查文件是否存在
    If Dir(wordFilePath) = "" Then
        MsgBox "未找到Word文件:" & wordFilePath
        Exit Sub
    End If
    
    
    ' 创建Word应用
    Set wordApp = CreateObject("Word.Application")
    wordApp.Visible = True
    
    ' 打开文档
    Set wordDoc = wordApp.Documents.Open(wordFilePath)
    ' 指定打印机
    wordApp.ActivePrinter = "POS80"
    
    ' 一行一行循环替换所有关键词,并打印。
    For k = 2 To RowNumber
        replaceWords(1) = WrkSht.Cells(k, colSate).Value
        replaceWords(2) = WrkSht.Cells(k, colSime).Value
        replaceWords(3) = WrkSht.Cells(k, colLocation).Value
        replaceWords(4) = WrkSht.Cells(k, colNumc).Value
        For i = 1 To UBound(originWords)
            With wordDoc.Range.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = originWords(i)
                .Replacement.Text = replaceWords(i)
                .Forward = True
                .Wrap = 1
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .Execute Replace:=2
            End With
        Next i
        ' 打印步骤在此,打印完后word模板还原。
        wordDoc.PrintOut
        Application.Wait Now + TimeValue("0:00:01")
        wordDoc.Undo Times:=4  ' 撤销4步
    Next k

End Sub
相关推荐
2501_930707789 小时前
使用C#代码在 Excel 中添加或设置批注格式
excel
梦因you而美12 小时前
Python win32com 复制Excel sheet优化:覆盖替换而非删除重建,彻底解决公式报错
python·excel·win32com·python自动化·批量复制sheet表
asdzx6713 小时前
使用 C# 将 Excel 转换成高质量 JPG
开发语言·c#·excel
VBA633713 小时前
VBA高级应用30例应用6 第2部分:利用XML文件修改Excel单元格字符
vba
城数派13 小时前
2014-2025年全国监测站点的逐月空气质量数据(15个指标\Excel\Shp格式)
arcgis·信息可视化·数据分析·excel
专注VB编程开发20年14 小时前
WPS 2024 Windows版UI用QT5和自研DirectUI-vba,jsa
qt·vba·wps·jsa·directui
Metaphor6922 天前
使用 Python 操作 Excel 文件中的工作表(添加和删除)
python·excel
开开心心就好2 天前
支持自定义名单的实用随机抽签工具
windows·计算机视觉·计算机外设·excel·散列表·启发式算法·csdn开发云
李昊哲小课2 天前
Python办公自动化教程 - 第2章 单元格样式魔法 - 让表格变得美观专业
开发语言·python·excel·openpyxl
Access开发易登软件2 天前
在 Access 中实现 Web 风格 To Do List
前端·数据结构·microsoft·list·vba·access·access开发