• Downloading from a site (VBA Word 2000)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Downloading from a site (VBA Word 2000)

    • This topic has 13 replies, 6 voices, and was last updated 22 years ago.
    Author
    Topic
    #385426

    I want to insert some code for button, such that when clicked it will download a file from our intranet site and save it in a specific location. I’ve looked through some posts on this site, but not found what I needed. I can use the FollowHyperlink method, but I’m concerned that it may respond differently depending on settings in IE. For example, some may have it set to download Word documents to the PC while others may have it set to open the documents, while some may be prompted with a choice. Maybe I’m wrong and that won’t happen, but I’m concerned that it could.

    If I knew it would always open the document, that would be easy to use the SaveAs method to save the file to a static location where I could find in code that follows the download. Or if I could set it to always download and specify the location to which it will download. Is there a registry setting I could access that has these settings?

    I really don’t want to distribute an FTP application or library with the documents that contain my code as that will add a whole new layer of complexity to noncomplex users.

    Thanks for the help!!
    Troy

    Viewing 1 reply thread
    Author
    Replies
    • #666345

      OK I think I’ve found part of my solution searching in the IE forum. The setting I’m looking at is in the registry (at least for Win2K) at:

      HKEY_CLASSES_ROOTWord.Document.8

      There is an key “EditFlags” with a value of

      10000 (Don’t confirm whether to open after download)

      or

      0 (Do confirm whether to open after download)

      OK, here is what I want to do:

      1. Confirm that Word 2000 is the latest version of Word installed. (NEED HELP ON THIS STEP)
      – If not (i.e. if user is using Word 2002 (XP) or 2003 beta, give a message and exit sub. (I have code to do this)
      2. Read the value of this key. (NEED HELP ON THIS STEP) (By the way, what happens if this key does not exist (rare but possible)? I’ll need to know that for an error handler.
      – If the value is 0,
      – a. set a variable to change it back to this value (no problem here, I’ll just create a string variable to store this value).
      – b. Then change the value to 1000 (NEED HELP ON THIS STEP)
      3. Open the link and save the file to a designated location (Already have the code that does this just fine.)
      4. If necessary, set the registry key back to its original value (Assume I can use the same method you provide for step 2. b. above)

      One other concern. Most users would have Windows 2000. A few might have XP and an even smaller minority might have 98. Is there any way to confirm the registry key I need to adjust for these 2 operating systems?

      FYI, I’m assuming that the users have IE 5.5 or above installed. There is no reason they should not.

      Thanks for the help!!
      Troy

      • #666494

        There are a variety of posts giving the API calls to read/write the registry from VB/VBA. I haven’t done it that way for so long, I don’t have any sample code for you. Recently I’ve been using Windows Script Host (WSH) and VBScript. E.g., post 121209. But I think in your case the API is a better approach.

        • #666519

          You don’t even need API calls to read and write to a custom key under CurrentUser. Just use the VBA GetSetting and SaveSetting calls.

          • #666522

            Please correct me if I’m wrong. I thought that GetSetting and SaveSetting could only read/write (string) values under HKEY_CURRENT_USERSoftwareVBA and VBA Program Settings. This implies that they can’t be used to get at the EditFlags value of the Word.Document.8 key in HKEY_CLASSES_ROOT. The System object in Word VBA has a property PrivateProfileString that can be used to retrieve/set any (string) value in the registry.

            • #671803

              Hans,

              I believe you are correct. GetSetting and SaveSetting only work with

              HKEY_CURRENT_USERSoftwareVB and VBA Program Settings

              according to John Walkenbach’s book on Excel 2000 VBA programming. I’ve used these functions successfully to stash settings for my VBA programs, which is probably their intended purpose. I’m thinking that Charlotte didn’t mean to imply that they could access ANY setting in HKCU.

    • #675522

      Public 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

      Simple API to download a file from the web (or an intranet) to a local machine.

      • #676059

        That looks great, but I’m not sure I understand the syntax enough to use it.

        Obviously I put this up in the declarations section. Then I would put in “URLDownloadToFile” in my procedure.

        What are each of the arguements mentioned? (I assume one of them is the URL)

        Thanks!!
        Troy

        • #676067

          pCaller is not important if you don’t call this function from an ActiveX component, you can set it to 0.
          szURL is the URL (string) you want to download from.
          szFileName is the name of the file to be written.
          dwReserved is not used; it must be set to 0.
          lpfnCB is a pointer to a callback function; it can be set to 0.

          Here is a more user-friendly wrapper (from AllApi):

          Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
          Dim lngRetVal As Long
          lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
          If lngRetVal = 0 Then DownloadFile = True
          End Function

          This function takes just 2 arguments: the URL and the file name. It returns True if the download succeeded. Use it like this:

          DownloadFile “http://www.allapi.net”, “c:allapi.htm”

        • #676144

          Hans posted exactly what you need. I was going to paste the MSDN page in here, if you want it, let me know, and I’ll email it to you.

          • #676391

            Drew,

            Please post a link to the MSDN page so that everyone can benefit from it.

            • #676405

              I was talking about the CD version of the MSDN, so it’s not a link…..

          • #676186

            Yes please e-mail me at XXXXXXXXXX

            Thanks for the help!! It works great!!

            Troy

    Viewing 1 reply thread
    Reply To: Downloading from a site (VBA Word 2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: