VBA to open/save excel file to hard drive (1 Viewer)

ggodwin

Registered User.
Local time
Today, 16:07
Joined
Oct 20, 2008
Messages
112
I have a VBA project that I am working on and have reached the end and just before the task is complete it does not do what I want it to do.

I am creating my first Access db and not real all that good with access or the VBA. The data that I am attempting to save will be the the key kog to my access database. Currently I use VBA in excel to navigate the website to access the data and have no problem doing so.

However, to make the system better and more multiuser friendly I am attempting to move this to access. I have re-written the VBA into access and I can navigate to the excel file no problem. However, currently the excel file opens in an IE instance window and I can not save it to my hard drive.

If you have any idea how I can tell it to save the file I would greatly appreciate your advise.

Current Code:
Code:
Option Compare Database
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
Public Function SKPIUPDATE()
Dim QPR As Object
Dim lnk As Object
Dim TimeOut As String
Dim frm As Object
Dim Start As Object
Dim Finish As Object
Dim drp2 As Object
Dim drp1 As Object
Dim src1 As Object
Dim p1 As Variant
Dim objWB As Object
Dim objExc As Object
 
Set QPR = CreateObject("InternetExplorer.application")
 
    QPR.Visible = True
 
    QPR.navigate "[URL]https://www.portal.toyotasupplier.com/wps/myportal/[/URL]"
 
  TimeOut = Now + TimeValue("00:00:20")  '-- wait maximum of 20 seconds
   Do While QPR.Busy Or QPR.readyState <> 4
      DoEvents
      If Now > TimeOut Then
        MsgBox "Time Out before Login"
        Exit Function
      End If
   Loop
 
   With QPR.Document.Forms("Login")
      .User.Value = "xxxxxxx"
      .Password.Value = "xxxxxxx"
      .submit
   End With
 
   TimeOut = Now + TimeValue("00:00:40")  '-- wait maximum of 10 seconds
   Do While QPR.Busy Or QPR.readyState <> 4
      DoEvents
      If Now > TimeOut Then
         MsgBox "Time Out after Login"
         Exit Function
      End If
   Loop
    QPR.navigate ("[URL]https://www.portal.toyotasupplier.com/skpi/[/URL]")
 
      TimeOut = Now + TimeValue("00:00:05")  '-- wait 1 second for above navigation to take effect
      Do While Now < TimeOut
         DoEvents
      Loop
 
    TimeOut = Now + TimeValue("00:00:50")  '-- wait maximum of 10 seconds
   Do While QPR.Busy Or QPR.readyState <> 4
      DoEvents
      If Now > TimeOut Then
         MsgBox "Did not navigate to SKPI application"
         Exit Function
      End If
   Loop
 
   Set lnk = QPR.Document.Links(3)   ' 3=TMMK-VEH,4=TMMK-PWT,5=TMMC,6=TMMTX,7=TABC,8=NUMMI,9=TMMI,10=TMMBC,11=TMMAL,12=TMMNK
   lnk.Click
 
   TimeOut = Now + TimeValue("00:00:20")  '-- wait 1 second for above navigation to take effect
      Do While Now < TimeOut
         DoEvents
   Loop
 
   QPR.navigate ("[URL]https://www.portal.toyotasupplier.com/skpi/SkpiGatewayServlet?jadeAction=NCPARTS_SEARCH[/URL]")
 
         TimeOut = Now + TimeValue("00:00:05")  '-- wait 1 second for above navigation to take effect
      Do While Now < TimeOut
         DoEvents
      Loop
 
    Set frm = QPR.Document.Forms("form1")
 
    Set Start = frm.all("SKPI_SEARCH_START_DATE_KEY")
    Start.Value = "01/01/" & Year(Now)
 
    Set Finish = frm.all("SKPI_SEARCH_END_DATE_KEY")
    Finish.Value = Format(Now - 1, "mm/dd/yyyy")
 
    Set drp2 = frm.all("SKPI_SEARCH_NC_TYPE_KEY")
    drp2.Item(1).Selected = True
 
    Set drp1 = frm.all("SKPI_SEARCH_NAMC_KEY")
    drp1.Item(p1).Selected = True
 
    Set src1 = frm.all("Submit")
 
    src1.Click
 
    TimeOut = Now + TimeValue("00:00:05")  '-- wait 1 second for above navigation to take effect
      Do While Now < TimeOut
         DoEvents
    Loop
 
    QPR.navigate ("[URL]https://www.portal.toyotasupplier.com/skpi/DownloadNCPartListServlet[/URL]")
 
    TimeOut = Now + TimeValue("00:01:00")  '-- wait 1 minute for above navigation to take effect
    Do While Now < TimeOut
         DoEvents
    Loop
 
    'Insert the file link below
    Const strUrl As String = "[URL]https://www.portal.toyotasupplier.com/skpi/DownloadNCPartListServlet[/URL]" 'example link
    Dim strSavePath As String
    Dim returnValue As Long
    'Path to save the file
    strSavePath = "C:\Documents and Settings\dsggodwin\My Documents\_DENSO QRE"
    returnValue = URLDownloadToFile(0, strUrl, strSavePath, 0, 0)
 
'err_1:
    'MsgBox Err.Description
    'Resume Err_Exit
Err_Exit:
 
   QPR.navigate ("[URL]https://www.portal.toyotasupplier.com/public/pr_logout.htm[/URL]")
 
End Function

Module 2
Code:
Option Compare Database
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
Sub DownloadFileFromWeb()
On Error GoTo err_1
    'Insert the file link below
    Const strUrl As String = "[URL]https://www.portal.toyotasupplier.com/skpi/DownloadNCPartListServlet.xls[/URL]" 'example link
    Dim strSavePath As String
    Dim returnValue As Long
    'Path to save the file
    strSavePath = "C:\Documents and Settings\dsggodwin\My Documents\_DENSO QRE\SKPI PARTS RETURN"
    returnValue = URLDownloadToFile(0, strUrl, strSavePath, 0, 0)
Err_Exit:
    Exit Sub
err_1:
    MsgBox Err.Description
    Resume Err_Exit
End Sub
 
Last edited:

ggodwin

Registered User.
Local time
Today, 16:07
Joined
Oct 20, 2008
Messages
112
Bump this thread up.
Just hoping...someone has an idea on how to do this.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 21:07
Joined
Sep 12, 2006
Messages
15,660
to save a file, get a query ,and look at same commands as before

ie
docmd.transferspreadsheet (excel directly) or docmd.transfertext (csv file)

in this case select option as acExportDelim etc, to indicate you are saving the data

you specify.among other things, an access query name, or table, and the destination file name
 

ggodwin

Registered User.
Local time
Today, 16:07
Joined
Oct 20, 2008
Messages
112
I appreciate the effort on this issue.

Let me clarify what is going on here.

In VBA in Access I can navigate to the webpage "download to Excel" button and select the data range that I want.

However, the data downloads (opens) in the Internet Explorer window. I think this is an add in to view Excel files.

From reading your idea. I think that you are under the impression that the data is already insider access.
 

jal

Registered User.
Local time
Today, 13:07
Joined
Mar 30, 2007
Messages
1,709
Not sure the problem. I used your code to download a spreadsheet to a file, like this:

Const strUrl As String = "http://www.epi.org/datazone/fambud/xls/epi_basic_family_budgets_rev200807b.xls"
Dim strSavePath As String
strSavePath = "C:\Dest.XLS"
Dim returnValue As Long
returnValue = URLDownloadToFile(0, strUrl, strSavePath, 0, 0)

and it worked fine. I guess the next step would be to open the file in excel. I used this line of code:

Application.FollowHyperlink "C:\dest.xls"

which should hopefully open the Excel file in the default spreadsheet-viewer (in my case Excel 2003). Worked okay, for me, anyway.

How is your situation different?
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 21:07
Joined
Sep 12, 2006
Messages
15,660
sorry, gg, got the wrong end of the stick completely

your problem (amd solution) looks very interesting and useful
 

ggodwin

Registered User.
Local time
Today, 16:07
Joined
Oct 20, 2008
Messages
112
Jal,
My code works great to download the data into the excel file.

However, I need to be able to save the file in excel in order to be able to import it. Currently, I just get an opened file.

The purpose of the project if to save time. My data takes about 8 minutes to navigate and download the data. I am trying to completely automate this process. I thought the hard part was done.

I can do it in VBA/Excel but have not been able to in Access.

Does yours save the file?
 
Last edited:

ggodwin

Registered User.
Local time
Today, 16:07
Joined
Oct 20, 2008
Messages
112
I just cleaned out my code and this will get me to the data.
It will navigate and open the .xls file (which appears in Internet Explorer)

Code:
Option Compare Database
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
Public Function SkpiUpdate()
Dim QPR As Object
Dim lnk As Object
Dim TimeOut As String
Dim frm As Object
Dim Start As Object
Dim Finish As Object
Dim drp2 As Object
Dim drp1 As Object
Dim src1 As Object
Dim p1 As Variant
Dim objWB As Object
Dim objExc As Object
 
Set QPR = CreateObject("InternetExplorer.application")
 
    QPR.Visible = True
    
    QPR.navigate "[URL]https://www.portal.toyotasupplier.com/wps/myportal/[/URL]"
    
  TimeOut = Now + TimeValue("00:00:20")  '-- wait maximum of 20 seconds
   Do While QPR.Busy Or QPR.readyState <> 4
      DoEvents
      If Now > TimeOut Then
        MsgBox "Time Out before Login"
        Exit Function
      End If
   Loop
 
   With QPR.Document.Forms("Login")
      .User.Value = "ggodwin"
      .Password.Value = "060469-c"
      .submit
   End With
 
   TimeOut = Now + TimeValue("00:00:40")  '-- wait maximum of 10 seconds
   Do While QPR.Busy Or QPR.readyState <> 4
      DoEvents
      If Now > TimeOut Then
         MsgBox "Time Out after Login"
         Exit Function
      End If
   Loop
    QPR.navigate ("[URL]https://www.portal.toyotasupplier.com/skpi/[/URL]")
    
      TimeOut = Now + TimeValue("00:00:05")  '-- wait 1 second for above navigation to take effect
      Do While Now < TimeOut
         DoEvents
      Loop
    
    TimeOut = Now + TimeValue("00:00:50")  '-- wait maximum of 10 seconds
   Do While QPR.Busy Or QPR.readyState <> 4
      DoEvents
      If Now > TimeOut Then
         MsgBox "Did not navigate to SKPI application"
         Exit Function
      End If
   Loop
     
   Set lnk = QPR.Document.Links(3)   ' 3=TMMK-VEH,4=TMMK-PWT,5=TMMC,6=TMMTX,7=TABC,8=NUMMI,9=TMMI,10=TMMBC,11=TMMAL,12=TMMNK
   lnk.Click
   
   TimeOut = Now + TimeValue("00:00:20")  '-- wait 1 second for above navigation to take effect
      Do While Now < TimeOut
         DoEvents
   Loop
   
   QPR.navigate ("[URL]https://www.portal.toyotasupplier.com/skpi/SkpiGatewayServlet?jadeAction=NCPARTS_SEARCH[/URL]")
   
         TimeOut = Now + TimeValue("00:00:05")  '-- wait 1 second for above navigation to take effect
      Do While Now < TimeOut
         DoEvents
      Loop
   
    Set frm = QPR.Document.Forms("form1")
        
    Set Start = frm.all("SKPI_SEARCH_START_DATE_KEY")
    Start.Value = "01/01/" & Year(Now)
    
    Set Finish = frm.all("SKPI_SEARCH_END_DATE_KEY")
    Finish.Value = Format(Now - 1, "mm/dd/yyyy")
    
    Set drp2 = frm.all("SKPI_SEARCH_NC_TYPE_KEY")
    drp2.Item(1).Selected = True
    
    Set drp1 = frm.all("SKPI_SEARCH_NAMC_KEY")
    drp1.Item(p1).Selected = True
    
    Set src1 = frm.all("Submit")
    
    src1.Click
    
    TimeOut = Now + TimeValue("00:00:05")  '-- wait 1 second for above navigation to take effect
      Do While Now < TimeOut
         DoEvents
    Loop
        
    QPR.navigate ("[URL]https://www.portal.toyotasupplier.com/skpi/DownloadNCPartListServlet[/URL]")
    
    TimeOut = Now + TimeValue("00:01:00")  '-- wait 1 minute for above navigation to take effect
    Do While Now < TimeOut
         DoEvents
    Loop
  
   
End Function

Any Ideas how I could save the .xls?
 

jal

Registered User.
Local time
Today, 13:07
Joined
Mar 30, 2007
Messages
1,709
Jal,
My code works great to download the data into the excel file.

However, I need to be able to save the file in excel in order to be able to import it. Currently, I just get an opened file.


Does yours save the file?
Yes. I did it in Access and it saved the file. That's what I was trying to tell you - and why I am so confused as to why it isn't working for you.
 

jal

Registered User.
Local time
Today, 13:07
Joined
Mar 30, 2007
Messages
1,709
Let's make sure we are on the same page here (no pun intended). I'm guessing that, for this to work, it's not enough for the web page to merely "be a page that displays an excel file". Rather, the URL must be the address of the excel file, in other words the URL itself should end in the letters XLS like this:


http.....filename.xls


If you have the other scenario (a web page that displays an excel file), you will probably need a totally different approach - you can probably go into Excel's Macro recorder, and then do Data > Get External DAta > New Web Query and setup to download the part of the web page that displays the excel file - and then have Access run your Excel macro. I've never tried this, just guesssing.
 

ggodwin

Registered User.
Local time
Today, 16:07
Joined
Oct 20, 2008
Messages
112
The webpage location is not hard coded. This is why I have to step through the pages and then enter the search criteria. Basically it steps into a form where I key in the variables and then displays the data. At the bottom of the page there is a button that says "download". The file name is given to the data after the search has been conducted. Therefore, there is no variables included in the address. (That would be nice)

THere is a prompt that ask for the file to be saved or opened. I have never been able to select the "save" option and get it to work.

In Excel it works very well for me. But, I am using the VBA in excel so it is easy. I just tell it the window name and save it.

In XP I can instruct it to open as a native excel file. However, Vista It is only in the IE window. (This may contribute) But, if I run the file in XP I can't get it to work either.

How would I incorporate your code or How did you do it?

If you are interested in this and want my pass word I can pass it to you and you can see how it works.
 

jal

Registered User.
Local time
Today, 13:07
Joined
Mar 30, 2007
Messages
1,709
I probably can't help you because I know nothing about web technology.

How would I incorporate your code or How did you do it?
Just copy and paste the snippet I posted. Your scenario is apparently different, though - you evidently don't have the address of the excel file. Instead you only have the address of a download link? I don't know enough about downloads to help you. When manually downloading files off the internet, in some cases I can right-click the link and select Properties to get the address of the file. In other cases, the link seems to be a redirect to the file, so the Properties only exposes a redirection link address, not the file address. That seems to be your scenario, and I wouldn't know how to solve it.

As far as your password, I would hate to encourage you to start sending passwords to strangers. That's not a good habit to get into. You shouldn't trust anyone on these forums. However, if you are sure that the password exposes no sensitive data, then sure go ahead and send it to me, though I probably can't solve this for you.
 

ggodwin

Registered User.
Local time
Today, 16:07
Joined
Oct 20, 2008
Messages
112
I actually have created dummy accounts for debugging this problem. Which is the only way I can give it out. I can then kill it shortly after.

But I think your synopsis of my problem is dead on.
 

ggodwin

Registered User.
Local time
Today, 16:07
Joined
Oct 20, 2008
Messages
112
This is the craziest thing I have ever seen...

OK. I tried you code above. All I did was change the link address and the save path. I copied and pasted it at the bottom of mine code.

It actually saved something as I asked it to. However, it save (something) before the data was ready to be saved. The Website was still compiling the results when the save occured.
Because of this? (I think) it actually saves a stipped out version of the Websites Home page (Where I enter my username and password)
 
Last edited:

ggodwin

Registered User.
Local time
Today, 16:07
Joined
Oct 20, 2008
Messages
112
Well, I actually got this thing to work.

I put in a hard delay and so far it has worked as expected.

Do you know if I am able to do some arranging to the .xls file before I save it?

Basically I just want to remove all strange Characters out of the heading.
"." and "/" & " ". Basically the period, the slash and the blanks.
 

jal

Registered User.
Local time
Today, 13:07
Joined
Mar 30, 2007
Messages
1,709
Well, I actually got this thing to work.

I put in a hard delay and so far it has worked as expected.
Good work. Glad you got it working.

Do you know if I am able to do some arranging to the .xls file before I save it?

Basically I just want to remove all strange Characters out of the heading.
"." and "/" & " ". Basically the period, the slash and the blanks.
Before you save it? I doubt it. Afterwards should be doable - but you're talking about editing column names, which can be slightly more complicated than editing data. In advance, do you know the column names? i.e. Are they fixed?

If so, transfer the sheet into a temp table:

On error resume next
DoCmd.RunSQl "DROP TABLE TempTable"
On error goto 0

Dim pathToSheet as string, pathToAccess as String
pathToSheet = C:\mySpreadsheet.xls"
pathToAccess = CurrentProject.Path + "\" + CurrentProject.Name
Dim cnExcel As New ADODB.Connection
cnExcel.ConnectionString = "Provider =MSDASQL; Driver={Microsoft Excel Driver (*.xls)}; DBQ=" & pathToSheet
cnExcel.Open
cnExcel.Execute "SELECT * INTO [MS Access;Database=" & pathToAccess & "].[TempTable] FROM [Sheet1$]"
cnExcel.Close

Don't use the asterisk. Instead of SELECT * do a column rename (for each column), like this.

SELECT [oldname1] as NewName1, [oldName2] as NewName2....

and then use DoCmd.TransferSpreadsheet to export the sheet back to disk.

Let me know if this works.
 
Last edited:

ggodwin

Registered User.
Local time
Today, 16:07
Joined
Oct 20, 2008
Messages
112
I am not sure what the reason is but I am starting to get the error again. about 40% of the downloads does not give me the excel data.

It appears to be saving the website login pages under the excel format and I am not sure what is the problem. Is this code trying to control Excel or Internet Explorer?

Most of my errors are coming on my XP machine which does not open the Excel file in the IE Window.

Any ideas how to force in through the Excel Window/Application.
 

Users who are viewing this thread

Top Bottom