2016-06-25 5 views
1

オブジェクトはシンプルです - 選択肢のテキストを大文字で置き換えるショートカットを「AllCaps」スタイルを適用せずにここでは行いません。Word VBAは大文字でのみ選択項目に表示されるテキストを置き換えます

私はSelection.DeleteSelection.Insertを使用して成功しましたが、段落スタイルに固有のフォントスタイルは保持されていないようです。

VBA Selection.Find.Execute:=ReplaceAllを使用すると、ドキュメント全体のすべてのインスタンスが置き換えられます。私はこれが.wrap=wdFindContinueの振る舞いでなければならないことを認識していますが、.wrap=wdFindStopを設定しました。私の最高の推測では、.textSelectionオブジェクトを呼び出す変数として設定されているため、が実行される結果、.textを設定すると何とか選択が失われます(コード選択をステップ実行すると視覚的には明らかになりません)。文書全体について。特定のケースの "find"(.text)文字列を手作業で入力すると、それは選択だけで実行されます(.Replacement.Textフィールドで呼び出された変数txtであっても!)が、実用的ではないことは明らかです。

はここに私のコードです:

Option Explicit 
Sub ReplaceWithUppercase() 

Dim pasteAutoFormatSetting As Boolean 
Dim txt As String 
'Save current user selection of whether to adjust spacing automatically when pasting: 
pasteAutoFormatSetting = Options.PasteAdjustWordSpacing 

'Turn off auto spacing adjustment: 
Options.PasteAdjustWordSpacing = False 

txt = Selection.Range.text 


With Selection.Font 
     .AllCaps = False 
End With 

'The next two lines work but do not seem to preserve built-in font styles 
'already applied to the selected text, so I wanted to instead use find/replace: 

'Selection.Range.Delete 
'Selection.Range.InsertAfter (UCase(txt)) 


    With Selection.Range.Find 
     .ClearFormatting 
     .Replacement.ClearFormatting 
     .text = txt 
     .Replacement.text = UCase(txt) 
     .Forward = True 
     .Wrap = wdFindStop 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
     .Execute Replace:=wdReplaceAll 
    End With 

'Return to user preference for auto apacing adjustment: 
Options.PasteAdjustWordSpacing = pasteAutoFormatSetting 

End Sub 
選択範囲の配列使っ

答えて

1

@PatentWookiee、私はあなたが最初に正しい軌道に乗っていたと思いますが、

Selection.Range.Delete 
Selection.Range.InsertAfter (UCase(txt)) 

問題は、あなたが選択したテキストを削除したら、あなたはその場所に置くものはassuます、ですあなたの削除前の最初の文字の書式設定。我々は、単にので、完全にあなたの書式を残して、選択した文字列の場合を交換

Option Explicit 
Sub ReplaceWithUppercase() 
    Selection.Text = UCase(Selection.Text) 
End Sub 

代わりの実際のテキスト(およびそれと一緒に書式設定を)交換する:この問題を解決するためにあなたが行きたいと思いますそのまま。あなたの質問を正しく読んだら、これはあなたの問題を完全に解決するはずです。そうでない場合は、私に知らせてください、我々はそれを介して動作します。

+0

私は1行のマクロが好きです、ありがとう! 「検索」テキストが選択定義の文字列変数である場合にのみ選択項目でwdReplaceAllを実行できるかどうかは不明ですが、「Selection.Text = someString'。 – PatentWookiee

+0

PatentWookiee、これはあなたの質問に反応するかどうかはわかりませんが、確かに使用することができます: 'Selection.Text = Replace(" abcdefg "、" cde "、" CDE ")" abCDEfg "を返すもの –

+0

いいでしょうかそのReplace関数の引数として正規表現を使うことができますが、私はVBAではないと推測しています。私は、正規表現を使用してテキストを検索して置き換えるマクロを書いていますが、それらはむしろ長年巻き込まれています: – PatentWookiee

1

高速作業 - とStrConv関数

Sub ChangeRangeToUCase() 
    Const vbUpperCase = 1 ' strConv constant 

    Dim arrCells As Variant 
    Dim intRow  As Integer 
    Dim intCol  As Integer 

    ' Assign Range to an array 
    arrCells = Selection 
    For intRow = LBound(arrCells, 1) To UBound(arrCells, 1) 
     For intCol = LBound(arrCells, 2) To UBound(arrCells, 2) 
      arrCells(intRow, intCol) = StrConv(arrCells(intRow, intCol), vbUpperCase) 
     Next intCol 
    Next intRow 

    Selection = arrCells 

End Sub 
+1

@ Jim_Simsonの回答は簡潔にするためにうなずきますが、私は何かを行うための複数の方法が好きです。 – PatentWookiee

+0

良いこと - セレクション内の多くのセルを扱っている場合、選択を直接使用するのではなく、配列メソッドがほぼ瞬時になることがわかります – dbmitch

+0

ありがとうございます - 私のライブラリにマクロをコピーして、大きな選択に対しては 'Selection.Text = someString'より速く実行するべきです。 – PatentWookiee

関連する問題