2017年11月12日

[Excel VBA] 外部テキストの文字列を一斉置換する関数。(UTF-8 アンド 通常版対応版)

1. 文字コード UTF-8 の場合
Call ReplaceUTF(一斉置換したいファイルのフルパス, 検索文字列, 置換文字列)
 
2. 文字コード UTF-8 以外の場合
Call ReplaceTXT(一斉置換したいファイルのフルパス, 検索文字列, 置換文字列)
 
[具体例]
フォルダ「C:\hinden\events.files\」内にある
ファイル「sheet001.htm」 (charset=UTF-8) の中に
「_parent」という文字列があったらすべて「_blank」という文字列に置き換えまくりたい。
 ↓ 
Call ReplaceUTF("C:\hinden\events.files\sheet001.htm", "_parent", "_blank")



[関数 : ReplaceUTF8]

'テキストを文字列置換 (文字コードがUTF-8の場合)
Public Function ReplaceUTF8(FileName As String, TargetText As String, Optional NewText As String = "")
Dim buf_strTxt As String '読み込みバッファ
'全文読み込み
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.Open
.LoadFromFile FileName
buf_strTxt = .ReadText
.Close
End With

'置換処理
buf_strTxt = Replace(buf_strTxt, TargetText, NewText, , , vbTextCompare)

'全文書き込み
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.Type = 2
.Open
.WriteText buf_strTxt
.SaveToFile (FileName), 2
.Close
End With
End Function



[関数 : ReplaceTXT]

'テキストを文字列置換 (通常版)
Public Function ReplaceTXT(FileName As String, TargetText As String, Optional NewText As String = "")
Dim fso As FileSystemObject 'ファイルシステムオブジェクト
Dim TXT As TextStream 'テキストストリームオブジェクト
Dim buf_strTxt As String '読み込みバッファ

On Error GoTo Func_Err:
'オブジェクト作成
Set fso = CreateObject("Scripting.FileSystemObject")
Set TXT = fso.OpenTextFile(FileName, ForReading)

'全文読み込み
buf_strTxt = TXT.ReadAll
TXT.Close

'元ファイルをリネームして、テンポラリファイル作成
Name FileName As FileName & "_"

'置換処理
buf_strTxt = Replace(buf_strTxt, TargetText, NewText, , , vbTextCompare)

'書込み用テキストファイル作成
Set TXT = fso.CreateTextFile(FileName, True)
'書込み
TXT.Write buf_strTxt
TXT.Close

'テンポラリファイルを削除
fso.DeleteFile FileName & "_"
'終了処理
Func_Exit:
Set TXT = Nothing
Set fso = Nothing
Exit Function
Func_Err:
MsgBox "Error Number : " & Err.Number & vbCrLf & Err.Description
GoTo Func_Exit:
End Function



このExcelに現物アリ。
 ↓ 
http://www2u.biglobe.ne.jp/~hinden/etc/excel/etc.xls



[関連する過去日記]

[「Ma_ho_Ma_ho_Family」のサイト。全ファイルの文字コードを一斉に「UTF-8」に改修]
http://hinden.at.webry.info/201711/article_6.html

上記 記事 に載せてある手順にて大体よろしかったんですけど、Excel VBA で自動作成しているページに、不具合 (文字化け) が発生しました (文字列の Replace 時に、アカンかったわけです)。それに対処したのが、今回の関数です。

これでもう、大丈夫。
 ↓ 
http://www2u.biglobe.ne.jp/~hinden/live

改修後の「まほまほファミリー」のホームページ。
 ↓ 
http://www2u.biglobe.ne.jp/~hinden/
humide : photo / 湿気 | Ma_ho_Ma_ho_Family / まほまほファミリー

[その他の、関連する過去日記]

[「Ma_ho_Ma_ho_Family」のサイト、一斉にページ更新しました。(VBAを自作して)]
http://hinden.at.webry.info/201710/article_20.html

[[Excel VBA] 表記ゆれを統一。英数字・記号はすべて半角に、カタカナは全角に置換。]
http://hinden.at.webry.info/201710/article_9.html

Excel VBAスパテク368 2007/2003/2002対応
翔泳社
福光 洋子

amazon.co.jpで買う
Amazonアソシエイト by Excel VBAスパテク368 2007/2003/2002対応 の詳しい情報を見る / ウェブリブログ商品ポータル


hinden Takahashi, Hideki ひんでん 高橋 秀樹 | facebookひんでん (ユリコ暴走)(@Gemini_hinden)さん | Twitter


hinden Takahashi, Hideki / ひんでん 高橋 秀樹--------
about me
--------
hinden Takahashi, Hideki
ひんでん 高橋 秀樹
http://hinden.at.webry.info/200002/article_1.html

[events]
イベント情報一覧
http://www2u.biglobe.ne.jp/~hinden/live
ラベル:コンピュータ
【関連する記事】
posted by (旧) hinden (まほまほファミリー) at 22:57| 東京 ☁| Comment(0) | コンピュータ | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。