Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nefo-mi/342064 to your computer and use it in GitHub Desktop.
Save nefo-mi/342064 to your computer and use it in GitHub Desktop.
Sub Zenkaku2Hankaku()
Dim t As Integer
Dim myMsg As String
Dim FChr As String
Dim LChr As String
Selection.HomeKey Unit:=wdStory '文書の先頭に
On Error GoTo Errmsg:
With Selection.Find
.ClearFormatting
.Text = ""
.Replacement.Text = ""
.MatchFuzzy = False
'半角カタカナ
FChr = Chr("&HA6") '半角ヲ
LChr = Chr("&HDF") '半角゜
While .Execute(FindText:="[" & FChr & "-" & LChr & "]{1,}", _
Wrap:=wdFindContinue, MatchWildcards:=True) = True
Selection.Range.CharacterWidth = wdWidthFullWidth
t = t + 1
Wend
'数字
While .Execute(FindText:="[0-9]{1,}", _
Wrap:=wdFindContinue, MatchWildcards:=True) = True
Selection.Range.CharacterWidth = wdWidthHalfWidth
t = t + 1
Wend
'アルファベット
While .Execute(FindText:="[A-z]{1,}", _
Wrap:=wdFindContinue, MatchWildcards:=True) = True
Selection.Range.CharacterWidth = wdWidthHalfWidth
t = t + 1
Wend
'全角スペース
While .Execute(FindText:=" ", _
Wrap:=wdFindContinue, MatchWildcards:=True) = True
Selection.Range.CharacterWidth = wdWidthHalfWidth
t = t + 1
Wend
'全角(
While .Execute(FindText:="(", _
Wrap:=wdFindContinue, MatchWildcards:=True) = True
Selection.Range.CharacterWidth = wdWidthHalfWidth
t = t + 1
Wend
'全角)
While .Execute(FindText:=")", _
Wrap:=wdFindContinue, MatchWildcards:=True) = True
Selection.Range.CharacterWidth = wdWidthHalfWidth
t = t + 1
Wend
Selection.HomeKey Unit:=wdStory '文書の先頭に
If t > 0 Then
myMsg = t & "語、変換しました。"
Else
myMsg = "変換するべき文字はありませんでした。"
End If
MsgBox myMsg, vbInformation
End With
Exit Sub
Errmsg:
MsgBox "エラー!: " & Err.Description, vbExclamation
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment