VBA ShellExecute强制URL为小写

时间:2013-11-16 20:20:20

标签: vba shellexecute

这曾经在上周工作。我怀疑Windows更新破坏了什么。使用ShellExecute时,它会将URL强制为小写,破坏传递给区分大小写的服务器的参数值!

Private Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" ( _
        ByVal hwnd As Long, _
        ByVal lpOperation As String, _
        ByVal lpFile As String, _
        Optional ByVal lpParameters As String, _
        Optional ByVal lpDirectory As String, _
        Optional ByVal nShowCmd As Long _
        ) As Long

Sub OpenBrowser()
    Let RetVal = ShellExecute(0, "open", "http://yaHOO.com?UPPERCASE=lowercase")

将打开http://www.yahoo.com/?uppercase=lowercase

版本

我正在使用Windows 8.1。我试过3个浏览器。 Chrome中的小写,IE中的小写和Opera会删除查询参数,但主机是小写的。

3 个答案:

答案 0 :(得分:3)

好的,我通过创建一个临时HTML文件,找到与之关联的可执行文件,然后直接使用URL启动可执行文件来解决它。啧。

Private Const SW_SHOW = 5       ' Displays Window in its current size and position
Private Const SW_SHOWNORMAL = 1 ' Restores Window if Minimized or Maximized

Private Declare Function ShellExecute Lib "shell32.dll" _
        Alias "ShellExecuteA" ( _
            ByVal hwnd As Long, _
            ByVal lpOperation As String, _
            ByVal lpFile As String, _
            Optional ByVal lpParameters As String, _
            Optional ByVal lpDirectory As String, _
            Optional ByVal nShowCmd As Long _
            ) As Long

Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" ( _
    ByVal lpFile As String, _
    ByVal lpDirectory As String, _
    ByVal lpResult As String _
    ) As Long

Private Declare Function GetTempPath Lib "kernel32" _
  Alias "GetTempPathA" ( _
    ByVal nBufferLength As Long, _
    ByVal lpBuffer As String) As Long

Private Declare Function GetTempFileName Lib "kernel32" _
  Alias "GetTempFileNameA" ( _
    ByVal lpszPath As String, _
    ByVal lpPrefixString As String, _
    ByVal wUnique As Long, _
    ByVal lpTempFileName As String) As Long

Public Function GetTempFileNameVBA( _
  Optional sPrefix As String = "VBA", _
  Optional sExtensao As String = "") As String
    Dim sTmpPath As String * 512
    Dim sTmpName As String * 576
    Dim nRet As Long
    Dim F As String
    nRet = GetTempPath(512, sTmpPath)
    If (nRet > 0 And nRet < 512) Then
      nRet = GetTempFileName(sTmpPath, sPrefix, 0, sTmpName)
      If nRet <> 0 Then F = Left$(sTmpName, InStr(sTmpName, vbNullChar) - 1)
      If sExtensao > "" Then
        Kill F
        If Right(F, 4) = ".tmp" Then F = Left(F, Len(F) - 4)
        F = F & sExtensao
      End If
      GetTempFileNameVBA = F
    End If
End Function

Sub Test_GetTempFileNameVBA()
    Debug.Print GetTempFileNameVBA("BR", ".html")
End Sub

Private Sub LaunchBrowser()
    Dim FileName As String, Dummy As String
    Dim BrowserExec As String * 255
    Dim RetVal As Long
    Dim FileNumber As Integer

    FileName = GetTempFileNameVBA("BR", ".html")
    FileNumber = FreeFile                    ' Get unused file number
    Open FileName For Output As #FileNumber  ' Create temp HTML file
        Write #FileNumber, "<HTML> <\HTML>"  ' Output text
    Close #FileNumber                        ' Close file
    ' Then find the application associated with it
    RetVal = FindExecutable(FileName, Dummy, BrowserExec)
    Kill FileName                   ' delete temp HTML file
    BrowserExec = Trim(BrowserExec)
    ' If an application is found, launch it!
    If RetVal <= 32 Or IsEmpty(BrowserExec) Then ' Error
        MsgBox "Could not find associated Browser", vbExclamation, "Browser Not Found"
    Else
        RetVal = ShellExecute(0, "open", BrowserExec, "http://www.yaHOO.com?case=MATTERS", Dummy, SW_SHOWNORMAL)
        If RetVal <= 32 Then        ' Error
            MsgBox "Web Page not Opened", vbExclamation, "URL Failed"
        End If
    End If
End Sub

答案 1 :(得分:1)

使用FileProtocolHandler而不是ShellExecute:

Public Declare Function FileProtocolHandler Lib "url.dll" _
      Alias "FileProtocolHandlerA" (ByVal hwnd As Long, ByVal hinst As Long, _
      ByVal lpszCmdLine As String, ByVal nShowCmd As Long) As Long

Public Sub OpenHyperlink(ByVal Url)
  FileProtocolHandler 0, 0, Url, 1
End Sub

使用FileProtocolHandler,不会发生小写转换。

我在Windows 8.1下遇到此问题,但在Windows 7下没有。

答案 2 :(得分:-1)

在我的情况下使用temp&#34; .html&#34;文件不是一个选项,因为它们链接到gedit所以我可以编辑它们。

我不能说它是否适用于域部分,但我需要GET参数区分大小写。

我通过简单编码十六进制的所有内容来实现。不仅仅是&#34; /&#34;但是一切。