Dim checkon, check
checkon = 1
toggle = 0
Do While checkon = 1
If (Chr(window.event.keyode = "m")) and toggle = 0 Then
toggle = 1
MsgBox "working"
End If
If (Chr(window.event.keyode = "m")) and toggle = 1 Then
toggle = 0
End If
Do While toggle = 1
If window.event.button = 1 Then
MsgBox "working"
WshShell.Sendkeys ("-{F10}")
WScript.Sleep 500
End If
Loop
Loop
The goal of this is to have it be run, and constantly check for the user to press M, which alternated toggle between 0 and 1. When toggle is true, the program is supposed to check for the mouse being held down and if it is click twice a second.
Currently it's giving an error
Object required:'Window"
and before that it did simply nothing. I am aware the code looks ugly, but I pasted msgboxes as a method of debugging what's going wrong, and none of them activated.
From what I read, you can't listen to events like that in a VBScript CScript environment. Usually window.event would imply the presence of a web page, but you said you're running this from the desktop, so perhaps you copied the code from a bad example. You may need to use another language or tool to do this.
Related
My Excel UserForms contain a variety of objects, including text boxes, combo boxes, radio buttons, etc. The UserForm and the objects on the UserForm shrink and expand when my laptop is on a docking station and the VBA window is open on a larger monitor.
When I access the UserForm editor from the Forms tab in VBA, I can drag the UserForm resize handles and the objects in the UserForm will immediately snap back to their original state, but I want to do this programmatically so that the end user will not deal with shrunken/expanded UserForms.
I have tried resizing the UserForm upon opening (UserForm_Initialize), but it seems as if the shrinking/expanding takes place while the UserForm is not active, meaning that my UserForm resizing only acts to return the UserForm to its shrunken/expanded state and not its original state.
Sub UserForm_Initialize()
Call ResizeUserform(Me)
End Sub
Sub ResizeUserform(UserForm_Name As Object)
UserForm_Name.Width = UserForm_Name.Width + 0.001
UserForm_Name.Width = UserForm_Name.Width - 0.001
UserForm_Name.Height = UserForm_Name.Height + 0.001
UserForm_Name.Height = UserForm_Name.Height - 0.001
End Sub
Don't leave your form's dimensions ambiguous or prone to logic circularity (i.e. as a function of itself); set them up before loading/showing.
i.e.:
'where XX and YY are integer constants:
With YourFormName
.width=XX
.height=YY
.show
end with
If you absolutely need to incur in circular statements, do it indirectly by storing your calculated variable in a global/local variable, and then proceed to declare its properties (i.e. YourFormName.width=variable)
Good luck!.
I had a similar issue, ever time the program opened the Login user form would be smaller than the last time. It only did it on my lap top with extra monitors. I finally used
Private Sub UserForm_Initialize()
With Userform
.Width = Application.Width * 0.3
.Height = Application.Height * 0.6
End With
End Sub
in the userforms code and it kind of stopped meaning it no longer showed the user the change but if I open the VBA it had changed size on the Height and Width but since it was only in the VBA and the user didn't have to try and enter a password in a mini box its fine.
I had a similar problem. The only thing that worked for me was going into the Advanced Options of Excel and checking the box, "Disable hardware graphics acceleration." I wasted several hours trying to find this. I hope this helps someone else!
Don't make the form maximized. "Maximized" means you don't know at design-time what the size of the form will be, because you're making that depend on what monitor size it's being displayed in.
Larger monitor = larger maximized form, that's by design.
If you don't want the form to resize automatically when it's displayed in a different-size monitor, then don't maximize it.
Alternatively, handle the form's Resize event, and programmatically move each control where it needs to be relative to the bottom-right edge. Note, that's tricky and extremely annoying code to write, especially if the form is any kind of complicated. Much simpler to just not have a maximized form.
this operation can also be done with a loop. I used the button to increase and decrease the height of userform. I created a loop and assigned it to the button.At the same time there was a nice animation.
You set a height value, when the button is pressed, the userform becomes longer if the height is less than this value, and the userform becomes shorter if it is larger.
Codes:
Private Sub CommandButton4_Click()
Dim X, d, yuk, mak As Integer
For X = 1 To 100
DoEvents
If e = 0 Then
d = d + 10
yuk = 242
mak = 342
Else
d = d - 10
yuk = 345
mak = 245
End If
UserForm2.Height = yuk + d
If UserForm2.Height >= mak And e = 0 Then GoTo 10
If UserForm2.Height <= mak And e = 1 Then GoTo 20
Next
10 CommandButton4.Caption = "<"
e = 1
ListBox1.ListIndex = 0
ScrollBar1_Change
Exit Sub
20 CommandButton4.Caption = ">"
e = 0
ListBox1.ListIndex = 0
End Sub
It's video :https://www.youtube.com/watch?v=1LCxgQGy9tU
Source and Example file here
Good Morning,
I’m hoping that some kind soul out there can help me with a roadblock I’ve encountered in my quest to manipulate a website with VBA. I am using MS Excel 2010 and Internet Explorer 11.0.56.
I’m somewhat comfortable with VBA but have never used it to navigate to a website, enter information and click on buttons. I’ve managed to muddle through as follows:
In Column A of my Excel spreadsheet, I have a list of 10 digit case numbers.
The code below will open IE, navigate to the desired website, pause while I log in, then navigate to the search screen, enter in the first case number and press the SEARCH button (yes, I have the case number in this example hard coded in with no looping, but that stuff I can handle so please ignore):
Sub Button_Click()
Dim objIE As Object
Set objIE = New InternetExplorerMedium
objIE.Top = 0
objIE.Left = 0
objIE.Width = 800
objIE.Height = 600
objIE.AddressBar = 0
objIE.StatusBar = 0
objIE.Toolbar = 0
objIE.Visible = True
objIE.Navigate ("https://somewebsite.com")
MsgBox ("Please log in and then press OK")
objIE.Navigate ("https://somewebsite.com/docs")
Do
DoEvents
Loop Until objIE.ReadyState = 4
objIE.Document.all("caseNumber").Value = "1234567890"
objIE.Document.getElementById("SearchButton").Click
Exit Sub
Do
DoEvents
Loop Until objIE.ReadyState = 4
MsgBox ("Done")
End Sub
That will bring me to this screen
The file number entered in the search field will return any number of files in a dynamic table with a checkbox to the left of each file.
For this example, let’s say I am ONLY concerned with the file called “CC8” under the “Type” column. There will only ever be one instance of “CC8” for a given file number.
What I need help with is, through VBA, how do I search through this table, find the “CC8” line, and then have the checkbox to the left automatically checked?
When I inspect the “CC8” element in IE, this is the HTML associated with it (highlighted in gray; the entire table is under class “listing list-view clearfix”)
see here
The HTML for the checkbox related to the “CC8” item is below:
HTML code here
The “id” for both has the same sequence of numbers, but one starts with “viewPages” and the other “doc”.
Can anyone help me out as to what I need to add to my code to get this checkbox checked? Thank you!
Note:
Please post the actual HTML using the snippet tool.
Generally:
Without HTML to properly test, I am assuming that the following 2 nodeLists are the same length, meaning that when the search text is found in aNodeList then the assumption is the same index can be used to target the corresponding checkbox in the bNodeList:
Dim aNodeList As Object, i As Long
With objIE.document
Set aNodeList = .querySelectorAll("a[target='_blank']")
Set bNodeList = .querySelectorAll("[title='Search Result: Checkbox']")
End With
For i = 0 To aNodeList.Length - 1
If aNodeList.item(i).innerText = "CC8" Then
bNodeList.item(i).Click
Exit For
End If
Next
You could also potentially use the following instead as you say the viewPages prefixes each item:
Set aNodeList = .querySelectorAll("a[id^='viewPages']")
Other observations:
Traditional checkboxes would have a checked attribute and syntax of
bNodeList.item(i).Checked = True, but as I can't see that attribute in your element I am assuming a .Click suffices.
As I tried to create a automation for my company I stumbled across difficulty I cannot surpass. I have read many articles on this and other sites however I did not found answer.
So basically I have a dropdown list triggered by click on internet application my company uses. User has to change value from "Read" to "All" for 10 positions. And then application acknowledges the change and allows for form to be saved.
As I managed to write a code which clicks on specific field to activate a dropdown, and choose correct option from this dropdown. However when I loop it through all of those 10 dropdowns, not effect is visible.
There is a funny part. Whenever I put a breakpoint on "debug.print objinputs.outerHTML" and then allow for macro to continue the changes takes places and everything is allright. But without breakpoint something is wrong and all the values goes back to "Read". Have anyone knows what might be the issue here?
If you need any more informations please let me know.
Set ifrm = Nothing
Do Until ifrm.Length > 0
Set ifrm = IE.Document.getElementsByTagName("iframe")(0).contentDocument.getElementsByTagName("iframe")(1).contentDocument.getElementsByTagName("iframe")(0).contentDocument.getElementsByTagName("iframe")(0).contentDocument.getElementsByTagName("tbody")(znacznik - 1).Document.getElementsByTagName("td")
Loop
Counter = 1
For Each ele In ifrm
Worksheets("Test").Cells(Counter, 1) = ele.outerHTML
Counter = Counter + 1
If InStr(ele.outerHTML, "<td title=" & Chr(34) & "Read") Then
ele.Click
Do Until Not ele.Busy And ele.readyState = READYSTATE_COMPLETE: Loop
Set objInputs = Nothing
Set objInputs = IE.Document.getElementsByTagName("iframe")(0).contentDocument.getElementsByTagName("iframe")(1).contentDocument.getElementsByTagName("iframe")(0).contentDocument.getElementsByTagName("iframe")(0).contentDocument.getElementsByTagName("tbody")(znacznik - 1).Document.getElementsByTagName("select")(3)
objInputs.Value = "All"
Debug.Print objInputs.Value
Do Until Not IE.Busy And IE.readyState = READYSTATE_COMPLETE: Loop
End If
Next
I found a solution.
It is not the most elegant one but it works and is correct enough for my needs.
I have added
CreateObject("WScript.Shell").Popup "Just Wait", 1, "Waiting"
after value change and it did the trick. I would love to know why does it happen, and why does this "trick" solves the issue.
I want to delete all (= a variable number of) custom sorting lists in vba excel, but I can't find the right syntax anywhere.
I know of two options:
1 write a command that automattically deletes all custom sortlists, this was my attempt at it:
Application.DeleteCustomList ()
2.a Write a command that returns the number of custom sortlists = b
2.b And then remove the custom sortlists manually with:
2.b.1:
for del = 0 to b
Application.DeleteCustomList (del)
next del
2.b.2:
for del = 1 to b
Application.DeleteCustomList (del)
next del
2.c
For delete_lists = 100 To 0 Step -1
On Error Resume Next 'delete_list
Application.DeleteCustomList (delete_lists)
Next delete_lists
Does anybody know a(ny) functioning way to achieve the goal?
Kind regards.
Your problem is that Excel doesn't want you to delete the basic, existing custom lists. There are 4 of them on my Excel - but that may differ from one version to the next.
(Days of the week, months, etc... if you go to "File", "Options", "Advanced options", then at the bottom "Edit customs lists")
I think you safest bet is to resume next upon error:
Sub test()
On Error Resume Next
Dim i As Long
For i = 1 To Application.CustomListCount
Application.DeleteCustomList (i)
Next i
On error GoTo 0
End Sub
Or go to errHandler if you have some error handling procedure - because otherwise it may resume next for your whole script which may not be what you want.
HI I am having trouble getting this code to work properly. Here is the code:
property firstRow : 2051
property lastRow : 5584
set r to firstRow
-- highly recommended
-- close all of Safari's windows before...
tell application "Safari"
close windows
end tell
-- loop through the given row numbers
repeat until r is (lastRow + 1)
-- get the search value for Safari auto completion (column J)
try
tell application "Microsoft Excel"
tell active sheet
set searchTerm to string value of range ("J" & r) of active sheet
end tell
end tell
on error
-- no document open, exit the script
return
end try
-- open Safari and make a new window
tell application "Safari"
activate
make new document with properties {URL:""}
delay 0.5
set pageLoaded to false
end tell
-- type the search value into the address field and hit return (aka select and open the first proposal)
tell application "System Events"
-- here with Safari 6.1 text field 1 of group 2 of tool bar 1 of window 1 points to the URL field
set focused of text field 1 of group 2 of toolbar 1 of window 1 of process "Safari" to true
delay 0.5
keystroke searchTerm
delay 1.5
keystroke return
end tell
-- let open Safari the suggested web page and read out the finally used URL
tell application "Safari"
repeat while not pageLoaded -- keep doing this loop until loading complete
delay 5
if (do JavaScript "document.readyState" in document 1) is "complete" then
set pageLoaded to true
else
-- not sure if this second else is needed, why not just wait until the first access has finished...
-- close document 1
-- make new document with properties {URL:""}
-- tell application "System Events"
-- delay 1.5
-- set focused of text field 1 of group 2 of tool bar 1 of window 1 of process "Safari" to true
-- delay 1.5
-- keystroke searchTerm
-- delay 1.5
-- keystroke return
-- end tell
end if
set thisULR to "NO SITE"
end repeat
try
set thisURL to URL of document 1
on error
set thisURL to "NO SITE"
end try
close document 1
end tell
-- write the result into the cell next to the key word (column K)
tell application "Microsoft Excel"
if thisURL ≠ "NO SITE" then
tell active sheet
make new hyperlink of cell ("K" & r) with properties {address:thisURL, name:thisURL}
end tell
else
tell active sheet
make new cell ("K" & r) with properties {name:"NO SITE"}
end tell
end if
end tell
set r to r + 1
end repeat
I am having trouble getting the code to not crash if there is no URL saved as variable thisURL.
However, it is still crashing. It often says thisURL is not defined and then it stops the script from going to the next r value instead of adding "NO SITE" to the cell. Not sure why its not working.
It was a big chaos with all your end telland end ifetc. Another thing is that I don't understand why you need the second nested repeat-loop...
But I think I figured it out: You want to
read a value from an excel sheet
pretend to type the read out value into Safari's address bar
use the autofill and read out the URL of the found site
write the result into the adjacent excel cell
After your post edit I edited the code to this:
property firstRow : 62
property lastRow : 5584
set r to firstRow
-- highly recommended
-- close all of Safari's windows before...
tell application "Safari"
close windows
end tell
-- loop through the given row numbers
repeat until r is (lastRow + 1)
-- get the search value for Safari auto completion (column J)
try
tell application "Microsoft Excel"
tell active sheet
set searchTerm to string value of range ("J" & r) of active sheet
end tell
end tell
on error
-- no document open, exit the script
return
end try
-- open Safari and make a new window
tell application "Safari"
activate
make new document with properties {URL:""}
set pageLoaded to false
end tell
-- type the search value into the address field and hit return (aka select and open the first proposal)
tell application "System Events"
-- here with Safari 6.1 text field 1 of group 2 of tool bar 1 of window 1 points to the URL field
set focused of text field 1 of group 2 of tool bar 1 of window 1 of process "Safari" to true
keystroke searchTerm
delay 1.5
keystroke return
end tell
-- let open Safari the suggested web page and read out the finally used URL
tell application "Safari"
try
repeat while not pageLoaded -- keep doing this loop until loading complete
delay 10
if (do JavaScript "document.readyState" in document 1) is "complete" then
set pageLoaded to true
end if
end repeat
set thisURL to URL of document 1
close document 1
on error
set thisURL to "NO SITE"
try
close windows
end try
end try
end tell
-- write the result into the cell next to the key word (column K)
tell application "Microsoft Excel"
if thisURL ≠ "NO SITE" then
tell active sheet
make new hyperlink of cell ("K" & r) with properties {address:thisURL, name:thisURL}
end tell
else
tell active sheet
make new cell ("K" & r) with properties {name:"NO SITE"}
end tell
end if
end tell
set r to r + 1
end repeat
Greetings, Michael / Hamburg
Here is another solution. I tested with different delay values inside the System Events part and found a better way of getting the URL from Safari. Have a look at the two main parts of the script, everything else don't need changes:
-- type the search value into the address field and hit return (aka select and open the first proposal)
tell application "System Events"
tell process "Safari"
-- give time to prepare the window
delay 0.5
set focused of text field 1 of group 2 of toolbar 1 of window 1 to true
-- give time to prepare the address field
delay 0.5
keystroke searchTerm
-- give time to get the proposals
delay 0.5
keystroke return
end tell
end tell
-- let Safari open the suggested web page and read out the finally used URL
tell application "Safari"
-- setting the default value
set thisURL to "NO SITE"
try
-- 6 tries with 5 seconds pause (-> max. 30 sec.)
repeat 6 times
try
-- give time to load
delay 5
-- try to access the URL, if it is not available, an error occurs and the repeat loop starts again
set thisURL to URL of document 1
-- close the document
close document 1
-- no error till now, exit the repeat loop
exit repeat
end try
end repeat
on error
-- just to be sure: close all windows
try
close windows
end try
end try
end tell
Greetings, Michael / Hamburg