excel - 在非常大的 word 文档中创建非常小的范围
问题描述
我在这里粘贴了较大代码的中间部分,以便你们可以看到它的较大部分,但是给我一些问题的行在第二部分,如下所示Set wordRng2 = ActiveDocument.Paragraphs(parNmbr).Range(Start:=startPos, End:=endPos)
:
有一个非常大的文件,大约有 200 段。因此,使用wordRng.SetRange Start:=ActiveDocument.Paragraphs(parNmbr).Range.Start, End:=ActiveDocument.Paragraphs(parNmbr).Range.End
我正在提取第 23 段并从中做出范围。在这一段中,有一个数据,我正在寻找。这是一个日期,但用波兰语写成一个月作为单词,因此很难使用单词通配符提取。
我有一些不变的词,在这一段中总是在同一个地方。我将它们分配给textToFind1
和textToFind2
变量。
我想从本段中提取的字符串正好在这两个变量之间——可能开头有一个空格,结尾也有一个尾随空格,但它就在那里。
With wordRng.Find
.Text = "<[A-Z]{3} [0-9]{6}>" 'you can use the tags < and > to mark the start and end of a word, respectively.
.MatchWildcards = True 'these wildcards refer to polish ID number that is: ABC 123456 or BCF 765432 - it works fine;
.MatchCase = False 'this part of the code works fine;
.Wrap = wdFindStop
.Forward = True
.Execute
If .Found = True Then
idNmbr = wordRng
idNmbr = Trim$(idNmbr)
intCol = Application.WorksheetFunction.match("nr_dokumentu", Worksheets("data").Range("1:1"), 0) 'Range("1:1") is row 1.
mySheet.Cells(lngRow, intCol) = idNmbr 'lngRow = lngRow + 1.
End If
End With
'InStr function returns a Variant (Long) specifying the position of the first occurrence of one string within another.
wordRng.SetRange Start:=ActiveDocument.Paragraphs(parNmbr).Range.Start, End:=ActiveDocument.Paragraphs(parNmbr).Range.End
Debug.Print wordRng
startPos = InStr(1, wordRng, textToFind1) - 1 'here we get 217, we're looking 4 id validity date;
endPos = InStr(startPos, wordRng, textToFind2) - 1 'here we get 247, we're looking 4 id validity date;
Set wordRng2 = ActiveDocument.Paragraphs(parNmbr).Range(Start:=startPos, End:=endPos) 'Set myRange = ActiveDocument.Range(Start:=pos, End:=pos2)
'Wrong number of arguments or invalid property assignment error.
'at the moment "wordRng2" variable holds long date with month written as a word = października, listopada, grudnia;
Debug.Print wordRng
Debug.Print wordRng2
wordRng2 = Trim$(wordRng2)
Debug.Print wordRng2
我设置了第二个范围变量,它应该准确地保存我需要提取的字符串,最后可能有一个尾随空格。但是我用来做的那一行:Set wordRng2 = ActiveDocument.Paragraphs(parNmbr).Range(Start:=startPos, End:=endPos)
返回“参数数量错误或属性分配无效”错误。
在我正在尝试的示例中,它应该有 18 或 19 个字符。
即使我将此错误的行更改为:
Set wordRng2 = ActiveDocument.Paragraphs(parNmbr).Range
wordRng2.SetRange Start:=startPos, End:=endPos
我仍然从整个文档的开头得到 217 和 247 位置之间的字符串,而不是第 23 段中这些值之间的字符串。
解决方案
根据您的描述,您不需要比以下更复杂的了:
With wordapp.ActiveDocument.Paragraphs(23).Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "<[0-9]{1,2} [cgklmpsw][airtuwz][eijprstuyzź]*> [0-9]{4}>"
.Replacement.Text = ""
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
If .Find.Found = True Then MsgBox .Text
End With
推荐阅读
- flutter - 如何从滑块设置颜色?
- php - 登录后自动重定向,除了 WooCommerce 结帐页面
- c# - 无法使用 C# 删除 cookie
- powerbi - 我无法打开我的 PBIX 文件,ExecuteXmla 结果失败
- google-sheets - 如何引用谷歌表格中的字段?
- android - 使用 Android Studio Artic Fox 安装 MapBox
- asp.net - 警告:Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[59]
- python - 我在 while 循环中寻找报告给相同变量的命令。调用时变量不会改变
- next.js - 如何在 Nextjs 中渲染任何内容之前初始化路由器?
- python - 如何确定 dask.distributed.cluster 工作人员的最大数量