2017-10-12 12 views
0

を見つけ、交換した場合、私は次の操作を実行したい:VBAワード:次に、このコードで

  • 単語は、文字Mを終了した場合には単語がで終わる場合、文字N.
  • と交換手紙N、手紙Mで置き換えてください。
  • 私はIF - Then文をよく使っていません。 ご協力いただければ幸いです。

    Sub find_end() 
        Selection.Find.ClearFormatting 
        Selection.Find.Replacement.ClearFormatting 
        With Selection.Find 
         .Text = "[nm]>" 
         .Replacement.Text = "" 
         .Forward = True 
         .Wrap = wdFindContinue 
         .MatchWildcards = True 
         Selection.Find.Execute 
         With Selection 
         If Selection.Find.Found = n Then 
          Selection.TypeText Text:=m 
         ElseIf Selection.Find.Found = m Then 
          Selection.TypeText Text:=n 
         End If 
    End Sub 
    
+0

あなたのコードでは、2つの「で終わる」を欠落している(いくつかの修正を必要とする;など=「M」と=「n」を、周りの引用しかし、あなたのコードは、すべての出現スルーループはしていません、次のを見てみましょう。すべての出現を見つける方法の例として、If Selection.Text = "n" "に変更した場合はifが機能しますが、true/falseを返す" Selection.Find.Found "があります。 –

答えて

0

私はで見つけコード変更:Repeating Microsoft Word VBA until no search results found をし、それが文書を通してスピンし、各単語の最後の文字を(それは「M」または「n」のの場合)に置き換えられます。注:2000 m以上の可能性がある場合は、削除する可能性のあるコードにループチェックがあります。

Option Explicit 

' The following code adapted from: https://stackoverflow.com/questions/13465709/repeating-microsoft-word-vba-until-no-search-results-found 
Sub SearchFN() 
    Dim iCount As Integer 
    Dim lStart As Long 
    'Always start at the top of the document 
    Selection.HomeKey Unit:=wdStory 

    'find a footnote to kick it off 
    With Selection.Find 
     .ClearFormatting 
     .Text = "[nm]>" 
     .Replacement.Text = "" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchKashida = False 
     .MatchDiacritics = False 
     .MatchAlefHamza = False 
     .MatchControl = False 
     .MatchByte = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchFuzzy = False 
     .MatchWildcards = True 
     .Execute 
    End With 

    'Jump back to the start of the document. 
    Selection.HomeKey Unit:=wdStory 

    'If we find one then we can set off a loop to keep checking 
    'I always put a counter in to avoid endless loops for one reason or another 
    Do While Selection.Find.Found = True And iCount < 2000 
     iCount = iCount + 1 

     Selection.Find.Execute 

     'On the last loop you'll not find a result so check here 
     If Selection.Find.Found Then 
      ' Exit if we start back at the beginning 
      If Selection.Start < lStart Then 
       Exit Do 
      End If 

      'Reset the find parameters 
      With Selection.Find 
       .ClearFormatting 
       .Text = "[nm]>" 
       If Selection.Text = "m" Then 
        Debug.Print "found 'm' at position: " & Selection.Start 
        Selection.Text = "n" 
       ElseIf Selection.Text = "n" Then 
        Debug.Print "found 'n' at position: " & Selection.Start 
        Selection.Text = "m" 
       End If 
       lStart = Selection.Start 
'    .Replacement.Text = "" 
       .Forward = True 
       .Wrap = wdFindContinue 
       .Format = False 
       .MatchCase = False 
       .MatchWholeWord = False 
       .MatchKashida = False 
       .MatchDiacritics = False 
       .MatchAlefHamza = False 
       .MatchControl = False 
       .MatchByte = False 
       .MatchAllWordForms = False 
       .MatchSoundsLike = False 
       .MatchFuzzy = False 
       .MatchWildcards = True 
      End With 
     End If 
    Loop 
End Sub 
+0

ありがとう非常に、あなたの助けが大いに感謝します。 – asad41163

関連する問題