|
本帖最后由 yuanmeng7848 于 2023-11-13 09:24 编辑
Sub ExcelToWordAndPrint()
Dim WordObject As Object
Dim MyWord As Object
Dim MyWordRange As Object
On Error Resume Next
Set WordObject = CreateObject("Word.Application")
WordObject.Visible = 0
Set MyWord = WordObject.Documents.Add 'DocumentType:=wdNewBlankDocument
'WordObject.Application.Activate
With MyWord.ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.PageWidth = WordObject.CentimetersToPoints(21)
.PageHeight = WordObject.CentimetersToPoints(29.7)
.TopMargin = WordObject.CentimetersToPoints(6)
.BottomMargin = WordObject.CentimetersToPoints(2)
.LeftMargin = WordObject.CentimetersToPoints(1.5)
.RightMargin = WordObject.CentimetersToPoints(1.5)
.HeaderDistance = WordObject.CentimetersToPoints(0.5)
.FooterDistance = WordObject.CentimetersToPoints(0.5)
End With
For Each Sh In Worksheets
If Sh.Name = "甲组" Or Sh.Name = "乙组" Or Sh.Name = "丙组" Then
For i = 3 To Sh.Cells(Rows.Count, 6).End(xlUp).Row
MyWord.Range(0, MyWord.Range.End) = ""
If Sh.Cells(i, 9) = "" Then
MyWord.Content.InsertAfter Sh.Cells(i, 2) & "班" & Sh.Cells(i, 1) & "同学在*****"
MyWord.Content.InsertAfter Mid(Sheets("运动员信息汇总").Cells(1, 1), 5, 9) & Sh.Cells(i, 3)
MyWord.Content.InsertAfter "子" & Sh.Name & Sh.Cells(i, 5) & "比赛中以" & Sh.Cells(i, 6) & "的成绩荣获"
MyWord.Content.InsertAfter vbCrLf & Sh.Cells(i, 7) & vbCrLf & "特发此奖,以资鼓励!" & vbCrLf
MyWord.Content.InsertAfter "********" & vbCrLf & Format(Date, "yyyy年mm月dd日")
Set MyWordRange = MyWord.Range
With MyWordRange
.Paragraphs(1).Range.ParagraphFormat.CharacterUnitFirstLineIndent = 2
.Paragraphs(1).Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Paragraphs(1).Range.Font.Size = 24
.Paragraphs(1).Range.Font.Name = "华文行楷"
.Paragraphs(1).Range.Font.Bold = True
.Paragraphs(2).Range.ParagraphFormat.CharacterUnitFirstLineIndent = 0
.Paragraphs(2).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Paragraphs(2).Range.Font.Size = 36
.Paragraphs(2).Range.Font.Name = "华文行楷"
.Paragraphs(2).Range.Font.Bold = True
'.Paragraphs(3).Range.ParagraphFormat.CharacterUnitFirstLineIndent = 0
.Paragraphs(3).Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Paragraphs(3).Range.Font.Size = 24
.Paragraphs(3).Range.Font.Name = "华文行楷"
.Paragraphs(3).Range.Font.Bold = True
.Paragraphs(4).Range.ParagraphFormat.CharacterUnitFirstLineIndent = 19
.Paragraphs(4).Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Paragraphs(4).Range.Font.Size = 24
.Paragraphs(4).Range.Font.Name = "华文行楷"
.Paragraphs(4).Range.Font.Bold = True
.Paragraphs(5).Range.ParagraphFormat.CharacterUnitFirstLineIndent = 19
.Paragraphs(5).Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Paragraphs(5).Range.Font.Size = 24
.Paragraphs(5).Range.Font.Name = "华文行楷"
.Paragraphs(5).Range.Font.Bold = True
End With
WordObject.PrintOut
Sh.Cells(i, 9) = "已打印"
Exit For
End If
Next
End If
'Exit For
Next
WordObject.Saved = True
WordObject.ActiveDocument.SaveAs ThisWorkbook.Path & "\打印奖状.docx"
WordObject.Application.Quit
Set WordObject = Nothing
End Sub
关于上面这段代码向各位大师请教两个问题:
1、设置页面部分未能启用(本想设置页面为横向,上边距为6cm)
2、段落设置部分:第一段设置正常,第二段本想设置成居中,第三段左对齐,第四、五段缩进19个字符。
结果二、三、四、五段完全一样。怎么回事?但第二段字号没问题!
请高手指点,谢谢!
|
|