2017-11-02 11 views
0

word2016の変数を含むテキストボックスを使用すると問題が発生します。テキストボックス内の変数を見つける

テキストボックスのテキスト内に存在する可能性のある変数を検索したい場合があります。また、特定のインスタンスで新しい変数に置き換えたい場合もあります。たとえば、テキストボックスは以下の通りです:私は「によって確認された」DOCPROPERTYを検索したい

enter image description here

。このDOCPROPERTYはテキストボックスには存在しませんが、 "Checked By"というテキストはありますが、下に添付されているコードではそれは無関係です。それはDOCPROPERTYではないのでtrueを返すべきではありません。

' ************************************************************ 
' ********* finding docproperties in text, headers and textboxes 
' ************************************************************** 
Public Function findProperty(doc As Document, findText As String) As Boolean 
    Dim rngStory As word.Range 
    Dim oFld As word.Field 
    Dim objShape As Shape 
    Dim temp As String 
    Dim temp2() As String 
    Dim element As Variant 

    ActiveWindow.View.ShowFieldCodes = True 
    If findText = "_DocumentTitle" Then 
     findProperty = True 
     Exit Function 
    End If 

    findProperty = False 


    For Each objShape In ActiveDocument.Shapes 
     If objShape.Type = msoTextBox Then 
      'do the required action 
      temp2 = Split(objShape.TextFrame.TextRange.Text, "DOCPROPERTY") 
      For Each element In temp2 
       temp = replace(element, "DOCPROPERTY", "") 
       temp = replace(temp, "\* MERGEFORMAT", "") 
       temp = replace(temp, """", "") 
       If InStr(UCase(temp), Trim(UCase(findText))) > 0 Then 
        findProperty = True 
        Exit Function 
       End If 
      Next 

     End If 
    Next objShape 

    For Each rngStory In doc.StoryRanges 
     Do 
     For Each oFld In rngStory.Fields 
      'If oFld.Type = wdFieldDocProperty Then 
       'Dig a little deeper and see what the field code contains. 
       'Formatting of property is a pain.... 
       temp = replace(oFld.Code.Text, "DOCPROPERTY", "") 
       temp = replace(temp, "\* MERGEFORMAT", "") 
       temp = replace(temp, """", "") 
       If Trim(UCase(temp)) = Trim(UCase(findText)) Then 
       findProperty = True 
       Exit Function 
       End If 

     Next oFld 
     Set rngStory = rngStory.NextStoryRange 
     Loop Until rngStory Is Nothing 
    Next rngStory 
    ActiveWindow.View.ShowFieldCodes = False 
End Function 

答えて

0

あなたは区切り文字として"DOCPROPERTY"を使用して、配列に文字列を分割しているので、vbNullString、(「」)で「DOCPROPERTY」を交換する必要はありません。

 temp2 = Split(objShape.TextFrame.TextRange.Text, "DOCPROPERTY") 
     For Each element In temp2 
      temp = replace(element, "DOCPROPERTY", "") 

私はそれが0より大きい場合、それは0で、その後何"DOCPROPERTY" FOUNDがなかったならば、見つかった"DOCPROPERTY"が、ありました、あなたはtemp2の大きさをテストすることができると思います。 @SlowLearnerからの提案で

if UBound(temp2) > 0 then findProperty = True 

しかし、おそらくより良い方法は、(N)フィールドで、フィールドコードのテキストを返し、その後、あなたが持っている正確に何を知っている.CODEを見ることです...

0

されます以下に示す作業コードです。

Public Function findProperty(doc As Document, findText As String) As Boolean 
    Dim rngStory As word.Range 
    Dim oFld As word.Field 
    Dim objShape As Shape 
    Dim element As Variant 

    findProperty = False 'default false 
    '************************************************************************************** 
    '**************** ALL DOCUMENTS NEED A TITLE EVEN IF ITS NOT IN USE ******************* 
    '************************************************************************************** 
    If findText = "_DocumentTitle" Then 
     findProperty = True 
     Exit Function 
    End If 


    For Each objShape In ActiveDocument.Shapes 
     If objShape.Type = msoTextBox Then 
      'do the required action 
      For Each element In objShape.TextFrame.TextRange.Fields 
       If InStr(UCase(element.Code.Text), Trim(UCase(findText))) > 0 Then 
        findProperty = True 
        Exit Function 
       End If 
      Next element 
     End If 
    Next objShape 

    For Each rngStory In doc.StoryRanges 
     Do 
     For Each oFld In rngStory.Fields 
       'Dig a little deeper and see what the field code contains. 
       If InStr(UCase(oFld.Code.Text), Trim(UCase(findText))) > 0 Then 
       findProperty = True 
       Exit Function 
       End If 

     Next oFld 
     Set rngStory = rngStory.NextStoryRange 
     Loop Until rngStory Is Nothing 
    Next rngStory 

End Function 
+0

喜んで助けてください。すべてが機能している場合は、その答えを解決策として受け入れてください(または詳細情報をお尋ねください) – SlowLearner

関連する問題