|
刚才尝试下载并解压,能正常打开文本文件。干脆直接将代码发出,只是长了一点,不好看。现在连文本文档都要压缩上传。
至于楼主的两个宏,因代码有几千行,过程很多,注释很少,有用wd常量名也有用常量值,易读性不强。不知楼主是基于什么原因写那么多过程,用于对OCR识别文档的整理吗?如果是对普通拟文稿排版,不应有这么复杂吧?
没找到落款Update宏。我很少用selection对象,通常用range,因selection对象是一个全局性的,虽然方便灵活,但也可有其不确定性,一不小心会出错。另外,selection对象总在当前页面活动窗格,其变动会带动页面滚动,即使关闭刷新也会给人一种慢的感觉。
- Sub Main()
- '生成档案标签邮件合并主文档并执行合并
- Dim oDoc As Document
-
- On Error GoTo hd
- Application.ScreenUpdating = False
- Set oDoc = Documents.Add
- myPageSetup oDoc
- TablesAdding oDoc
- oDoc.Content.InsertParagraphAfter
- Mailmerging oDoc
- Application.ScreenUpdating = True
- Exit Sub
- hd:
- MsgBox "程序运行出现异常,退出!", vbCritical
- End Sub
- Sub myPageSetup(myDoc As Document)
- '主文档页面设置
- With myDoc.PageSetup
- .PaperSize = wdPaperA4
- .Orientation = wdOrientLandscape
- .TopMargin = CentimetersToPoints(0.5)
- .BottomMargin = CentimetersToPoints(0.5)
- .LeftMargin = CentimetersToPoints(0.5)
- .RightMargin = CentimetersToPoints(0.5)
- End With
- End Sub
- Sub TablesAdding(myDoc As Document)
- '在新文档插入嵌套表格,设置首个标签的框线,并以此为标准套用
- Dim oTable As Table
- Dim aTable As Table
- Dim aCell As Cell
- Dim i As Integer
-
- Options.DefaultBorderLineWidth = wdLineWidth150pt
- Options.DefaultBorderLineStyle = wdLineStyleSingle
- Set oTable = myDoc.Tables.Add(myDoc.Content, 1, 10, wdWord8TableBehavior)
- With oTable
- For Each aCell In .Range.Cells
- With aCell.Range
- .Collapse wdCollapseStart
- If i = 0 Then
- Set aTable = aCell.Tables.Add(.Duplicate, 7, 1)
- With aTable
- With .Borders
- .InsideLineStyle = Options.DefaultBorderLineStyle
- .InsideLineWidth = Options.DefaultBorderLineWidth
- .OutsideLineStyle = wdLineStyleThinThickThinSmallGap
- .OutsideLineWidth = wdLineWidth450pt
- .OutsideColorIndex = wdGreen
- End With
- .Range.Cells(2).Borders(wdBorderTop).Visible = False
- .Range.Cells(5).Borders(wdBorderTop).Visible = False
- SetCellHeightAndText i, myDoc, aCell.Tables(1)
- End With
- Else
- .FormattedText = aTable.Range.FormattedText '主文档后续标签套用首个标签样式
- With .Tables(1).Range.Cells(1).Range
- .Collapse wdCollapseStart
- myDoc.MailMerge.Fields.AddNext .Duplicate
- End With
- End If
- End With
- i = i + 1
- Next
- End With
- End Sub
- Sub SetCellHeightAndText(n As Integer, myDoc As Document, myTable As Table)
- '设置单个标签内容及格式
- Dim i As Integer
- Dim nCell As Cell
- Dim cellheight() As Variant
- Dim fieldnames() As Variant
- Dim fontsizes() As Variant
- Dim textwidths() As Variant
- Dim myField As MailMergeField
-
- cellheight = Array(1.2, 1.2, 1.8, 1.8, 9, 1.8, 1.8) '标签中各单元格的高度
- textwidths = Array(2, 2, 2.2, 1.2, 8.2, 2.2, 2.2) '标签各项内容的宽度
- fontsizes = Array(18, 18, 22, 22, 26, 22, 22) '标签各项内容的字符磅值
- fieldnames = Array("", "单位名称", "档案名称", "编号", "案卷名称", "类别", "年度")
-
- myTable.Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter
- myTable.Rows.Alignment = wdAlignRowCenter
- myTable.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
-
- For Each nCell In myTable.Range.Cells
- nCell.HeightRule = wdRowHeightExactly
- nCell.Height = CentimetersToPoints(cellheight(i))
- nCell.Range.Font.Size = fontsizes(i)
- If i > 0 Then
- myDoc.MailMerge.Fields.Add nCell.Range, fieldnames(i)
- nCell.Range.FitTextWidth = CentimetersToPoints(textwidths(i))
- With nCell.Range.Font
- If i < 3 Then
- .NameFarEast = "宋体"
- .Color = wdColorRed
- ElseIf i = 4 Then
- .NameFarEast = "方正小标宋简体"
- nCell.Range.Orientation = wdTextOrientationVerticalFarEast
- Else
- .NameFarEast = "楷体_GB2312"
- End If
- End With
- End If
- i = i + 1
- Next
- End Sub
- Sub Mailmerging(myDoc As Document)
- '设置并执行邮件合并
- With myDoc.MailMerge
- .MainDocumentType = wdDirectory
- .Destination = wdSendToNewDocument
- .SuppressBlankLines = False
- .DataSource.FirstRecord = wdDefaultFirstRecord
- .DataSource.LastRecord = wdDefaultLastRecord
- If Dialogs(81).Show = -1 Then .Execute True '如手动打开数据源则执行合并
- End With
- End Sub
复制代码 |
|