i am unable to refer to the file, opened (not saved) through the dialogue box from the website by executing(F5) the entire program but could able to refer it through F8
我尝试添加Application.Wait Now + TimeValue(“ 00:00:05”),以便执行获得打开文件的时间,但仍然无法正常工作。
我甚至试图通过将等待时间增加到10秒来增加更多时间,但即使这样仍然无法正常工作,有人可以建议如何引用该文件吗?我们通过对话框将其打开了吗? / p>
Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Public Sub AddReference()
ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\SysWOW64\UIAutomationCore.dll"
End Sub
Sub BSEData2()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument ' it refers to html document
Dim HTMLInput As MSHTML.IHTMLElement ' it refers to any element in the document page
Dim HTMLAs As MSHTML.IHTMLElementCollection
Dim HTMLA As MSHTML.IHTMLElement
Dim A As Workbook
Dim B As Workbook
Dim Ws As Worksheet
Set B = ActiveWorkbook
IE.Visible = True
IE.navigate "https://www.bseindia.com/markets/Equity/EQReports/MarketwatchDownloads.aspx"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
Set HTMLAs = HTMLDoc.getElementsByTagName("a")
For Each HTMLA In HTMLAs
'Debug.Print HTMLA.getAttribute("classname"), HTMLA.getAttribute("href")
If HTMLA.getAttribute("href") = "javascript:__doPostBack('ctl00$ContentPlaceHolder1$imgDownload','')" Then
HTMLA.Click
Application.Wait Now + TimeValue("00:00:03")
Dim o As IUIAutomation
Dim E As IUIAutomationElement
Set o = New CUIAutomation
Dim H As Long
H = IE.Hwnd
H = FindWindowEx(H, 0, "Frame Notification Bar", vbNullString)
If H = 0 Then Exit Sub
Set E = o.ElementFromHandle(ByVal H)
Dim iCnd As IUIAutomationCondition
Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Open")
Dim Button As IUIAutomationElement
Set Button = E.FindFirst(TreeScope_Subtree, iCnd)
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
Application.Wait Now + TimeValue("00:00:05")
' facing error at this point when file gets opened & i want to assign it with the variable A
Set A = ActiveWorkbook
'If InStr(sFilename, "MarketWatch") Then
A.Sheets(1).Range("A1").CurrentRegion.Copy
B.Activate
With ThisWorkbook
Set Ws = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
ActiveSheet.Range("A1").PasteSpecial xlPasteValues
Selection.EntireColumn.AutoFit
End With
A.Activate
Application.CutCopyMode = False
A.Close
Exit For
End If
Next HTMLA
IE.Quit
Set IE = Nothing
End Sub
如果整个程序通过F5运行,有人可以建议如何引用该文件吗?