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 секунд, но даже после этого оно не работает. Может кто-нибудь подсказать, как ссылаться на файл, мы открыли его через диалоговое окно?
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?