(このページはグループ:turgenev_ReplaceNonBMPinWordの情報をコピーしたものです)
Sub Macro0()
Set objUndo = Application.UndoRecord
objUndo.StartCustomRecord ("Replace All with HanaMin")
'Fix the skipped blank Header/Footer problem.
lngValidate = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document.
Dim rngStory As Range
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories.
Do
SearchAndReplaceInStory rngStory
On Error Resume Next
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
SearchAndReplaceInStory oShp.TextFrame.TextRange
End If
Next
End If
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
objUndo.EndCustomRecord
End Sub
Sub SearchAndReplaceInStory(myStoryRange As Range)
Dim CJK As String
Dim CJKComp As String
Dim CJKA As String
Dim IVS As String
Dim SVS As String
Dim CJKBtoF As String
Dim CJKCompS As String
Dim TIP As String
Dim KRadi As String
Dim RadiSup As String
CJK = "[" & ChrW(&H4E00) & "-" & ChrW(&H9FFF) & "]"
CJKComp = "[" & ChrW(&HF900) & "-" & ChrW(&HFAFF) & "]"
CJKA = "[" & ChrW(&H3400) & "-" & ChrW(&H4DBF) & "]"
KRadi = "[" & ChrW(&H2F00) & "-" & ChrW(&H2FDF) & "]"
RadiSup = "[" & ChrW(&H2E80) & "-" & ChrW(&H2EFF) & "]"
IVS = ChrW(&HDB40) & "[" & ChrW(&HDD00) & "-" & ChrW(&HDDEF) & "]"
SVS = "[" & ChrW(&HFE00) & "-" & ChrW(&HFE0F) & "]"
CJKBtoF = "[" & ChrW(&HD840) & "-" & ChrW(&HD87D) & "][" & ChrW(&HDC00) & "-" & ChrW(&HDFFF) & "]"
CJKCompS = ChrW(&HD87E) & "[" & ChrW(&HDC00) & "-" & ChrW(&HDFFF) & "]"
TIP = "[" & ChrW(&HD880) & "-" & ChrW(&HD8BF) & "][" & ChrW(&HDC00) & "-" & ChrW(&HDFFF) & "]"
replaceFont myStoryRange, "花園明朝A", CJK
replaceFont myStoryRange, "花園明朝A", CJKComp
replaceFont myStoryRange, "花園明朝A", CJKA
replaceFont myStoryRange, "花園明朝A", KRadi
replaceFont myStoryRange, "花園明朝A", RadiSup
replaceFont myStoryRange, "花園明朝A", CJKCompS
replaceFont myStoryRange, "花園明朝A", CJK & IVS
replaceFont myStoryRange, "花園明朝A", CJKA & IVS
replaceFont myStoryRange, "花園明朝A", CJKBtoF & IVS
replaceFont myStoryRange, "花園明朝A", TIP & IVS
replaceFont myStoryRange, "花園明朝A", CJK & SVS
replaceFont myStoryRange, "花園明朝A", CJKA & SVS
replaceFont myStoryRange, "花園明朝A", CJKBtoF & SVS
' replaceFont myStoryRange, "花園明朝A", TIP & SVS
replaceFont myStoryRange, "花園明朝B", CJKBtoF
replaceFont myStoryRange, "花園明朝B", TIP
End Sub
Sub replaceFont(myStoryRange As Range, fontName As String, text As String)
myStoryRange.Find.ClearFormatting
myStoryRange.Find.Replacement.ClearFormatting
myStoryRange.Find.Replacement.Font.Name = fontName
With myStoryRange.Find
.text = text
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub