Office関連

64ビット版OfficeでURLエンコード処理ができない?

2011/12/28 追記:
関連記事として「文字コードを指定してURLエンコードを行う」を書きました。

「64ビット環境でのScriptControlの代わり」で記事を書いていますが、64ビット環境でScriptControlを利用してURLエンコード処理を行おうとした場合、エラーが発生して処理することができません(64ビットOS上の32ビット版Office(WOW64上)ではエラーになりません)。

これは「msscript.ocx」が64ビット環境に対応していないためですが、今回は「64ビット環境でのScriptControlの代わり」で紹介している方法とは違う方法でURLエンコード処理を実行してみます。

[標準モジュール]

Option Explicit

Public Sub Sample()
  MsgBox EncodeURLx64("東京都千代田区")
End Sub

Public Function EncodeURLx64(ByVal str As String) As String
  Dim s As String, com As String, ret As String
  Dim ScriptFilePath As String, ExeFilePath As String
  
  ret = "" '初期化
  
  'スクリプト用コード設定
  s = "Option Explicit" & vbCrLf
  s = s & vbCrLf
  s = s & "Dim Args, ret" & vbCrLf
  s = s & vbCrLf
  s = s & "Set Args = WScript.Arguments" & vbCrLf
  s = s & "If Args.Count < 1 Then WScript.Quit" & vbCrLf
  s = s & "With CreateObject(""ScriptControl"")" & vbCrLf
  s = s & "  .Language = ""JScript""" & vbCrLf
  s = s & "  ret = .CodeObject.encodeURIComponent(Args(0))" & vbCrLf
  s = s & "End With" & vbCrLf
  s = s & "Set Args = Nothing" & vbCrLf
  s = s & "WScript.Echo ret"
  
  ScriptFilePath = VBA.Environ$("TEMP") & "\enc.vbs"
  With CreateObject("Scripting.FileSystemObject")
    With .CreateTextFile(ScriptFilePath, True)
      .Write s
      .Close
    End With
    If .FileExists(ScriptFilePath) Then
      ExeFilePath = .GetSpecialFolder(0).Path & "\SysWOW64\cscript.exe"
      If .FileExists(ExeFilePath) Then
        com = ExeFilePath & " //Nologo """ & ScriptFilePath & """ """ & str & """"
        ret = CreateObject("WScript.Shell").Exec(com).StdOut.ReadAll
      End If
      .DeleteFile ScriptFilePath
    End If
  End With
  EncodeURLx64 = ret
End Function

上記「Sample」を実行すると、MsgBoxでURLエンコード処理された文字列が表示されます。

上記コードでは一時的にVBScriptファイルを作成し、32ビット版のcscript.exeでそのスクリプトを実行することで、URLエンコード処理を行っています。

menu内にあるbuttonの数を増やす前のページ

McAf.eeで短縮URLを取得するブックマークレット次のページ

関連記事

  1. Office アドイン

    [Office用アプリ]選択範囲変更時のイベントを利用する。

    ※ この情報はOffice 2013 カスタマー プレビュー版を元にし…

  2. Office関連

    Instagram APIをVBAから呼び出してみる。

    最近画像共有系のSNS、Instagram(インスタグラム)を使い始め…

  3. Office関連

    セル内にあるブックマークをカウントするWordマクロ

    Twitterを眺めていたら下記ツイートを発見しました。【Wo…

  4. Office関連

    オフィス祭り 2018 in 東京が9月15日(土)に開催されます。

    突然ですが、私はMicrosoft Officeが大好きです。20…

  5. Office アドイン

    Office 365でVisio JavaScript APIsを試してみました。

    昨年末にVisio Onlineの機能をJavaScriptで拡張する…

コメント

  1. この記事へのコメントはありません。

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP