ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: 413191246se

[分享] 选项对齐(宏)最新更新 2022-10-01

[复制链接]

TA的精华主题

TA的得分主题

发表于 2022-10-4 21:37 | 显示全部楼层
413191246se 发表于 2022-10-4 19:40
* sylun 兄:我用表格填充版心再减去两个 0.19 厘米(想起过去《表格处理》宏里面的 .padding=0.19)得到当 ...

能否请413191246se重新分享下处理 2、3、4、5 个选项的宏代码,忘记保存了
英文试卷可能涉及选项不一样情况,有些选择题是3个选项,有些是4个选项

TA的精华主题

TA的得分主题

发表于 2022-10-5 00:17 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
tangqingfu 发表于 2022-10-4 21:34
sylun兄,用您的代码测试附件的文档,好像效果不好,是不是我操作不对

tang兄的新文档与之前提供的文档有两点不同:一是两节的页边距设置不同,导致wid值出错,前面我已表示忽略右缩进与节格式页边距差异,要考虑就得用Range对象的Information属性获取aRange对象所在的节编号,再获取该节的页边距信息,就新文档本例可将第6行代码改为With ActiveDocument.Sections(1).PageSetup。
二是文档中有的选项中隐藏了不可见的1号字符,不知从何而来的,十分奇怪。要先清除它,可将第67行代码改为:For Each Match In .Execute(Replace(aRange.Text, Chr(1), ""))

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-10-5 01:51 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
tang 兄:还好,最近我保存了一些历史版本。在 9月30日 的版本中找到了 2345 个选项对齐的宏。
sylun 兄:像示例文档中有两个不可见字符,我觉得不清除也可以,无非就是多占几行。
今晚,我又折腾了一下《公文排版、普通排版、条文排版》三个宏,前两个默认段后一行,后者段后空段。
  1. Sub OptionAlign()
  2. '选项对齐
  3.     Dim t As Table, i As Paragraph, TableWidth!, oTab!, Tab1!, Tab2!, Tab3!, Tab4!, OptionNum&

  4.     With ActiveDocument
  5.         .Tables.Add .Range(0, 0), 1, 1
  6.         TableWidth = Round(.Tables(1).Cell(1, 1).Width / 28.35, 2)
  7.         .Tables(1).Delete

  8.         .Content.Find.Execute "^l", , , 0, , , , , , "^p", 2

  9.         For Each t In .Tables
  10.             With t.Range.Rows
  11.                 .WrapAroundText = False
  12.                 .Alignment = wdAlignRowCenter
  13.             End With
  14.         Next

  15.         With .Content.Find
  16.             .Execute "(^13)([  ^s^t]{1,})", , , 1, , , , , , "\1", 2
  17.             .Execute "([  ^s^t]{1,})(^13)", , , 1, , , , , , "\2", 2
  18.             .Execute "([A-E])[.、]", , , 1, , , , , , "\1.", 2
  19.             .Execute "([  ^s^t^13]{1,})([B-E].)", , , 1, , , , , , "\2", 2
  20.             .Execute "(^13)([0-9]{1,})[.、]", , , 1, , , , , , "\1\2.", 2
  21.             .Execute "([B-E].)", , , 1, , , , , , "^t\1", 2
  22.         End With

  23.         For Each i In .Paragraphs
  24.             With i.Range
  25.                 If Not .Information(12) Then
  26.                     If .Text Like "A.*" Then
  27.                         With .Font
  28.                             If .ColorIndex = wdRed Then .ColorIndex = wdBlue Else .ColorIndex = wdRed
  29.                             .Bold = wdToggle
  30.                         End With

  31.                         If .Text Like "A.*B.*C.*D.*E.*" Then
  32.                             OptionNum = 5
  33.                             GoSub op
  34.                             With .ParagraphFormat.TabStops
  35.                                 .ClearAll
  36.                                 .Add Position:=CentimetersToPoints(Tab1)
  37.                                 .Add Position:=CentimetersToPoints(Tab2)
  38.                                 .Add Position:=CentimetersToPoints(Tab3)
  39.                                 .Add Position:=CentimetersToPoints(Tab4)
  40.                             End With

  41.                             'cut
  42.                             If .ComputeStatistics(1) = 2 Then
  43.                                 .Find.Execute "^t([CE].)", , , 1, , , , , , "^p\1", 2
  44.                             End If

  45.                         ElseIf .Text Like "A.*B.*C.*D.*" Then
  46.                             OptionNum = 4
  47.                             GoSub op
  48.                             With .ParagraphFormat.TabStops
  49.                                 .ClearAll
  50.                                 .Add Position:=CentimetersToPoints(Tab1)
  51.                                 .Add Position:=CentimetersToPoints(Tab2)
  52.                                 .Add Position:=CentimetersToPoints(Tab3)
  53.                             End With

  54. '                            'cut
  55.                             If .ComputeStatistics(1) = 2 Or .ComputeStatistics(1) = 3 Then
  56.                                 .Find.Execute "^t(C.)", , , 1, , , , , , "^p\1", 2
  57.                             ElseIf .ComputeStatistics(1) = 4 Then
  58.                                 .Find.Execute "^t([BD].)", , , 1, , , , , , "^p\1", 2
  59.                             End If
  60.                             'cuts
  61.                             If .ComputeStatistics(1) = 2 Then
  62.                                 With .ParagraphFormat.TabStops
  63.                                     .ClearAll
  64.                                     .Add Position:=CentimetersToPoints(Tab2)
  65.                                 End With
  66.                             ElseIf .ComputeStatistics(1) = 4 Then
  67.                                 .Find.Execute "^t([B-D].)", , , 1, , , , , , "^p\1", 2
  68.                             End If

  69.                         ElseIf .Text Like "A.*B.*C.*" Then
  70.                             OptionNum = 3
  71.                             GoSub op
  72.                             With .ParagraphFormat.TabStops
  73.                                 .ClearAll
  74.                                 .Add Position:=CentimetersToPoints(Tab1)
  75.                                 .Add Position:=CentimetersToPoints(Tab2)
  76.                             End With

  77.                             'cut
  78.                             If .ComputeStatistics(1) = 2 Then
  79.                                 .Find.Execute "^t([BC].)", , , 1, , , , , , "^p\1", 2
  80.                             End If

  81.                         ElseIf .Text Like "A.*B.*" Then
  82.                             OptionNum = 2
  83.                             GoSub op
  84.                             With .ParagraphFormat.TabStops
  85.                                 .ClearAll
  86.                                 .Add Position:=CentimetersToPoints(Tab1)
  87.                             End With
  88.                         End If

  89.                         With .ParagraphFormat
  90.                             .CharacterUnitFirstLineIndent = 0
  91.                             .FirstLineIndent = CentimetersToPoints(0)
  92.                             .CharacterUnitLeftIndent = 0
  93.                             .LeftIndent = CentimetersToPoints(0)
  94.                             .CharacterUnitRightIndent = 0
  95.                             .RightIndent = CentimetersToPoints(0)
  96.                             .CharacterUnitFirstLineIndent = 2
  97.                         End With
  98.                     End If
  99.                 End If
  100.             End With
  101.         Next
  102.     End With
  103.     Exit Sub

  104. op:
  105.     If OptionNum = 4 Then
  106.         oTab = (Int((TableWidth - 2 * 0.19) * 2.7 + 0.5) - 2) / 4
  107.         Tab1 = Round((2 + 1 * oTab) / 2.7, 2)
  108.         Tab2 = Round((2 + 2 * oTab) / 2.7, 2)
  109.         Tab3 = Round((2 + 3 * oTab) / 2.7, 2)

  110.     ElseIf OptionNum = 3 Then
  111.         oTab = (Int((TableWidth - 2 * 0.19) * 2.7 + 0.5) - 2) / 3
  112.         Tab1 = Round((2 + 1 * oTab) / 2.7, 2)
  113.         Tab2 = Round((2 + 2 * oTab) / 2.7, 2)

  114.     ElseIf OptionNum = 2 Then
  115.         oTab = (Int((TableWidth - 2 * 0.19) * 2.7 + 0.5) - 2) / 2
  116.         Tab1 = Round((2 + 1 * oTab) / 2.7, 2)

  117.     ElseIf OptionNum = 5 Then
  118.         oTab = (Int((TableWidth - 2 * 0.19) * 2.7 + 0.5) - 2) / 5
  119.         Tab1 = Round((2 + 1 * oTab) / 2.7, 2)
  120.         Tab2 = Round((2 + 2 * oTab) / 2.7, 2)
  121.         Tab3 = Round((2 + 3 * oTab) / 2.7, 2)
  122.         Tab4 = Round((2 + 4 * oTab) / 2.7, 2)
  123.     End If
  124.     Return
  125.     'cm=char/2.7
  126. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2023-2-19 15:30 | 显示全部楼层
测试了下,最后一个选项如果过长的话不能将每个选项单独成行

TA的精华主题

TA的得分主题

发表于 2023-4-28 12:18 | 显示全部楼层
你好,如果有图的题目,选项都弄成一个选项一段,代码应该怎么修改呢?

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-4-29 02:11 | 显示全部楼层
楼上朋友,你好!替换的话手动也可以,但有时测试时 Word 会崩溃;代码的好处是不易崩溃。
请试用下面的代码:
  1. Sub a0001_answer_replace_ABCD()
  2.     ActiveDocument.Content.Find.Execute "([!^13])([BCD][..、])", , , 1, , , , , , "\1^p\2", 2
  3. End Sub
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-4 16:38 , Processed in 0.044876 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表