Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

UrlDownloadToFile() works in Word macro but not in vb.net

Posted on 2007-04-11
9
857 Views
Last Modified: 2013-11-26
I am using urldownloadtofile() to download a file from the web to a local machine.  The coding works fine in a Word macro but does not work in vb.net. The strange thing is that it used to work and as far as I know I have not changed any of the settings. I now get
AccessVioationException was unhandled (attempted to read or write protected memory) on the line DownloadFile2 = URLDownloadToFile(0&, _    .....

The coding is:

Option Explicit On


Public Class Form1
    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 DeleteUrlCacheEntry Lib "Wininet.dll" _
       Alias "DeleteUrlCacheEntryA" _
      (ByVal lpszUrlName As String) As Long

    Private Const ERROR_SUCCESS As Long = 0
    Private Const BINDF_GETNEWESTVERSION As Long = &H10
    Private Const INTERNET_FLAG_RELOAD As Long = &H80000000
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call DownloadFromWeb()
    End Sub


    Sub DownloadFromWeb()
        '
        ' DownloadFromWeb Macro
        ' Macro created 3/16/2007 by Brian O'Gorman
        '
        Dim sSourceUrl As String
        Dim sLocalFile As String


        sSourceUrl = "http://63.134.211.113/TestText.csv"
        sLocalFile = "c:\Web Design\LIFEnet\TestText.csv"

        'Label1.Caption = sSourceUrl
        'Label2.Caption = sLocalFile

        'Attempt to delete any cached version of
        'the file. Since we're only interested in
        'nuking the file, the routine is called as
        'a sub. If the return value is requires
        '(calling as a function), DeleteUrlCacheEntry
        'returns 1 if successful, or 0 otherwise, e.g.
        '  If DeleteUrlCacheEntry(sourceUrl) = 1 Then
        '     Debug.Print "cached file found and deleted"
        '  Else
        '     Debug.Print "no cached file for " & sourceUrl
        '  End If
        'Note that the remote URL is passed as this is the
        'name the cached file is known by. This does NOT
        'delete the file from the remote server.
        Call DeleteUrlCacheEntry(sSourceUrl)

        If DownloadFile2(sSourceUrl, sLocalFile) = True Then

           

        End If

    End Sub


    Private Function DownloadFile2(ByVal sSourceUrl As String, _
    ByVal sLocalFile As String) As Boolean

        'Download the file. BINDF_GETNEWESTVERSION forces
        'the API to download from the specified source.
        'Passing 0& as dwReserved causes the locally-cached
        'copy to be downloaded, if available. If the API
        'returns ERROR_SUCCESS (0), DownloadFile returns True.
        DownloadFile2 = URLDownloadToFile(0&, _
                                         sSourceUrl, _
                                         sLocalFile, _
                                         BINDF_GETNEWESTVERSION, _
                                         0&) = ERROR_SUCCESS

    End Function
End Class

Would be grateful for help on this.
Brian



                                       
0
Comment
Question by:bogorman
  • 4
  • 3
9 Comments
 
LVL 3

Expert Comment

by:JipFromParis
ID: 18889384
Strangely, any documentation, including MSDN2 web site state that the 4th parameter for URLDownloadToFile should be 0. Please, first make your code conforming with documentation, then try again an let me know if you are still in trouble.
0
 

Author Comment

by:bogorman
ID: 18889408
Hi,
Do you mean changing the declaration to:
Private Const BINDF_GETNEWESTVERSION As Long = 0

Brian

0
 
LVL 3

Expert Comment

by:JipFromParis
ID: 18889502
Not exactly. The declaration is fine, however using BINDF_GETNEWESTVERSION as the 4th parameter doesn't conform to the documentation. Fix suggestion is :

DownloadFile2 = URLDownloadToFile(0&, _
                                         sSourceUrl, _
                                         sLocalFile, _
                                         0&, _
                                         0&) = ERROR_SUCCESS
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:bogorman
ID: 18889941
Hi,
I now get PInvokeStackImbalance was detected
Brian
0
 
LVL 3

Accepted Solution

by:
JipFromParis earned 500 total points
ID: 18890163
Much better ;-) This is a problem with the way Interop functions is declared. I will check later.
0
 

Author Comment

by:bogorman
ID: 19133523
Hi JipFromParis,
Di you ever find out anything more about the causes of this problem?
Regards
Brian
0
 

Author Comment

by:bogorman
ID: 19332263
Hi,
Although JipFromParis did not complete the answer to the question he did try, so I have assigned the points to him
Brian
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to limit User Input 2 27
Name Space error VS2015 1 22
Where is this file? 3 25
VB.net Use code to open two DateTimePickers at the same time 7 34
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question