VBA Excel代码-Windows弹出窗口打开或保存
一段时间以来,我一直在寻找我问题的潜在答案,但收效甚微。我发现很多编码参考似乎都绕过了这个问题。所以我决定问这个问题 我有一个简单的编码,可以打开一个网站,登录,访问一个特定的报告,然后下载.xls文件。执行此操作时,windows弹出窗口显示打开或保存文件 我需要的是将文件保存到特定位置,例如c:\datatemp.xls 我找不到成功实现这一目标的方法 这是我的简单代码:VBA Excel代码-Windows弹出窗口打开或保存,excel,vba,Excel,Vba,一段时间以来,我一直在寻找我问题的潜在答案,但收效甚微。我发现很多编码参考似乎都绕过了这个问题。所以我决定问这个问题 我有一个简单的编码,可以打开一个网站,登录,访问一个特定的报告,然后下载.xls文件。执行此操作时,windows弹出窗口显示打开或保存文件 我需要的是将文件保存到特定位置,例如c:\datatemp.xls 我找不到成功实现这一目标的方法 这是我的简单代码: Sub main() Dim ie As Object Dim user As String Dim pwd
Sub main()
Dim ie As Object
Dim user As String
Dim pwd As String
Dim stockrep As String
Dim btn As Object
user = Range("B1").Value 'This is the username derived from the sheet cell
pwd = Range("B2").Value 'This is the password derived from the sheet cell
stockrep = Range("B3").Value 'This is the http of the stock report file from the sheet cel
'Now to open the website in Internet Explorer
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate Url
ie.Visible = True
'wait until the page loads
Do While ie.readyState <> 4
DoEvents
Loop
'Now to enter the username and password, then click enter
With ie.document
.getElementById("userName").Value = user
.getElementById("password").Value = pwd
.getElementById("submitButton").Click
End With
'Pause while it loads
Do While ie.readyState <> 4
DoEvents
Loop
'Continue to the defined stock report page
ie.navigate stockrep
'Wait for the page to load
Do While ie.readyState <> 4
DoEvents
Loop
'Pause to allow the report to load and then click the download xls button
Application.Wait (Now + TimeValue("0:00:30"))
Set btn = ie.document.getElementById("xlsbutton")
btn.Click
'This is as far as I have got. Need to click the Save As button, save to a set location,
'open the file, select the cells, copy and paste, then close the 2nd doc and delete the file
End Sub
Sub-main()
模糊的物体
将用户设置为字符串
作为字符串的Dim pwd
Dim stockrep作为字符串
作为对象的Dim btn
用户=范围(“B1”)。值“这是从图纸单元格派生的用户名
pwd=范围(“B2”)。值“这是从工作表单元格派生的密码
stockrep=范围(“B3”)。值“这是来自工作表cel的股票报告文件的http
'现在在Internet Explorer中打开网站
设置ie=CreateObject(“InternetExplorer.Application”)
浏览网址
可见=真实
'等待页面加载
请稍等,即readyState 4
多芬特
环
'现在输入用户名和密码,然后单击enter
用ie文件
.getElementById(“用户名”).Value=用户
.getElementById(“密码”).Value=pwd
.getElementById(“submitButton”)。单击
以
“加载时暂停
请稍等,即readyState 4
多芬特
环
'继续到已定义的库存报告页面
例如:股票经纪人
'等待页面加载
请稍等,即readyState 4
多芬特
环
'暂停以允许加载报告,然后单击下载xls按钮
Application.Wait(现在+时间值(“0:00:30”))
设置btn=ie.document.getElementById(“xlsbutton”)
点击
“这就是我所能做到的。需要单击“另存为”按钮,保存到设置的位置,
'打开文件,选择单元格,复制并粘贴,然后关闭第二个文档并删除文件
端接头
如果我可以简单地将代码另存为,那么我应该能够完成其余的工作。有什么想法吗