[VBA] htmlデコード関数

この記事は投稿は1年以上経過しており、最新の情報でない場合があります。 ご注意下さい。
VBAでhtmlデコードする必要が出てきた。

めんどくさがりなので、頭を使わずにネットのソースコピペで済まそうとググってみた。

しかし、URLデコード/エンコードはググったらすぐ出てきたのにhtmlデコードは出てこない。

すこし検索範囲を広げると英語圏でVBScriptでのhtmlデコードを発見した
→ Classic ASP (VBScript) convert HTML codes to plain text
コピペして、ほんんんnの少しだけ修正して完成


スポンサーリンク

Function HTMLDecode(sText As String)
Dim regEx
Dim matches
Dim match
sText = Replace(sText, """, Chr(34))
sText = Replace(sText, "<", Chr(60))
sText = Replace(sText, ">", Chr(62))
sText = Replace(sText, "&", Chr(38))
sText = Replace(sText, " ", Chr(32))

Set regEx = CreateObject("VBScript.RegExp")
With regEx
.Pattern = "&#(\d+);" 'Match html unicode escapes
.Global = True
End With
Set matches = regEx.Execute(sText)

'Iterate over matches
For Each match In matches
'For each unicode match, replace the whole match, with the ChrW of the digits.
sText = Replace(sText, match.Value, ChrW(match.SubMatches(0)))
Next

HTMLDecode = sText
End Function
ソースは斜め読みもしてないけど、動いてるので問題ないぽ。
リンク元に感謝

 

スポンサーリンク

この記事をシェアする

関連記事

  1. 感謝 2016.11.25 4:32am
    このサイトのソースから、
     https://www.amazon.co.jp/%E7%A7%91%E5%AD%A6%E6%8A%80%E8%A1%93%E8%A8%88%E7%AE%97%E3%81%AE%E3%81%9F%E3%82%81%E3%81%AEPython%E5%85%A5%E9%96%80-%E2%80%95%E2%80%95%E9%96%8B%E7%99%BA%E5%9F%BA%E7%A4%8E%E3%80%81%E5%BF%85%E9%A0%88%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%80%81%E9%AB%98%E9%80%9F%E5%8C%96-%E4%B8%AD%E4%B9%85%E5%96%9C-%E5%81%A5%E5%8F%B8/dp/4774183881
    次のようなテキストを得るために、
     (2016-09-22) 978-4774183886 _科学技術計算のためのPython入門 ――開発基礎、必須ライブラリ、高速化
    非常に役立ちました。
    どうも有り難う。

    ソースでは、「科学技術計算のための .....」のところが
          「科学技術計算 .....」
    のようになっていて、5、6時間は検索していました。
    本当にありがとう。