Excel VBA程序开发

蓝色仰望 Lv.2

关注
求助各位大神,word题库中提取题号和答案,然后将题库中的答案和下边的依据删除。原题库内容很多,有的题是有依据,有的没有。劳烦大神帮助,谢谢!题库在附件中,谢谢!

竞赛题库.zip   2022-8-31 15:49 上传

14.47 KB, 下载次数: 35

2936阅读
17回复 倒序

x-xx-xx Lv.4 2楼

https://club.excelhome.net/forum.php?mod=viewthread&tid=1629699&extra=&mobile=2

chxw68 Lv.7 3楼

  1. Sub test()
  2. '  Dim wordapp As Object
  3. '  Dim mydoc As Object
  4. '  Set wordapp = CreateObject("word.application")
  5. '  Set mydoc = CreateObject("word.document")
  6.   Dim wordapp As New Word.Application
  7.   Dim mydoc As New Word.Document
  8.   Dim r%, i%
  9.   Dim myapth$, myname$
  10.   Dim reg1 As New regexp
  11.   Dim reg2 As New regexp
  12.   Dim brr()
  13.   Application.DisplayAlerts = False
  14.   Application.ScreenUpdating = False
  15.   With reg1
  16.     .Global = False
  17. '    .Pattern = "[\((](\s*[A-D])\s*[\))]"
  18.     .Pattern = "^(\d+)\.(.+)$"
  19.   End With
  20.   With reg2
  21.     .Global = True
  22.     .Pattern = "([A-D])\.(.*?)(?=[A-D]|$)"
  23.   End With
  24.   
  25.   Filename = Application.GetOpenFilename(filefilter:="Excel 文件 (*.doc;*.docx),*.doc;*.docx", MultiSelect:=False)
  26.   If VarType(Filename) = vbBoolean Then
  27.     MsgBox "没有选择源文件!"
  28.     wordapp.Quit
  29.     Exit Sub
  30.   End If
  31.   Set mydoc = wordapp.Documents.Open(Filename)
  32. '  Set mydoc = GetObject(Filename)
  33.   wordapp.Visible = True
  34.   With mydoc
  35.     ReDim brr(1 To .Paragraphs.Count, 1 To 6)
  36.     m = 0
  37.     For i = 1 To .Paragraphs.Count
  38.       ss = .Paragraphs(i).Range.listformat.liststring & .Paragraphs(i).Range.Text
  39.       ss = Left(ss, Len(ss) - 1)
  40.       If Len(ss) <> 0 Then
  41.         If reg1.test(ss) Then
  42.           Set mh1 = reg1.Execute(ss)
  43.           m = m + 1
  44.           brr(m, 1) = mh1(0).SubMatches(0)
  45.           brr(m, 2) = mh1(0).SubMatches(1)
  46. '          brr(m, 1) = reg1.Replace(ss, "(" & Space(5) & ")")
  47. '          brr(m, 6) = mh1(0).SubMatches(0)
  48.         Else
  49.           Set mh2 = reg2.Execute(ss)
  50.           If mh2.Count > 0 Then
  51.             For j = 0 To mh2.Count - 1
  52.               n = Asc(mh2(j).SubMatches(0)) - 62
  53.               brr(m, n) = Trim(mh2(j).SubMatches(1))
  54.             Next
  55.           End If
  56.         End If
  57.       End If
  58.     Next
  59.     .Close
  60.   End With
  61.   wordapp.Quit
  62.   With Worksheets("sheet1")
  63.     .UsedRange.Offset(1, 0).ClearContents
  64.     .Cells.NumberFormatLocal = "@"
  65.     If m > 0 Then
  66.       With .Range("a2").Resize(m, UBound(brr, 2))
  67.         .Value = brr
  68.         .Borders.LineStyle = xlContinuous
  69.       End With
  70.     End If
  71.   End With
  72.   Application.ScreenUpdating = True
  73.   MsgBox "题目导入完毕!"
  74. End Sub


chxw68 Lv.7 4楼

详见附件。

新建文件夹 (2).rar   2022-8-31 20:18 上传

35.96 KB, 下载次数: 22

天道酬勤y Lv.3 5楼

正想找个这样的,你是删掉word里面的答案?还是提取到Excel里面删除

蓝色仰望 楼主 6楼

引用: chxw68 发表于 2022-8-31 20:18
详见附件。

先谢谢您,我主要想将题干和答案分开,如何将答案从题干中提取,再将题干中的有答案的部分变为空,烦请再给看看,以下面图的形式为准,另外多选有时候会有五个选项,应该如何修改呢?麻烦了
屏幕截图 2022-08-31 231241.png

蓝色仰望 楼主 7楼

引用: 天道酬勤y 发表于 2022-8-31 22:22
正想找个这样的,你是删掉word里面的答案?还是提取到Excel里面删除

提取到excel里,题干中答案删除,另起一列标注答案

天道酬勤y Lv.3 8楼

引用: chxw68 发表于 2022-8-31 20:18
详见附件。

chxw68 大师 可以把答案单独提取成一列吗

chxw68 Lv.7 9楼

修改好了。没有时间认真测试。

新建文件夹 (2).rar   2022-9-1 14:32 上传

36.46 KB, 下载次数: 15

pc087 Lv.3 10楼


代码在VBA文档里面
image.png

1竞赛题库 - 副本.rar   2022-9-1 15:30 上传

21.95 KB, 下载次数: 17

加载更多