3

Webサイトに、文字列「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.LoadFromFileandWinHttp.WinHttpRequest.Openメソッドを使用してみましたが、どちらもファイルを読み取ることができません。

何を記入GetCurrentVersionNumber()するかについての提案をいただければ幸いです。:)

4

2 に答える 2

8

質問に直接答えることはできませんが、より簡単なアプローチは、テキストファイルではなくXMLファイルにすることです。URLからXMLファイルを簡単に開くための組み込みツールが他にもあります。2番目の利点は、柔軟性が高くなるため、後で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?。?への参照を追加する必要があります。ただし、ライブラリ。

于 2012-09-06T17:20:40.880 に答える
2

これを試してください(未テスト

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
于 2012-09-06T17:05:14.860 に答える