2016-06-20 13 views
0

私は電子メールでレポートを送信するためのExcel VBAスクリプトを作成しています。添付ファイルを電子メールに添付する前に、添付ファイルを検証するために次の機能を使用しました。Word文書から行を読む

以下の関数は、ドキュメントという単語を開き、最初の行が顧客IDと一致するかどうかを確認してboolを返します。

しかし、Wordからデータを読み込むと、テキストに隠し引用符が含まれています。

両方の文字列は123aですが、別のテキストエディタに貼り付けると、Wordから "123a"という文字列が読み込まれます。 MsbBoxを使って印刷すると、両方とも123aに等しくなります。

Function ValidateAttachment(attachmentURL As String, customerID As String) As Boolean 

    Dim oWord As Word.Application 
    Dim oWdoc As Word.Document 

    Set oWord = CreateObject("Word.Application") 

    Set oWdoc = oWord.Documents.Open(attachmentURL) 

    If StrComp(oWdoc.Paragraphs(1).Range.Text, customerID, vbTextCompare) = 0 Then 
     ValidateAttachment = True 
    Else 
     ValidateAttachment = False 
    End If 

    oWord.Quit 

    Set oWord = Nothing 

    Exit Function 
End Function 

これは、両方の結果を通常のセルに書き込むときに表示されます。私は平等をチェックするために簡単な式を作っても、うまくいきません。

enter image description here

+0

を、私は困惑です。最初にoWdoc.Paragraphs(1).Range.Textを変数に代入して比較を行います。 –

+0

同じ出力で、これをもっと奇妙にするのは、ブレークポイントを挿入して変数をホバーすると、正しい値が表示されます。 – Marcelo

+0

oWdoc.Paragraphs(1).Range.Text = customerIDの場合はどうですか。 –

答えて

0

私は目に見えない引用符に対処する方法を発見しました。

Application.WorksheetFunction.Clean() 

そしてここでは、最終的なコードです::これを使用して

はトリックでした暗闇での撮影

Function ValidateAttachment(attachmentURL As String, customerID As String) As Boolean 

    Dim oWord As Word.Application 
    Dim oWdoc As Word.Document 

    Set oWord = CreateObject("Word.Application") 

    Set oWdoc = oWord.Documents.Open(attachmentURL) 

    Dim x As String 
    x = "123a" 

    Application.Sheets(1).Columns(3).Rows(2) = Replace(oWdoc.Paragraphs(1).Range.Text, Chr(34), "") 

    If StrComp(Application.WorksheetFunction.Clean(oWdoc.Paragraphs(1).Range.Text), customerID, vbTextCompare) = 0 Then 
     ValidateAttachment = True 
    Else 
     ValidateAttachment = False 
    End If 

    oWord.Quit 

    Set oWord = Nothing 

    Exit Function 
End Function 
0

試してみてください。

If StrComp(Replace(oWdoc.Paragraphs(1).Range.Text,chr(34),""), customerID, vbTextCompare) = 0 Then 
+0

メモ帳に結果を貼り付けて同じ結果を出力123a "123a " ExcelとMsgBoxが123a – Marcelo

関連する問題