HTML VBA does not work on all computers

So, I have this code that I'm working on for my deptrent, which goes to the site, enters data, starts clicks and uploads the csv file to the worksheet. It works great on my PC and in my profile on computers that another department uses. We use the same versions of windows, excel and IE. When I have someone from another department, run the macro, it will open the site, but it will never enter data into the fields, despite the fact that the site is the same code as when entering the system.

Sub testmetric() Dim appIE As Object Dim Obj As Object On Error Resume Next Sheets("Audit").Select Selection.AutoFilter Sheets("Audit").Cells.Clear Application.ScreenUpdating = False Application.ScreenUpdating = True Set appIE = CreateObject("InternetExplorer.Application") sURL = "http://cctools/rportal/main.php?p=agentavaya" ' Instructes the macro to open IE and navigate to sURL. With appIE .Navigate sURL .Visible = True Application.Wait Now + TimeValue("00:00:02") Set HTMLDOC = .Document End With Application.Wait Now + TimeValue("00:00:02") appIE.Document.getElementById("agentLob").selectedIndex = "3" appIE.Document.getElementById("timezone").selectedIndex = "1" appIE.Document.getElementById("sdate").Value = Range("Date_Start") appIE.Document.getElementById("edate").Value = Range("Date_End") appIE.Document.getElementById("stenure").Value = Range("TenStart") appIE.Document.getElementById("etenure").Value = Range("TenEnd") Application.Wait Now + TimeValue("00:00:02") For Each Btninput In appIE.Document.getElementsByTagName("INPUT") If Btninput.Value = " Run " Then Btninput.Click Exit For End If Next Application.Wait Now + TimeValue("00:00:02") Call CSV appIE.Quit Sheets("Audit").Select Range("A1").Select Sheets("audit").Paste End Sub Sub CSV() sCSVLink = "http://cctools/rportal/csvexport.php" sfile = "csvexport.php" ssheet = "Sheet10" Set wnd = ActiveWindow Application.ScreenUpdating = False Workbooks.Open Filename:=sCSVLink Windows(sfile).Activate ActiveSheet.Cells.Copy wnd.Activate Range("A1").Select Sheets("Audit").Paste Application.DisplayAlerts = False Windows(sfile).Close False Application.DisplayAlerts = True Application.ScreenUpdating = True Sheets("Audit").Select Range("A1").Select Sheets("audit").Paste End Sub 

When this code is managed by a member of another department, it simply opens the website, doesn’t enter anything, and does not press the RUN button on the website.

Any ideas on what could be causing this? What setting or something else. I have confirmed that the links to the VBA PC are there, and there is no "Place where there are no paths."

0
source share
2 answers

You never check if the loading of a web page is complete!

Your On Error Resume Next statement also hides any errors and prevents you from taking appropriate measures to correct errors by correcting the code. Send a report indicating the error number and the line that causes the error. I have an idea which line and which error, and my answer is for this:

Type 91 error: Set HTMLDOC = .Document

Why?

While you wait, Application.Wait is a really inefficient way to wait for the browser to load, and 2 seconds may not always be enough time. The best way I can think of dealing with this, assuming you get an error like 91, is like this:

How to make a wait loop to control the browser

Basically you need to put the stream in a loop until the browser loads the page. In pseudo code, you do:

 Do Until Browser Is Loaded DoEvents 'etc. Loop 

Using the WinAPI sleep function

You will need to use the WinAPI Sleep function (many people use DoEvents or Application.Wait , but I found here that Sleep is preferred.

Since this is Declare , you must put it in a regular module; it cannot get into a class module or user form.

 '## This needs to go in an ordinary code module Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 

WinAPI sleep function call

 Sleep 250 'sleep for 1/4 of a second, or Sleep 1000 to sleep for a full second, etc. 

Putting it all together

You will need to put Do ... Loop , which calls this function immediately after the .Navigate method.

  '## This goes in where you do Navigate method: With appIE .Navigate sURL Do Sleep 250 Loop While Not .ReadyState <> 4 'READYSTATE_COMPLETE Set HTMLDoc = .Document End With 

If it still does not work ...

Yes, there is always an exception. I do very little internet automation, and most of what I do is just messing around with stuff to help people. I noticed that more and more sites are served dynamically (perhaps AJAX?), And that in these cases the browser may be .ReadyState = READYSTATE_COMPLETE and even .Busy = False , but .Document is still Nothing .

If this happens, you can put a secondary loop, for example:

 Dim numberOfAttempts Do While .Document Is Nothing Sleep 250 numberOfAttempts = numberOfAttempts + 1 If numberOfAttempts > 100 Then MsgBox "This is taking too long, try again later." Exit Do End If Loop 

You might want to add an iterator / counter, as I did, and simulate a timeout, otherwise this could potentially lead to an infinite loop.

+1
source

Thanks for all the great tips that many of them will implement. The true mistake behind this was the installation of Windows. UAC:

http://windows.microsoft.com/en-us/windows7/products/features/user-account-control By setting it to “never notify” below, he immediately fixed it.

0
source

Source: https://habr.com/ru/post/1234177/


All Articles