2012-11-26 4 views
0

나는 3 개의 공백 중 첫 번째 공백의 형식을 바꾸기위한 매크로를 작성했으며 파란색 글꼴의 숫자가있는 특정 문자열과 괄호 안의 공백을 바꿔주는 공백을 교체하기위한 매크로를 작성했습니다. 끈.MS-Word VBA 스크립트 찾기 및 바꾸기

이 두 가지 절차를 최적화하는 방법을 알고 있습니까 (저는 검색의 와일드 카드를 사용하고 MS-Word의 대화 상자를 바꾸지 만 VBA에서 이것을 사용하는 것이 바람직하지 않습니까?).

내 매크로 :

Sub replace_3spaces() 

Dim str_after As String 
Dim re_number As Integer 

str_after = "normal" 
re_number = "1" 

    Selection.Find.ClearFormatting 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "([^s]{3})" & "(" & str_after & ")" 
     .Replacement.Text = "§§§\2" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 

    Selection.Find.ClearFormatting 
    Selection.Find.Replacement.Font.ColorIndex = wdBlue 
    With Selection.Find 
     .Text = "§§§" 
     .Replacement.Text = re_number & " " 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 
End Sub 

답변

0

나는 당신이 당신의 코드가 를 않기 때문에 뭘 하려는지 전혀 모르겠어요. 으로 최적화한다고 말하면 더 빠른 방법이 있는지 묻습니다. 코드를 줄여 줄 수 있습니까? ,

With Selection.Find 
     .ClearFormatting 
     .Replacement.ClearFormatting 
     .Text = "([^s]{3})(normal)" 
     .Replacement.Text = "§§§\2" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchWildcards = True 
     .Execute Replace:=wdReplaceAll 
     .Forward = False 
     .ClearFormatting 
     .Replacement.Font.ColorIndex = wdBlue 
     .Format = True 
     .Text = "§§§" 
     .Replacement.Text = "1 " 
     .Execute Replace:=wdReplaceAll 
    End With 

당신이 그 크기를 가지고 있다는 사실을 바탕으로 : 나는 당신이 처음에 설정 한 치수에 대한 이유를 볼 수 없습니다, 그래서 당신은 단지 짧은 코드를 찾고 있다면, 다음과 같은 사용할 수 있습니다 그리고 그 중 하나에 번호를 사용 했으므로 각 인스턴스에 번호가 부여 된 번호 매기기 시스템을 실제로 만들려고했다고 생각합니다. 이 경우 사용할 코드는 다음과 같습니다.

Dim str_after, oldColor As String 
Dim re_number As Integer 

str_after = "normal" 
re_number = "1" 

    Selection.HomeKey unit:=wdStory 
    With Selection.Find 
     .ClearFormatting 
     .Replacement.ClearFormatting 
     .Text = "([^s]{3})" & "(" & str_after & ")" 
     .Replacement.Text = "§§§\2" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
     While Selection.Find.Execute 
      oldColor = Selection.Font.Color 
      Selection.Font.Color = wdColorBlue 
      Selection.TypeText Text:=re_number & " " 
      Selection.Font.Color = oldColor 
      Selection.TypeText Text:=str_after 
      re_number = re_number + 1 
     Wend