首页 > 解决方案 > 在非常大的 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 段并从中做出范围。在这一段中,有一个数据,我正在寻找。这是一个日期,但用波兰语写成一个月作为单词,因此很难使用单词通配符提取。

我有一些不变的词,在这一段中总是在同一个地方。我将它们分配给textToFind1textToFind2变量。

我想从本段中提取的字符串正好在这两个变量之间——可能开头有一个空格,结尾也有一个尾随空格,但它就在那里。

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 段中这些值之间的字符串。

标签: excelvbams-word

解决方案


根据您的描述,您不需要比以下更复杂的了:

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

推荐阅读