Results 1 to 14 of 14
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Downloading from a site (VBA Word 2000)

    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

  2. #2
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Downloading from a site (VBA Word 2000)

    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

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Downloading from a site (VBA Word 2000)

    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>post 121209</post#>. But I think in your case the API is a better approach.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Downloading from a site (VBA Word 2000)

    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.
    Charlotte

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Downloading from a site (VBA Word 2000)

    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.

  6. #6
    Lounger
    Join Date
    Aug 2002
    Location
    Boise, Idaho, USA
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Downloading from a site (VBA Word 2000)

    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.

  7. #7
    4 Star Lounger
    Join Date
    Aug 2002
    Location
    Dallas, Texas, USA
    Posts
    594
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Downloading from a site (VBA Word 2000)

    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.

  8. #8
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Downloading from a site (VBA Word 2000)

    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

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Downloading from a site (VBA Word 2000)

    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"

  10. #10
    4 Star Lounger
    Join Date
    Aug 2002
    Location
    Dallas, Texas, USA
    Posts
    594
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Downloading from a site (VBA Word 2000)

    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.

  11. #11
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Downloading from a site (VBA Word 2000)

    Thanks Hans!!

    Troy

  12. #12
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Downloading from a site (VBA Word 2000)

    Drew,

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

  13. #13
    4 Star Lounger
    Join Date
    Aug 2002
    Location
    Dallas, Texas, USA
    Posts
    594
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Downloading from a site (VBA Word 2000)

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

  14. #14
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Downloading from a site (VBA Word 2000)

    Yes please e-mail me at XXXXXXXXXX

    Thanks for the help!! It works great!!

    Troy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •