2014-04-30 2 views
2

내 문제를 인식하지 않습니다VBA 말씀 - Document.Words은 ​​ID

Sub findIDs() 

    Dim oDoc As Document 
    Dim obj_RegEx As Object 
    Dim nextID As String 

    Set oDoc = ActiveDocument 
    Set obj_RegEx = CreateObject("VbScript.RegExp") 
    obj_RegEx.Pattern = "RS.[0-9]{4;4}/[0-9]{2;2}.[0-9]{5;5}" 

    For i = 1 To oDoc.Words.Count 
     If obj_RegEx.Test(oDoc.Words.Item(i).Text) = True Then 
      Debug.Print "Found: " & oDoc.Words.Item(i).Text 
     Else 
      Debug.Print "Not Found: " & oDoc.Words.Item(i).Text 
     End If 
    Next 

End Sub 
: 나는 같은 "RS.1234/56.78910"

내 코드 인 Word 문서에서 ID에 대해보고 싶어요

내 문제 : 정규식은 잘 될 것 같다하지만 Words.Count/Words.item가 추천 단어로 ID를 분할 이럴 "Document.Range.Find"를 사용하지 않는 이유는

RS.1234/56.78910" -> "RS" "." "1234" "/" "56" "." "78910" 

? -> 단어를 변수에 저장하고 가장 높은 ID를 찾고이를 어떻게 할 것인지 모르겠다. range.find

+0

어떻게 당신이 그것을 분할하고 싶어해야 하는가? –

+0

전혀 분할하고 싶지 않습니다. -> 정규 표현식으로 ID를 찾으려고하지만 document.words는 ID를 7 개의 문자열로 나눕니다. 정규 표현식에서는 아무 것도 찾지 못합니다. ( – user3588414

답변

0

단어 대신 전체 문서 텍스트를 검토해야합니다.

또 다른 것은, 당신의 정규식이 작동하지 않습니다 당신이 점을 탈출해야하고, {4,4}{4}

Sub findIDs() 

    Dim oDoc As Document 
    Dim obj_RegEx As Object 
    Dim nextID As String 
    Dim result As Variant 
    Dim id As Variant 

    Set oDoc = ActiveDocument 
    Set obj_RegEx = CreateObject("VbScript.RegExp") 
    obj_RegEx.Global = True 'match all occurrences 
    obj_RegEx.Pattern = "RS\.[0-9]{4}/[0-9]{2}\.[0-9]{5}" 

    Set result = obj_RegEx.Execute(oDoc.Content.Text) 
    For Each id In result 
      Debug.Print "Found: " & id.value 
    Next 

End Sub 
+0

감사합니다! – user3588414