VBA:从URL打开文本文件以进行读取

时间:2012-09-06 16:51:24

标签: excel vba excel-vba

我的网站上有一个文本文件,其中只包含字符串“1.15”(对于我正在编写的应用程序的版本)。在初始化用户表单时,我想从其URL读取该文件并返回字符串“1.15”,以便我可以根据应用程序的版本(存储为const字符串)进行检查。

这是我想要的格式......

Const version As String = "1.14"
Const currentVersionURL As String = "http://mywebsite.com/currentversion.txt"

Sub UserForm_Initialize()

    If version <> GetCurrentVersionNumber() Then
        MsgBox "Please update the application."
    End If

End Sub

Function GetCurrentVersionNumber() As String

    ' What do I put here? :(

End Function

我知道Workbooks.OpenText方法,但我不想将字符串写入工作簿。我尝试使用ADODB.LoadFromFileWinHttp.WinHttpRequest.Open方法,但两者都无法读取文件。

任何有关填写GetCurrentVersionNumber()的内容的建议都将不胜感激。 :)

2 个答案:

答案 0 :(得分:7)

虽然它没有直接回答您的问题,但更简单的方法是将其设为XML文件而不是文本文件。有更多内置工具可以轻松地从URL打开XML文件。第二个优点是它还使其更加灵活,因此您可以在以后更轻松地将新数据元素添加到XML文件中。

例如,如果您创建了一个http://mywebsite.com/currentversion.xml文件,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<AppData>
    <Version>1.14</Version>
</AppData>

然后,在VB.NET中你可以像这样轻松阅读:

Function GetCurrentVersionNumber() As String
    Dim doc As New XmlDocument()
    doc.Load("http://mywebsite.com/currentversion.xml")
    Return doc.SelectSingleNode("/AppData/Version").InnerText
End Function

或者,在VBA中,您可以这样阅读:

Function GetCurrentVersionNumber() As String
    Dim doc As MSXML2.DOMDocument??  ' Where ?? is the version number, such as 30 or 60
    Set doc = New MSXML2.DOMDocument??
    doc.async = False
    doc.Load("http://mywebsite.com/currentversion.xml")
    GetCurrentVersionNumber = doc.SelectSingleNode("/AppData/Version").Text
End Function

但是,您需要添加对 Microsoft XML,v?。?库的引用。

答案 1 :(得分:2)

试试这个( UNTESTED

Option Explicit

Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, _
ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

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

Private Const MAX_PATH As Long = 260

Const currentVersionURL As String = "http://mywebsite.com/currentversion.txt"
Const version As String = "1.14"

Dim Ret As Long

Sub UserForm_Initialize()
    If version <> GetCurrentVersionNumber() Then
        MsgBox "Please update the application."
    End If
End Sub

Function GetCurrentVersionNumber() As String
    Dim strPath As String

    '~~> Destination for the file
    strPath = TempPath & "currentversion.txt"

    '~~> Download the file
    Ret = URLDownloadToFile(0, currentVersionURL, strPath, 0, 0)

    '~~> If downloaded
    If Ret = 0 Then
        Dim MyData As String, strData() As String

        Open "C:\MyFile.Txt" For Binary As #1
        MyData = Space$(LOF(1))
        Get #1, , MyData
        Close #1

        GetCurrentVersionNumber = MyData
    Else
        MsgBox "Unable to download the file"
        GetCurrentVersionNumber = ""
    End If
End Function

'~~> Get Users Temp Path
Function TempPath() As String
    TempPath = String$(MAX_PATH, Chr$(0))
    GetTempPath MAX_PATH, TempPath
    TempPath = Replace(TempPath, Chr$(0), "")
End Function
相关问题