【发布时间】:2021-08-21 23:16:50
【问题描述】:
我正在尝试创建一组 plates(一组 5 行,其中一些数据取自主工作表)并创建一个 Word 文件,每页放置 2 个板。在每个盘子之前我想插入一个标志(我试图在 for 循环中添加标志,但我现在迷路了),具有自定义样式(与结果页面相同) 到目前为止,我有部分代码,我向您展示了我试图获得的结果: (第一张是最终结果,第二张是我现在得到的结果)
Option Explicit
Sub PrintLabels_Word()
Dim wb As Workbook, ws As Worksheet, wsPDF As Worksheet, xWs As Worksheet, WdObj As Object
Dim iLastRow As Long, ar(1 To 7, 1 To 1), rng As Range
Dim i As Long, r As Long, c As Integer, k As Integer
Dim LastRow As Long
Dim area As Range
Dim saveLocation As String
Dim strFileName As String, myRow As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets("Summary")
Set wsPDF = wb.Sheets("Foglio1")
Set xWs = Application.ActiveSheet
wsPDF.Cells.Clear
xWs.ResetAllPageBreaks
' fixed
ar(1, 1) = ws.Cells(1, 10)
ar(2, 1) = "CLIENT: " & ws.Cells(1, 7)
ar(3, 1) = "ORDER" & " " & ws.Cells(2, 7)
ar(5, 1) = "JOB" & " " & ws.Cells(1, 2) & " " & ws.Cells(1, 3)
ar(6, 1) = "CASE NUMBER: 1/1"
iLastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
r = 1 ' start row
c = 1 ' column A
For i = 6 To iLastRow
ar(4, 1) = "INSIDE CASE (TAG): " & ws.Cells(i, "H")
' fill plate
Set rng = wsPDF.Cells(r, c).Resize(7, 1)
rng.Value2 = ar
' merge cells
For k = 1 To 7
With rng.Cells(k, 1).Resize(1, 4)
.Merge
.HorizontalAlignment = xlCenter
.Font.Bold = True
End With
Next
r = r + 8
Next
MsgBox "Done"
With ActiveSheet.PageSetup
.Orientation = xlPortrait
.Zoom = 200
.FitToPagesTall = False
.FitToPagesWide = False
End With
Dim tblRange As Excel.Range
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim WordTable As Word.Table
iLastRow = wsPDF.Cells(Rows.Count, 1).End(xlUp).Row
Set tblRange = ThisWorkbook.Worksheets("Foglio1").Range("A1:D" & iLastRow)
On Error Resume Next
Set WordApp = GetObject(class:="Word.Application")
If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
'WordApp.Visible = True
'WordApp.Activate
Set WordDoc = WordApp.Documents.Add
tblRange.Copy
WordDoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
With WordApp
.ChangeFileOpenDirectory ThisWorkbook.Path
.ActiveDocument.SaveAs Filename:="Targhetta Adesiva" & " " & ws.Range("B3").Value & "_" &
ws.Range("G1").Value & ".doc"
.ActiveDocument.Close
End With
End Sub
【问题讨论】:
标签: excel vba ms-word formatting