ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 【求助】查找WORD中特定字符串前后的字符并保存

[复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-2-16 23:47 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
weiyingde 发表于 2020-2-16 22:17
你这个例子看不出效果,因为四个都不相同,换个例子或许可行。思路是第一次将找到的各项装入字典,
第二 ...

可能我的意图还是说的不够清楚, 是文章中会正文会有Aubin\upcite{A78},则后文会有A78的人员介绍\bibitem{A78} Aubin,我是想核对前文中的Aubin是不是写正确了,然后对每个正文核对后进行颜色标注;

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-2-16 23:48 | 显示全部楼层
weiyingde 发表于 2020-2-16 22:17
你这个例子看不出效果,因为四个都不相同,换个例子或许可行。思路是第一次将找到的各项装入字典,
第二 ...

或者受累能不能给个有关类似装入字典再索引的例子

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-2-17 14:27 | 显示全部楼层
weiyingde 发表于 2020-2-16 22:17
你这个例子看不出效果,因为四个都不相同,换个例子或许可行。思路是第一次将找到的各项装入字典,
第二 ...

老师,我按照您的思路,自己写了一个,个人认为循环和正则都没问题了,但是最后选中标记的正则时有问题,老是选中整个WORD,望赐教指导

第二个正则后的if语句中,.Content.HighlightColorIndex = 5有点问题,不是匹配的该正则匹配项,而是全文

TEST2.rar

19.52 KB, 下载次数: 1

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-2-18 10:23 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
weiyingde 发表于 2020-2-14 15:02
重复的要不要算?

老师,我把我的代码传上去了,基本能实现,但是有点错误,有空给指导一下,谢谢   

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-2-19 14:27 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
每天顶一下,不要沉,其他老师有空再给指导一下

TA的精华主题

TA的得分主题

发表于 2020-2-19 19:41 | 显示全部楼层
本帖最后由 weiyingde 于 2020-2-19 19:57 编辑
fypm1009 发表于 2020-2-19 14:27
每天顶一下,不要沉,其他老师有空再给指导一下

'你的代码思路不清,我再写了一个,你用我的例子测试。
Sub 再测试()
On Error Resume Next
Dim colr As Integer
Set dic1 = CreateObject("scripting.dictionary")
Set dic2 = CreateObject("scripting.dictionary")
Set RegEx = CreateObject("VBscript.RegExp")
    RegEx.Pattern = "([A-Za-z]+)(\s\\upcite\{)([A-Z1-9]+)"
    RegEx.Global = True
    RegEx.MultiLine = True
    With ActiveDocument
         sr = .Content.Text
         Set mh = RegEx.Execute(sr)
         For Each mt In mh
             If dic1.Exists(mt.submatches(0)) = False Then
                N = N + 1
                Nubr1 = IIf(N > 8, N Mod 8, N)
                dic1.Add mt.submatches(0), N * 2 + 1
             End If
             If dic2.Exists(mt.submatches(2)) = False Then
                M = M + 1
                Nubr2 = IIf(M > 8, M Mod 8, M)
                dic2.Add mt.submatches(2), M * 2
              End If
         Next
         ky1 = dic1.keys: tm1 = dic1.items
         KY2 = dic2.keys: tm2 = dic2.items
         For Each mt In mh
             fst = mt.firstindex
             lgh0 = Len(mt.submatches(0))
             lgh1 = Len(mt.submatches(1))
             lgh2 = Len(mt.submatches(2))
             For x1 = 0 To UBound(ky1)
                 If mt.submatches(0) = ky1(x1) Then
                    .Range(.Range.Start + fst, .Range.Start + fst + lgh0).Font.ColorIndex = tm1(x1)
                 End If
             Next
             For x2 = 0 To UBound(KY2)
                 If mt.submatches(2) = KY2(x2) Then
                    .Range(.Range.Start + fst + lgh0 + lgh1, .Range.Start + fst + lgh0 + lgh1 + lgh2).Font.ColorIndex = tm2(x2)
                 End If
             Next
         Next
    End With
End Sub

相同项颜色相同.rar

23.38 KB, 下载次数: 6

评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2020-2-19 19:53 | 显示全部楼层
weiyingde 发表于 2020-2-19 19:41
'你的代码思路不清,我再写了一个,但你示例不典型,可能无法看到效果
Sub 再测试()
On Error Resume N ...

大佬,完善成一个自定义函数,这样通用性更强呀,也可重复利用~~您说是吧

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2020-2-19 19:58 | 显示全部楼层
本帖最后由 weiyingde 于 2020-2-19 20:00 编辑
FOB_FN_L 发表于 2020-2-19 19:53
大佬,完善成一个自定义函数,这样通用性更强呀,也可重复利用~~您说是吧

你用我上传的附件测试。不是自定义函数,而是公共过程。这个不难,简单改造一下就成了。

TA的精华主题

TA的得分主题

发表于 2020-2-19 19:58 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Get strings on either side of special character.

'Here is a function that will get the string on each side of
'a string that is really two strings, with a special char in
'the center.



Function GetString(tmpStr As String, tmpDiv As String, Mode As String) As
String
Dim tmpRetStr As String
Dim tmpLen As Integer
Dim tmpErr As Integer
  '
  ' tmpString = The whole string
  ' tmpDiv = The Divider chr
  ' if mode = "F" then get the string in front of the div
  ' if mode = "B" then get the string in back of the div
  '
  ' Return values:
  '  Errors
  '    Err1 = wrong mode ("F" & "B" ok)
  '    Err2 =  No Div, did not found a divider
  '    Err3 = No F, did not find any string in front of the div
  '    Err4 = No B, did not find any string in back of the div
  '    Err5 = No String
  '  Normal
  '    String
  '
  ' Example: GetString("Red=Green","=","F") will retrun "Red"
  ' *** for string
  tmpErr = 0
  If Len(tmpStr) = 0 Then
    tmpErr = 5
    GoTo GetStringErr
  End If
  
  ' *** test for chr in tmpDiv
  If Len(tmpDiv) = 0 Then
    tmpErr = 2
    GoTo GetStringErr
  End If
  
  ' *** test for div in string
  If InStr(1, tmpStr, tmpDiv) = 0 Then
    tmpErr = 2
    GoTo GetStringErr
  End If
  
  ' *** process "F"
  If Mode = "F" Then
     tmpRetStr = Left(tmpStr, (InStr(1, tmpStr, tmpDiv) - 1))
     If Len(tmpRetStr) > 0 Then
       GetString = tmpRetStr
       Exit Function
     Else
       tmpErr = 3
       GoTo GetStringErr
     End If
  End If
  
  If Mode = "B" Then
     tmpRetStr = Right(tmpStr, Len(tmpStr) - (InStr(1, tmpStr, tmpDiv)))
     If Len(tmpRetStr) > 0 Then
       GetString = tmpRetStr
       Exit Function
     Else
       tmpErr = 4
       GoTo GetStringErr
     End If
  
  End If
    tmpErr = 1
    GoTo GetStringErr
  Exit Function
GetStringErr:
GetString = "Err" & tmpErr
End Function


VB中截取特定字符两边的一个自定义函数,完善一下就能成通用的了

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-2-19 20:08 | 显示全部楼层
FOB_FN_L 发表于 2020-2-19 19:58
Get strings on either side of special character.

'Here is a function that will get the string on  ...

先谢谢,我是小白,感谢各位帮助,先谢过,再学习
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-6-2 18:20 , Processed in 0.048174 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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