2011-06-21 14 views
1

これはうまくいきませんが、テキストをOkに選択して変数に読み込む必要があります。Word VBA 2つの部分文字列間のテキストを選択し、変数に割り当てる方法は?

Sub findTest() 

Dim firstTerm As String 
Dim secondTerm As String 
Dim myRange As Range 
Dim selRange As Range 
Dim selectedText As String 

Set myRange = ActiveDocument.Range 
firstTerm = "<patientFirstname>" 
secondTerm = "</patientFirstname>" 
With myRange.Find 
.Text = firstTerm 
.MatchWholeWord = True 
.Execute 
myRange.Collapse direction:=wdCollapseEnd 
Set selRange = ActiveDocument.Range 
selRange.Start = myRange.End 
.Text = secondTerm 
.MatchWholeWord = True 
.Execute 
myRange.Collapse direction:=wdCollapseStart 
selRange.End = myRange.Start 
selectedText = selRange.Select 
End With 
End Sub 

小さな疑似XMLパケットからデータを抽出しようとしています。そのため、検索文字列は各単語文書で1回しか発生しません。

+0

(3年後) 'selRange.Select'から' selRange.Text'に変更してください! – dcromley

答えて

3

あなたの質問は、より重要な質問のカップルにつながる:

  • は、XML文書を解析していますか? (あなたの例に基づいていると仮定して)
  • WordのXML文書を解析していますか? (おそらくない良いアイデア)
しかし

、額面上の質問を取って、私はあなたがこれに似た文書があると仮定するつもりです:

<patientFirstname>Bob</patientFirstname> 
<patientFirstname>Sally</patientFirstname> 

をそして、あなたが抽出したいですすべての患者の名前。

あなたはあなたが投稿何よりも単純な次のコードを見つけることがあります。上記のコードはエラーを持っている

Sub findTest() 

    Dim firstTerm As String 
    Dim secondTerm As String 
    Dim myRange As Range 
    Dim documentText As String 

    Dim startPos As Long 'Stores the starting position of firstTerm 
    Dim stopPos As Long 'Stores the starting position of secondTerm based on first term's location 
    Dim nextPosition As Long 'The next position to search for the firstTerm 

    nextPosition = 1 

    'First and Second terms as defined by your example. Obviously, this will have to be more dynamic 
    'if you want to parse more than justpatientFirstname. 
    firstTerm = "<patientFirstname>" 
    secondTerm = "</patientFirstname>" 

    'Get all the document text and store it in a variable. 
    Set myRange = ActiveDocument.Range 
    'Maximum limit of a string is 2 billion characters. 
    'So, hopefully your document is not bigger than that. However, expect declining performance based on how big doucment is 
    documentText = myRange.Text 

    'Loop documentText till you can't find any more matching "terms" 
    Do Until nextPosition = 0 
     startPos = InStr(nextPosition, documentText, firstTerm, vbTextCompare) 
     stopPos = InStr(startPos, documentText, secondTerm, vbTextCompare) 
     Debug.Print Mid$(documentText, startPos + Len(firstTerm), stopPos - startPos - Len(secondTerm)) 
     nextPosition = InStr(stopPos, documentText, firstTerm, vbTextCompare) 
    Loop 

    MsgBox "I'm done" 

End Sub 
+0

ありがとうRay、それは私をそこに連れて来る。 – Saul

+0

@Saul yw。答えとして+ 1および/またはマークは可能ですか? – ray

+1

NP、ありがとう、私はそれがさらに素晴らしいソリューションを持っている場合には、もう少し時間がかかります! :-) – Saul

0

を - 次の行の最後の要素は、レン(firstTerm)と ないレン(secondTerm)でなければなりません以下のように:

Debug.Print Mid$(documentText, startPos + Len(firstTerm), stopPos - startPos - Len(firstTerm)) 

私は上記のコードを使用し、それが私に動作します - 私は次のように私の変数に文字列を割り当てる:

myString = Mid$(documentText, startPos + Len(firstTerm), stopPos - startPos - Len(firstTerm)) 

よろしくお願いいたします。

関連する問題