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用アプリ]マニフェストファイルをSharePointに配置する。

    今更になりますが、今回はマニフェストファイルをSharePoint上に…

  2. Office アドイン

    [Office用アプリ]アプリ開発コンテスト・受賞者発表

    以前書いた記事でお知らせしていた「Apps for Office アプ…

  3. Office関連

    アクティブなスライドを取得するPowerPointマクロ

    PowerPointのマクロを触っていて、「ActiveSlide」の…

  4. Office関連

    Outlookの連絡先をvcf形式で一括保存する方法

    Outlookの連絡先をvcf形式で保存する場合、通常は「連絡先を v…

  5. アイコン一覧

    Office 2013 アイコン一覧(G)

    ・Office 2013 アイコン一覧 NUM…

  6. Office関連

    文字列を横方向に移動するWordマクロ(WordBasic編)

    いつもお世話になっているWord MVPの新田さんが、まるでカニの動き…

コメント

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP