Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用Excel VBA中的正则表达式仅从Word文档中提取第一个匹配项_Regex_Vba_Ms Word - Fatal编程技术网

Regex 使用Excel VBA中的正则表达式仅从Word文档中提取第一个匹配项

Regex 使用Excel VBA中的正则表达式仅从Word文档中提取第一个匹配项,regex,vba,ms-word,Regex,Vba,Ms Word,有一份像这样的文件。我每天处理20个这样的文档,它们看起来都是相同的结构,我的意思是,非常一致 此宏的目标是仅从.ActiveDocument.Content中提取正则表达式模式的第一个匹配项。在整个文档中有很多匹配项,但我只需要第一个。正在处理的文档将在宏运行之前手动打开 我只是一个VBA初学者,所以如果有可能不用数组、集合或一些字典来编写它,我会非常感激。只有一项要提取,因此最好将其加载到repNmbr字符串变量中,然后从中仅加载ws.range30.Value=repNmbr。越简单越好

有一份像这样的文件。我每天处理20个这样的文档,它们看起来都是相同的结构,我的意思是,非常一致

此宏的目标是仅从.ActiveDocument.Content中提取正则表达式模式的第一个匹配项。在整个文档中有很多匹配项,但我只需要第一个。正在处理的文档将在宏运行之前手动打开

我只是一个VBA初学者,所以如果有可能不用数组、集合或一些字典来编写它,我会非常感激。只有一项要提取,因此最好将其加载到repNmbr字符串变量中,然后从中仅加载ws.range30.Value=repNmbr。越简单越好

我使用了这些非常有用的资源,但我仍然不知道如何将第一个匹配单独加载到repNmbr字符串变量中。我希望在不使用任何循环的情况下执行此操作,因为我只想将单个字符串加载到此repNmbr变量中

目前我有如下代码:

Sub ExtractRepertor03()
    'Application.ScreenUpdating = False
    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Dim ExcelApp As Excel.Application
    Dim rng As Word.Range
    Dim ws As Worksheet
    Dim regEx As Object
    Dim matches As MatchCollection
    Dim match As String
    Dim repNmbr As String

    'Assigning object variables
    Set WordApp = GetObject(, "Word.Application")      'ActiveX can't create object is when
    Set ExcelApp = GetObject(, "Excel.Application")    'there is no Word document open;
    Set regEx = CreateObject("VBScript.RegExp")
    Set WordDoc = WordApp.ActiveDocument
    Set rng = WordApp.ActiveDocument.Content

    'Create the regular expression object
    regEx.Global = False    'because I need only the first match instead of all occurences;
    regEx.IgnoreCase = True
    regEx.Pattern = "([0-9]{1,5})([ ]{0,4})([/])([0-9]{4})"
    'regEx.Pattern = "([0-9]{1,5})([\s]{0,4})(/[0-9]{4})"

    repNmbr = regEx.Execute(rng.text)      'here is something wrong but I don't know what;
                            'I'm trying to assign the first RegEx match to repNmbr variable;
    Debug.Print repNmbr
    repNmbr = Replace(repNmbr, " ", "") 
'    Set matches = regEx.Execute(rng.text)
'    Debug.Print regEx.Test(rng)
'    'Debug.Print regEx.Value
'        For Each match In matches    'I just want this macro run without the loop
'           Debug.Print match.Value   'Result: 9042 /2019
'           repNmbr = match.Value
'        Next match

    ExcelApp.Application.Visible = True
    ws.Range("G30").Value = repNmbr
End Sub
还有这样一个错误:

Sub ExtractRepertor03()
    'Application.ScreenUpdating = False
    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Dim ExcelApp As Excel.Application
    Dim rng As Word.Range
    Dim ws As Worksheet
    Dim regEx As Object
    Dim matches As MatchCollection
    Dim match As String
    Dim repNmbr As String

    'Assigning object variables
    Set WordApp = GetObject(, "Word.Application")      'ActiveX can't create object is when
    Set ExcelApp = GetObject(, "Excel.Application")    'there is no Word document open;
    Set regEx = CreateObject("VBScript.RegExp")
    Set WordDoc = WordApp.ActiveDocument
    Set rng = WordApp.ActiveDocument.Content

    'Create the regular expression object
    regEx.Global = False    'because I need only the first match instead of all occurences;
    regEx.IgnoreCase = True
    regEx.Pattern = "([0-9]{1,5})([ ]{0,4})([/])([0-9]{4})"
    'regEx.Pattern = "([0-9]{1,5})([\s]{0,4})(/[0-9]{4})"

    repNmbr = regEx.Execute(rng.text)      'here is something wrong but I don't know what;
                            'I'm trying to assign the first RegEx match to repNmbr variable;
    Debug.Print repNmbr
    repNmbr = Replace(repNmbr, " ", "") 
'    Set matches = regEx.Execute(rng.text)
'    Debug.Print regEx.Test(rng)
'    'Debug.Print regEx.Value
'        For Each match In matches    'I just want this macro run without the loop
'           Debug.Print match.Value   'Result: 9042 /2019
'           repNmbr = match.Value
'        Next match

    ExcelApp.Application.Visible = True
    ws.Range("G30").Value = repNmbr
End Sub
有人能解释一下为什么Set matches=regEx.executeng.text可以正常工作,但是 repNmbr=regEx.executeng.text返回错误:参数数目错误或属性赋值无效


设置regEx.Global=False后,regEx只会找到一个值,因此为什么VBA拒绝将此字符串分配到repNmbr字符串变量中???

正如我在您的另一个问题中所说,您不需要regEx库来实现此目的。坚持使用Word的通配符!尝试:


因为这一切都是多余的——即使是您自己的代码也从未将任何内容分配给ws

尝试使用Set repNmbr=regEx.executeng.text,但在以以下方式声明它之后:Dim repNmbr As MatchCollection。第一个事件是repNmbr1…为了你的运气,我是波兰人,我警告你:这包含非常敏感的个人数据,你可能会因为那样发布而被起诉。试着找出一个示例来说明您的问题。第一:不要试图使用正则表达式从Word中提取信息。使用Word的内置查找功能。要获得基本语法,首先以最终用户Ctrl+H的形式尝试Find,然后选择Find选项卡,单击More查看所有可能的选项。仔细看一看通配符,它是Word的内置注册表项。如果您在计算查找术语时遇到困难,请在最终用户场所(如超级用户)询问。然后录制一个宏以获取VBA代码所需的基本语法。若要获取第一个匹配项,请将Forward属性设置为True(这是默认值)。它必须作为MatchCollection进行调整,因为此变量类型是由讨论中的函数返回的。即使只有一次。或者您可以使用ai推荐的第二种方式:repNmbr=regEx.executeng.text 0。这意味着返回集合的第一个元素。。。如果你在这里向某人写信,建议在我的情况下使用@FaneDuru。你可以看到我们的评论,因为你是问这个问题的人…我想在收到通知时提及。。。
Dim ExcelApp As Excel.Application
Dim rng As Word.Range
Dim ws As Worksheet
Dim regEx As Object
Dim matches As MatchCollection
Dim match As String
Dim repNmbr As String