1. Sub LayoutForExamPaper()
  2. Dim StartTime As Variant
  3. Dim UsedTime As Variant
  4. StartTime = VBA.Timer
  5. Application.ScreenUpdating = False
  6. Dim oneP As Paragraph
  7. Dim rng As Range
  8. Call ClearParagraphFill
  9. Call ConvertNoToText '项目编号转为文本
  10. Call ConvertShape '图形转为inlineShape
  11. Call DivideInLineShape '图文分段
  12. Call ReplaceABCDNUM '统一选项字母为半角字母
  13. Call ZeroIndent '0缩进
  14. '全文居左对齐
  15. ActiveDocument.Paragraphs.Format.Alignment = wdAlignParagraphLeft
  16. '删除所有空行
  17. ActiveDocument.Content.Find.Execute "^13[  ^t" & ChrW(160) & "^11^13]{1,}", , , 2, , , , , , "^13", 2
  18. '替换所有空白
  19. ActiveDocument.Content.Find.Execute "^w", , , 0, , , , , , "^s", 2
  20. '全角点号转为半角点号
  21. 'ActiveDocument.Content.Find.Execute ".", , , 0, , , , , , ".", 2
  22. '替换手动换行符
  23. ActiveDocument.Content.Find.Execute "^l", , , 0, , , , , , "^13", 2
  24. '插入空白段落
  25. ActiveDocument.Range(0, 0).InsertBefore vbCrLf
  26. '删除段首空白
  27. ActiveDocument.Content.Find.Execute "^13@^s@([!^s]@)", , , 1, , , , , , "^13\1", 2
  28. '删除事先插入的空白段落
  29. ActiveDocument.Paragraphs(1).Range = ""
  30. '统一题号标点
  31. ActiveDocument.Content.Find.Execute "([0-9]@)[.、]([!^s0-9]@)", , , 1, , , , , , "\1.\2", 2
  32. '删除ABCD及题号尾随空白
  33. ActiveDocument.Content.Find.Execute "([A-D0-9]@)[.、]^s@([!^s]@)", , , 1, , , , , , "\1.\2", 2
  34. 'ABCD选项独立为行
  35. ActiveDocument.Content.Find.Execute "[!^13]([B-D].)", , , 1, , , , , , "^13\1", 2
  36. '删除题干和选项段尾空白
  37. ActiveDocument.Content.Find.Execute "(^13[A-D0-9]@.[!^s]@)^s@(^13)", , , 1, , , , , , "\1\2", 2
  38. '选项中间的空白替换为顿号 一个选项多个部分组成的情况
  39. For n = 1 To 5 '最多支持一个选项有5个部分构成 有疑问 括号内多处顿号的问题
  40. ActiveDocument.Content.Find.Execute "(^13[A-D].[! ^s\((]@)^s@([!^s\))]@)", , , 1, , , , , , "\1、\2", 2
  41. Next n
  42. Debug.Print " "
  43. '删除题干中的空白
  44. For n = 1 To 5 '最多支持一个题干有5处部分构成
  45. ActiveDocument.Content.Find.Execute "(^13[0-9]@.[!^s\((]@)^s@([!^s\))]@)", , , 1, , , , , , "\1\2", 2
  46. Next n
  47. '统一括号内为四个空白字符 如 12.该岛屿孤猴集中分布区的自然景观是( )
  48. ActiveDocument.Content.Find.Execute "^13([0-9]@.[!^s]@)[\((]^s@[\))]^13", , , 1, , , , , , "^13\1( )^13", 2
  49. '假回车转硬回车
  50. ActiveDocument.Content.Find.Execute "^13", , , 0, , , , , , "^p", 2
  51. '删除分页符
  52. ActiveDocument.Content.Find.Execute "^m", , , 0, , , , , , "", 2
  53. ActiveDocument.Content.Find.Execute ".", , , 0, , , , , , ".", 2
  54. Call ModifyFont '根据行首行尾字符判断 修改字体格式
  55. Call AddTabStopForOptions '根据选项长度添加制表位
  56. Call InsertPageNo '插入页码
  57. Call PageSetUpB5 '设置纸张
  58. Application.ScreenUpdating = True
  59. UsedTime = VBA.Timer - StartTime
  60. Debug.Print "UsedTime :" & Format(UsedTime, "#0.0000 Seconds")
  61. 'MsgBox "UsedTime :" & Format(UsedTime, "#0.0000 Seconds")
  62. End Sub
  63.  
  64. Private Sub ZeroIndent()
  65. '清除缩进
  66. With ActiveDocument.Paragraphs.Format
  67. .TabStops.ClearAll
  68. .CharacterUnitLeftIndent = 0
  69. .CharacterUnitRightIndent = 0
  70. .CharacterUnitFirstLineIndent = 0
  71. '以上三句必须在前面 而以下三句必须在后面才能生效
  72. .FirstLineIndent = CentimetersToPoints(0)
  73. .LeftIndent = CentimetersToPoints(0)
  74. .RightIndent = CentimetersToPoints(0)
  75. .SpaceBefore = 0
  76. .SpaceBeforeAuto = False
  77. .SpaceAfter = 0
  78. .SpaceAfterAuto = False
  79. .LineUnitBefore = 0
  80. .LineUnitAfter = 0
  81. .MirrorIndents = False
  82. End With
  83. End Sub
  84.  
  85. Private Sub ClearParagraphFill()
  86. With ActiveDocument.Paragraphs.Format
  87. With .Shading
  88. .Texture = wdTextureNone
  89. .ForegroundPatternColor = wdColorAutomatic
  90. .BackgroundPatternColor = wdColorAutomatic
  91. End With
  92. .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
  93. .Borders(wdBorderRight).LineStyle = wdLineStyleNone
  94. .Borders(wdBorderTop).LineStyle = wdLineStyleNone
  95. .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  96. .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
  97. With .Borders
  98. .DistanceFromTop = 1
  99. .DistanceFromLeft = 4
  100. .DistanceFromBottom = 1
  101. .DistanceFromRight = 4
  102. .Shadow = False
  103. End With
  104. End With
  105. With Options
  106. .DefaultBorderLineStyle = wdLineStyleSingle
  107. .DefaultBorderLineWidth = wdLineWidth050pt
  108. .DefaultBorderColor = wdColorAutomatic
  109. End With
  110. End Sub
  111.  
  112. Private Sub ConvertNoToText()
  113. Dim oneList As List
  114. For Each oneList In ActiveDocument.Lists
  115. oneList.ConvertNumbersToText
  116. Next
  117. End Sub
  118.  
  119. Private Sub ModifyFont()
  120. Dim rng As Range
  121. For Each oneP In ActiveDocument.Paragraphs
  122. n = n + 1
  123. Set rng = oneP.Range
  124. If Not rng.Information(wdWithInTable) Then
  125. Count = Len(rng.Text)
  126. '题干和选项、综合题小题等 字体设置
  127. If rng.MoveStartWhile("(())01234567890123456789ABCDABCD①②③④⑤⑥⑦⑧⑨⑩.、.分", wdForward) >= 1 Then
  128. With oneP.Range.Font
  129. .Name = "宋体"
  130. .Size = 10.5
  131. .ColorIndex = wdBlack
  132. .Bold = False
  133. .Italic = False
  134. End With
  135. Else
  136. '题型字体设置
  137. If rng.MoveStartWhile("第一二三部分.、.非选择综合题Ⅰ卷Ⅱ卷", wdForward) > 1 Then
  138. With oneP.Range.Font
  139. .Name = "宋体"
  140. .Size = 12
  141. .Bold = True
  142. .Italic = False
  143. .ColorIndex = wdBlack
  144. End With
  145. Else
  146. '引言字体设置
  147. If rng.MoveEndWhile("1234567890~-据此完成回答下列各题.。(())分结合材料下面小" & Chr(13) & Chr(11), wdBackward) < -2 Or _
  148. rng.MoveStartWhile("材料一二三四五六七、:", wdForward) > 1 Then
  149. With oneP.Range.Font
  150. .Name = "楷体"
  151. .Size = 10.5
  152. .ColorIndex = wdBlack
  153. .Bold = False
  154. .Italic = False
  155. End With
  156. Else
  157. With oneP.Range.Font
  158. .Name = "宋体"
  159. .Size = 10.5
  160. .ColorIndex = wdBlack
  161. .Bold = False
  162. .Italic = False
  163. End With
  164.  
  165. End If
  166. End If
  167. End If
  168. End If
  169. Next
  170. End Sub
  171.  
  172. Private Sub AddTabStopForOptions()
  173. '处理选项和制表位
  174. Dim rng As Range
  175. Dim ap As Paragraph, bp As Paragraph, cp As Paragraph, dp As Paragraph
  176. lenth = ActiveDocument.PageSetup.CharsLine
  177. For i = ActiveDocument.Paragraphs.Count To 4 Step -1
  178. Set oneP = ActiveDocument.Paragraphs(i)
  179. Set rng = oneP.Range
  180. If Not rng.Information(wdWithInTable) Then
  181. movestep = rng.MoveStartWhile("D..", 10)
  182. If movestep >= 2 Then
  183. Set dp = ActiveDocument.Paragraphs(i)
  184. Set cp = ActiveDocument.Paragraphs(i - 1)
  185. Set bp = ActiveDocument.Paragraphs(i - 2)
  186. Set ap = ActiveDocument.Paragraphs(i - 3)
  187. If dp.Range.ComputeStatistics(wdStatisticCharacters) < lenth / 4 - 1 And _
  188. cp.Range.ComputeStatistics(wdStatisticCharacters) < lenth / 4 - 1 And _
  189. bp.Range.ComputeStatistics(wdStatisticCharacters) < lenth / 4 - 1 And _
  190. ap.Range.ComputeStatistics(wdStatisticCharacters) < lenth / 4 - 1 Then '一行足够
  191. ap.Range.Text = vbTab & Replace(ap.Range.Text, Chr(13), vbTab) & Replace(bp.Range.Text, Chr(13), vbTab) & Replace(cp.Range.Text, Chr(13), vbTab) & dp.Range.Text
  192. bp.Range.Text = ""
  193. cp.Range.Text = ""
  194. dp.Range.Text = ""
  195. AddTabStopInRange ActiveDocument.Paragraphs(i - 3).Range, 4
  196. 'Debug.Print "一行"
  197. Else
  198. If dp.Range.ComputeStatistics(wdStatisticCharacters) > lenth / 2 - 2 Or _
  199. cp.Range.ComputeStatistics(wdStatisticCharacters) > lenth / 2 - 2 Or _
  200. bp.Range.ComputeStatistics(wdStatisticCharacters) > lenth / 2 - 2 Or _
  201. ap.Range.ComputeStatistics(wdStatisticCharacters) > lenth / 2 - 2 Then '分四行好看
  202. dp.Range.Text = vbTab & dp.Range.Text
  203. cp.Range.Text = vbTab & cp.Range.Text
  204. bp.Range.Text = vbTab & bp.Range.Text
  205. ap.Range.Text = vbTab & ap.Range.Text
  206. AddTabStopInRange ActiveDocument.Paragraphs(i - 3).Range, 1
  207. AddTabStopInRange ActiveDocument.Paragraphs(i - 2).Range, 1
  208. AddTabStopInRange ActiveDocument.Paragraphs(i - 1).Range, 1
  209. AddTabStopInRange ActiveDocument.Paragraphs(i).Range, 1
  210. 'Debug.Print "四行"
  211. Else '分两行
  212. ap.Range.Text = vbTab & Replace(ap.Range.Text, Chr(13), vbTab) & bp.Range.Text
  213. bp.Range.Text = vbTab & Replace(cp.Range.Text, Chr(13), vbTab) & dp.Range.Text
  214. cp.Range.Text = ""
  215. dp.Range.Text = ""
  216. AddTabStopInRange ActiveDocument.Paragraphs(i - 3).Range, 2
  217. AddTabStopInRange ActiveDocument.Paragraphs(i - 2).Range, 2
  218. End If
  219. End If
  220. End If
  221. End If
  222. Next i
  223. End Sub
  224.  
  225. Private Sub AddTabStopInRange(ByVal rng As Range, ByVal tabStopCount As Integer)
  226. Dim pgWidth As Double, pgLeftMargin As Double, opWidth As Integer
  227. Dim chrLine As Integer, i As Integer
  228. With ActiveDocument.PageSetup
  229. pgLeftMargin = .LeftMargin
  230. pgWidth = .PageWidth - .LeftMargin - .RightMargin
  231. End With
  232. opWidth = Int(pgWidth / tabStopCount) '计算选项宽度
  233. chrLine = ActiveDocument.PageSetup.CharsLine '获取每行字符数
  234. rng.ParagraphFormat.TabStops.ClearAll '清除原有制表位
  235. '新增制表位
  236. For i = 1 To tabStopCount
  237. rng.ParagraphFormat.TabStops.Add Position:=20 + (i - 1) * opWidth, _
  238. Leader:=wdTabLeaderSpaces, Alignment:=wdAlignTabLeft
  239. Next i
  240. End Sub
  241.  
  242. Private Sub ConvertShape()
  243. '转换图形
  244. Dim shp As Shape
  245. Dim inshp As InlineShape
  246. ConvertTime = 0
  247. Do While ActiveDocument.Shapes.Count > 0
  248. ConvertTime = ConvertTime + 1
  249. For Each shp In ActiveDocument.Shapes
  250. shp.ConvertToInlineShape
  251. Next shp
  252. If ConvertTime > 20 Then Exit Do
  253. Loop
  254. End Sub
  255.  
  256. Private Sub DivideInLineShape()
  257. Dim p As Paragraph
  258. Dim rng As Range
  259. For i = ActiveDocument.Paragraphs.Count To 1 Step -1
  260. Set p = ActiveDocument.Paragraphs(i)
  261. If p.Range.InlineShapes.Count > 0 Then
  262. pic = 0
  263. '不断向后查找段落中inlineshape的位置 并插入回车
  264. lenth = Len(p.Range.Text)
  265. Set rng = p.Range
  266. hasMove = rng.MoveStartUntil(Chr(47), lenth)
  267. m = 0
  268. Do While hasMove > 0
  269. If rng.Characters.First.Previous <> Chr(13) Then
  270. rng.InsertBefore vbCrLf
  271. End If
  272. rng.Start = rng.Start + 1
  273. If rng.Characters.First.Next <> Chr(13) Then
  274. rng.InsertBefore vbCrLf
  275. End If
  276. lenth = Len(rng.Text)
  277. hasMove = rng.MoveStartUntil(Chr(47), lenth)
  278. m = m + 1
  279. If m = 20 Then Exit Do
  280. Loop
  281. End If
  282. Next i
  283. End Sub
  284.  
  285. Private Sub ReplaceABCDNUM()
  286. '猜测可能是因为全角符号是两个字符长度
  287. '所以不能在通配查找里面使用字符组[ABCD],因为字符组内每个字符要求单字符长度
  288. Const qjzm As String = "ABCD0123456789. "
  289. Const bjzm As String = "ABCD0123456789. "
  290. Dim idx As Integer
  291. For idx = 1 To 4
  292. ActiveDocument.Content.Find.Execute Mid(qjzm, idx, 1), , , 0, , , , , , Mid(bjzm, idx, 1), 2
  293. Next idx
  294. End Sub
  295.  
  296. Private Sub InsertPageNo()
  297. Dim rng As Range
  298. With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
  299. Set rng = .Range
  300. rng.Font.Size = 10.5
  301. rng.Font.Name = "Times New Roman"
  302. ActiveDocument.Fields.Add rng, wdFieldEmpty, "Page"
  303. .Range.Fields.Update
  304. .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
  305. End With
  306. If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
  307. ActiveWindow.Panes(2).Close
  308. End If
  309. If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
  310. ActivePane.View.Type = wdOutlineView Then
  311. ActiveWindow.ActivePane.View.Type = wdPrintView
  312. End If
  313. 'ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
  314. 'Selection.WholeStory
  315. 'Selection.Delete
  316. 'With Selection.ParagraphFormat
  317.  
  318. ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
  319. Selection.WholeStory
  320. Selection.Delete
  321. Selection.ClearFormatting
  322.  
  323. With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
  324. .Delete '删除段落
  325. With .ParagraphFormat
  326. .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
  327. .Borders(wdBorderRight).LineStyle = wdLineStyleNone
  328. .Borders(wdBorderTop).LineStyle = wdLineStyleNone
  329. .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  330. With .Borders
  331. .DistanceFromTop = 1
  332. .DistanceFromLeft = 4
  333. .DistanceFromBottom = 1
  334. .DistanceFromRight = 4
  335. .Shadow = False
  336. End With
  337. End With
  338. End With
  339. With Options
  340. .DefaultBorderLineStyle = wdLineStyleSingle
  341. .DefaultBorderLineWidth = wdLineWidth075pt
  342. .DefaultBorderColor = wdColorAutomatic
  343. End With
  344. ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
  345. End Sub
  346.  
  347. Private Sub PageSetUpB5()
  348. With ActiveDocument.Styles(wdStyleNormal).Font
  349. If .NameFarEast = .NameAscii Then
  350. .NameAscii = ""
  351. End If
  352. .NameFarEast = ""
  353. End With
  354. With ActiveDocument.PageSetup
  355. .LineNumbering.Active = False
  356. .Orientation = wdOrientPortrait
  357. .TopMargin = CentimetersToPoints(1.5)
  358. .BottomMargin = CentimetersToPoints(1.5)
  359. .LeftMargin = CentimetersToPoints(1.5)
  360. .RightMargin = CentimetersToPoints(1.5)
  361. .Gutter = CentimetersToPoints(0)
  362. .HeaderDistance = CentimetersToPoints(1.5)
  363. .FooterDistance = CentimetersToPoints(1.5)
  364. .PageWidth = CentimetersToPoints(18.2)
  365. .PageHeight = CentimetersToPoints(25.7)
  366. .FirstPageTray = wdPrinterDefaultBin
  367. .OtherPagesTray = wdPrinterDefaultBin
  368. .SectionStart = wdSectionNewPage
  369. .OddAndEvenPagesHeaderFooter = False
  370. .DifferentFirstPageHeaderFooter = False
  371. .VerticalAlignment = wdAlignVerticalTop
  372. .SuppressEndnotes = False
  373. .MirrorMargins = False
  374. .TwoPagesOnOne = False
  375. .BookFoldPrinting = False
  376. .BookFoldRevPrinting = False
  377. .BookFoldPrintingSheets = 1
  378. .GutterPos = wdGutterPosLeft
  379. .LayoutMode = wdLayoutModeLineGrid
  380. End With
  381. End Sub

  

20190412wdVBA 排版的更多相关文章

  1. android textview 自动换行 整齐排版

    一.问题在哪里? textview显示长文字时会进行自动折行,如果遇到一些特殊情况,自动折行会杯具成这个样子: 上述特殊情况包括: 1)全角/半角符号混排(一般是数字.字母.汉字混排) 2)全角/半角 ...

  2. 网页万能排版布局插件,web视图定位布局创意技术演示页

    html万能排版布局插件,是不是感觉很强大,原理其实很简单,不过功能很强大哈哈,大量节省排版布局时间啊! test.html <!doctype html> <html> &l ...

  3. 用EmEditor实现PDF转Word后的对齐排版

    Redraw = false//禁止重绘(类似于VBA中的: Application.screenupdating=FALSE),以提高运行效率 //去除所有空行和只由空白字符构成的行 documen ...

  4. bootstrap学习笔记--bootstrap排版类的使用

    标题 Bootstrap 中定义了所有的 HTML 标题(h1 到 h6)的样式,这个和一般的html没啥区别.请看下面的实例: <h1>测试1 h1</h1> <h2& ...

  5. Windows下LATEX排版论文攻略—CTeX、JabRef使用介绍

    Windows下LATEX排版论文攻略—CTeX.JabRef使用介绍 一.工具介绍 TeX是一个很好排版工具,在学术界十分流行,特别是数学.物理学和计算机科学界. CTeX是TeX中的一个版本,指的 ...

  6. eclipse自动排版JSP问题

    eclipse自动排版JSP非常难看,标签每行显示不完整,开发时很难受,下面设置一下这个就好多了: window-->preferences-->Web-->HTML Files-- ...

  7. html学习第二天—— 第九、十章——CSS的继承、层叠和特殊性+CSS格式化排版

    继承CSS的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代.比如下面代码:如某种颜色应用于p标签,这个颜色设置不仅应用p标签, ...

  8. bootstrap之排版类

    bootstrap之排版类

  9. 测试 MathJax 排版功效

    这是第一篇博文,用于检测博客园提供的数学排版功能,下面是一些数学公式. \[ \text{sgn}(\mathbf{w}^T\phi(\mathbf{x})+b) = \text{sgn}\left( ...

随机推荐

  1. java项目的异常处理

    异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error:如果你用System.ou ...

  2. 用原生js+canvas实现五子棋

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. 关于Ajax的认识和封装(小记)

    一,Ajax 的概念 1,Ajax 是一种在无需重新加载整个网页(即刷新网页)的情况下,能够更新部分网页的技术. 2,Ajax 的全称是Asynchronous Javascript And XML” ...

  4. Centos7 关于防火墙的一些简单配置

    近期安装了linux系统Centos7,接触下来发现了与原来的Centos6.5有一些差别,这里主要记录下来我的一些关于Centos7防火墙的了解. 一.firewall简介 CentOS 7中防火墙 ...

  5. Python_lambda

    最近学习到python的lambda表达式也是匿名函数, lambda不需要使用def 语句这样标准的形式定义一个函数,并不需要花很多时间去额外定义一个不常用的函数.lambda的本省就是一个长度为一 ...

  6. CASE WHEN 及 SELECT CASE WHEN的用法

    CASE WHEN 及 SELECT CASE WHEN的用法 Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE sex WHEN '1' THEN '男' WH ...

  7. Vue渐进式JavaScript 框架

    1. Vue简介 1.1  初步了解Vue.js框架 Vue.js (读音 /vjuː/,类似于 view) 是一种轻量级前端MVVM框架.同时吸收了React(组件化)和Angular(灵活指令页面 ...

  8. 使用Redux DevTools浏览器插件调试redux

    与redux的Devtools模块不同,该工具主要依赖浏览器插件完成.模式也比Devtools简单点. step1 下载插件 Chrome地址(360极速模式也可以用): https://chrome ...

  9. spoj 1029 Matrix Summation

    题意: 对一个矩阵有2种操作: 1.把某个元素设为x. 2.查询以(x1,y1)为左上角 以(x2,y2)为右上角的矩阵中的数字的和. 思路: 二维树状数组入门题,同时对横坐标和纵坐标做前缀和就行了. ...

  10. spring里的事物设置

    有的人说事物在spring里设置有两种,其实事物设置在spring配置文件中共有五种方式:第一种方式:每个Bean都有一个代理第二种方式:所有Bean共享一个代理基类第三种方式:使用拦截器第四种方式: ...