|
楼主的表格太不规范了,表格之前的内容可能是1行-3行,做起来懵。
给个表格规范时的中间过程吧,希望楼主有能力研究- Sub 批量修改_MeThee()
- On Error Resume Next
- Application.ScreenUpdating = False
- Dim aPath$, arr, aDoc As Document
- Dim aTable As Table, aRange As Range
- Dim aTableCount&, aParaCount, aLength&, bLength&
- Dim aSectionCount&
- aPath = ThisDocument.Path
- arr = mySearch(aPath & "\107\*.doc") '遍历结果
- iLbound = LBound(arr)
- iUbound = UBound(arr)
- For i = iLbound To iUbound - 1
- If InStr(1, arr(i), "~") = 0 Then
- Set aDoc = Documents.Open(arr(i))
- aSectionCount = aDoc.Sections.Count
- For j = 1 To aSectionCount
- With aDoc.Sections(j).PageSetup
- .TopMargin = CentimetersToPoints(2)
- .BottomMargin = CentimetersToPoints(2)
- .LeftMargin = CentimetersToPoints(2.5)
- .RightMargin = CentimetersToPoints(1.5)
- '.HeaderDistance = 1.5
- '.FooterDistance = 1.75
- End With
- Next j
- aTableCount = aDoc.Tables.Count
- For j = 1 To aTableCount
- Set aTable = aDoc.Tables(j)
- aParaCount = aDoc.Range(0, aTable.Range.Start).Paragraphs.Count
- Set aRange = aDoc.Paragraphs(aParaCount - 2).Range
- aRange.SetRange aRange.Start, aRange.End - 1
- With aRange '设置第一行格式
- .Font.Name = "宋体"
- .Font.Size = 15
- .Font.Bold = True
- .Font.Spacing = 3 '设置字距
- .ParagraphFormat.LineSpacingRule = wdLineSpaceExactly
- .ParagraphFormat.LineSpacing = 22
- .ParagraphFormat.Alignment = wdAlignParagraphCenter
- End With
- aLength = myLength(aRange)
- Set aRange = aDoc.Paragraphs(aParaCount - 1).Range
- aRange.SetRange aRange.Start, aRange.End - 1
- aRange.Text = VBA.Trim$(aRange.Text)
- With aRange
- .Font.Name = "宋体"
- .Font.Size = 18
- .Font.Bold = True
- .ParagraphFormat.LineSpacingRule = wdLineSpaceExactly
- .ParagraphFormat.LineSpacing = 22
- .ParagraphFormat.Alignment = wdAlignParagraphCenter
- '根据特殊要求调整字距
- aSpacing = .Font.Spacing
- aChrCount = .Characters.Count
- aRange.SetRange aRange.Start, aRange.End - 1
- bLength = myLength(aRange)
- aRange.Select
- s:
- If bLength < aLength Then
- Selection.Font.Spacing = Selection.Font.Spacing + 1
- bLength = myLength(aRange)
- If bLength < aLength Then GoTo s
- End If
- .Font.Spacing = .Font.Spacing + 0.5
- End With
- With aRange.Find
- Set bRange = aRange
- .ClearFormatting
- .MatchWildcards = True
- .Text = "[0-9A-Za-z]{2}"
- .Forward = True
- .Wrap = wdFindStop
- Do While .Execute = True
- Set aRange = .Parent
- aRange.SetRange aRange.Start, aRange.End - 1
- aRange.Font.Spacing = 0
- aRange.SetRange aRange.End, bRange.End
- Loop
- End With
- Set aRange = aDoc.Paragraphs(aParaCount).Range
- With aRange
- .Font.Name = "宋体"
- .Font.Size = 10
- End With
- '标题段格式设置完成
- With aTable.Range.Font
- .Name = "宋体" '设置表格字体
- .Size = 9
- .Bold = False '去除加粗
- End With
- '这里是其他操作
- Next j
- aDoc.Close True '保存关闭
- End If
- Next i
- Application.ScreenUpdating = True
- End Sub
复制代码- Enum aSearchType
- aSearchTypeFolder = 0
- aSearchTypeFile = 1
- aSearchTypeAll = 2
- End Enum
- Function mySearch(aPath$, Optional searchTraversal As Boolean = True, Optional searchType As aSearchType = aSearchTypeFile) As String()
- '参数1:遍历目录全路径
- '参数2:是否搜索子目录
- '参数3:搜索文件、目录或者全部
- Dim aNum&, cmdStr$, folder$, aTemp$
- folder = """" & aPath & """"
- cmdStr = Environ$("comspec") & " /c dir " & folder '初始化字串
- If searchTraversal Then cmdStr = cmdStr & " /s" '定义是否遍历
- aTemp = Mid(aPath, InStrRev(aPath, "") + 1) '获取最后一个""后的内容
- If Left(aTemp, 2) = "*." Then searchType = aSearchTypeFile
- If searchType = aSearchTypeFile Then '定义搜索文件、目录还是全部
- cmdStr = cmdStr & " /a:-d /b"
- ElseIf searchType = aSearchTypeFolder Then
- cmdStr = cmdStr & " /a:d /b"
- ElseIf searchType = aSearchTypeAll Then
- cmdStr = cmdStr & " /b"
- End If
- cmdStr = cmdStr & " > C:\aTemp.txt"
- With CreateObject("WScript.Shell")
- .Run cmdStr, 0, True
- End With
- aNum = FreeFile
- Open "C:\aTemp.txt" For Input As #aNum
- mySearch = Split(StrConv(InputB(LOF(aNum), aNum), vbUnicode), vbCrLf)
- Close #aNum
- End Function
- Public Function myRegExp(ByVal str$, ByVal aPattern$)
- With CreateObject("Vbscript.RegExp")
- .Global = True
- .Pattern = aPattern '"[^一-﨩]"
- myRegExp = .Replace(str, "")
- End With
- End Function
- Function myLength(aRange As Range)
- Dim aLeft&, aRight&
- Dim aStart&, aEnd&
- aStart = aRange.Start
- aEnd = aRange.End
- With aRange
- aLeft = .Information(wdHorizontalPositionRelativeToPage)
- .SetRange .End, .End
- aRight = .Information(wdHorizontalPositionRelativeToPage)
- .SetRange aStart, aEnd
- End With
- myLength = Abs(aRight - aLeft)
- End Function
复制代码 |
|