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関連

    PDFのしおり数を取得するVBAマクロ

    「VBA Acrobat しおり数」といったキーワード検索でのアクセス…

  2. Office関連

    [リボン・カスタマイズ]グループの表示・非表示をトグルボタンで切り替える。

    数年前に書いた記事に下記コメントをいただきました。Excelに…

  3. Office関連

    ユーザー設定フォームに基づいてメールを作成するOutlookマクロ

    Outlookには「ユーザー設定フォーム」(Custom Forms)…

  4. Office アドイン

    Office Scripts機能によってWeb版Officeの操作を自動化する

    前回、Ignite 2019で発表されたPower Automate(…

  5. アイコン一覧

    Office 365アイコン(imageMso)一覧(X,Y,Z)

    Office 365のデスクトップ版Officeアプリケーション(Wo…

コメント

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP