Actually Im trying to go through every "class" defined, and get his content (text), I got a code that works fine, but it just take the text from one class, and it doesn't continues with the other one.
Its repeats the action 7 times, but just with the first one class and doesn't show any error or something that can give me a clue of what's wrong on the code.
Im kinda new on this, but is part of my hobby learn about VBA.
Im practicing with https://www.tecnoglobal.cl/tiendaonline/webapp/componentes-pc/placas-madre/246
Here is the code:
Dim bot As New WebDriver, myproducts As WebElements, myproduct As WebElement, i As Integer, productnum As String, clicknum As Integer, mysheet As Worksheet
i = 2
Set mysheet = Sheets("example2")
Set myproducts = bot.FindElementsByXPath("//h1[#class='minificha__nombre-producto']")
For Each myproduct In myproducts
If myproduct.FindElementByXPath("//span[#class='minificha__sku ng-binding']").Text <> "" Then
mysheet.Cells(i, 1).Value = myproduct.FindElementByXPath("//span[#class='minificha__sku ng-binding']").Text
i = i + 1
End If
Next
MsgBox ("complete")
End Sub
Thanks you very much in advance.
I think you want following sibling. The element you are after is not a child of myproduct. It is the adjacent sibling (same level next element).
myproduct.FindElementByXPath("following-sibling::span").Text
Related
I'm so close to getting this code working, I just need a little push please. I would like to
take the name of a combo box and then add a string to the end, But then get the value of a textbox with that string. This is to create a dynamic function instead of pasting the same code over and over.
Here's what I have so far, after you select something in the dropdown, the data is then pulled to populate the boxes next to it. I have about 8 drop downs so far so that's why I need this to work.
'Combobox after update
Call GrabData(Me, Me.ActiveControl)
Then
Private Sub GrabData(ctl As Control)
'name of ctl/combobox is "Kitchen"
data1 = (ctl.Name & "Size") '"KitchenSize"
'Here is where it all goes wrong
data1.Value = size.value
'size.value is just a textbox for example
End Sub
I can debug this with:
msgbox(data1)
'outputs "KitchenSize"
But I cannot get the value of kitchensize's textbox with data1.value
Error:
Object Required
I have also added Dim As String / Dim As Control.
I will be assigning the variable to some other stuff in this 50 line code I wrote so please don't take the above example as exactly what I intend to do, I just need help appending the ctl.name to a string, then use that to reference another control and so on.
EDIT
For anyone who wants to know, I figured it out.
Dim Ctrl As Control
Dim CtrlName As String
CtrlName = ctl.Name & "Size"
Set Ctrl = Me.Controls(CtrlName)
Ctrl.Value = 'Wherever you want to send the values to
See the edit.
You need to dim it as a string, then use Set Ctrl
Sub initialize()
For boxNum = 1 To 10
vaultValuesForm.Controls("h" & boxNum).Value = ""
vaultValuesForm.Controls("d" & boxNum).Value = ""
Next boxNum
vaultValuesForm.Show
End Sub
Sub button_Populate_Click()
Dim array_h(9) As String, array_d(9) As String
For boxNum = 0 To 9
array_h(boxNum) = vaultValuesForm.Controls("h" & (boxNum + 1)).Value
array_d(boxNum) = vaultValuesForm.Controls("d" & (boxNum + 1)).Value
Next boxNum
Call populateTable(array_h(), array_d())
End Sub
Sub populateTable(array_0() As String, array_1() As String)
For x = 1 To 4
ThisDocument.Bookmarks("bd" & x).Range.Text = array_0(0)
Next x
End Sub
I have tested the functionality of this code at various points, and it works flawlessly right up until this line:
ThisDocument.Bookmarks("bd" & x).Range.Text = array_0(0)
Specifically, until it reaches = array_0(0). In its current state, reaching this point in the Sub results in "Run-time error '5941': The requested member of the collection does not exist." Same deal when I originally tried using = array_0(x) (which is ultimately what I'm trying to accomplish). However, if replaced with something direct such as = "AA", it works. How do I phrase this bit properly to set the bookmark values to those within the array?
Note: In case you're wondering, the arrays are being referenced and passed properly; I tested this by changing the loop to comments and using MsgBox() with various array elements.
The answer from comments. The issue I wasn't aware of was that the bookmarks were being deleted after running the module, so it wouldn't work again unless the bookmarks were created again.
Are you sure the bookmarks bd1...bd4 are still there in the document? Because a bookmark's range.text deletes the bookmark, so if you want to be able to repeat the bookmark text assignments you have to recreate the bookmarks after assigning the texts. FWIW I ran your code and it was fine when bd1..bd2 etc. existed but threw 5941 the next time. (This is quite a common problem!) – slightly snarky Sep 3 at 8:37
So, for the official answer to my question, the way I had done it initially is how; it just couldn't be repeated.
I have a form which is partially filled in automatically from tables. There is a combo, where the number of transaction is chosen and then there is a textbox, where I want to fill in the partner name (searched in sheets).
I have spend some long time to figure out what am I having wrong in my code. In the end I managed the code work, but it looks very mysterious for me and it's not clean.
The original code:
Private Sub ComboTransaction_Change()
Dim ws_su As Worksheet
Set ws_su = Worksheets("Sale Unsettled")
TextPartner = ws_su.Range("SaleUnsettled_Start").Offset(Application.WorksheetFunction.Match(Val(ComboTransaction), ws_su.Range("SaleUnsettled_Transactions"), 0), 1)
End Sub
The "solution":
Private Sub ComboTransaction_Change()
Dim ws_su As Worksheet
Set ws_su = Worksheets("Sale Unsettled")
PartnerValue = ws_su.Range("SaleUnsettled_Start").Offset(Application.WorksheetFunction.Match(Val(ComboTransaction), ws_su.Range("SaleUnsettled_Transactions"), 0), 1)
TextPartner = PartnerValue
End Sub
Why I have to do it via the PartnerValue, that's a mystery for me. Not only it bothers me because it's messing up the code (there are more values which I have to fill in the same way), but I also have another forms (Purchase etc), where it works without this strange patch.
I would like to get rid of if so if you have any idea what's wrong, I will appreciate your message.
The only difference between TextPartner and PartnerValue is that TextPartner is a TextBox in the form and PartnerValue isn't.
The Run-time error means that it is a Type mismatch.
Try to use
TextPartner.Text = ws_su.Range("SaleUnsettled_Start").Offset( _
Application.WorksheetFunction.Match(Val(ComboTransaction), _
ws_su.Range("SaleUnsettled_Transactions"), 0), 1)
I create a browser like so, and manually navigate to the web page I need to be. I intend to automatically pull certain elements once I get to the page I need to be on via a seperate macro
Sub Test()
Set CAS = New SHDocVw.InternetExplorer ' create a browser
CAS.Visible = True ' make it visible
CAS.navigate "http://intraneturl"
Do Until CAS.readyState = 4
DoEvents
Loop
This works fine, then I do
Public Sub Gather
Set HTMLDoc2 = CAS.document.frames("top").document
Call Timer1
With HTMLDoc2
.getElementById("tab4").FirstChild.Click
End With
Call Timer2
Dim fir, las, add1, add2, cit, stat, zi As String
Dim First As Variant
Dim Last As Variant
Dim addr1 As Variant
Dim addr2 As Variant
Dim city As Variant
Dim Thisstate As Variant
Dim Zip As Variant
Call Timer2
Set HTMLDoc = CAS.document.frames("MainFrame").document
Call Timer2
With HTMLDoc
First = .getElementsByName("IndFirst")
Last = .getElementsByName("IndLast")
addr1 = .getElementsByName("txtAdd_Line1")
addr2 = .getElementsByName("txtAdd_Line2")
city = .getElementsByName("txtAdd_City")
Thisstate = .getElementsByName("cmb_Add_State")
Zip = .getElementsByName("txtAdd_Zip")
End With
fir = First.Value
las = Last.Value
add1 = addr1.Value
add2 = addr2.Value
cit = city.Value
stat = Thisstate.Value
zi = Zip.Value
'navigate back to start page
With HTMLDoc2
.getElementById("tab3").FirstChild.Click
End With
End Sub
This works the first time, but after the first time, I get "Object variable or with block variable not set" when trying to run the gather() sub again, on a different web page that contains similar information. Any Ideas as to what im doing wrong?
"The error "object variable or with block variable not set" occurs on: Set HTMLDoc2 = CAS.document.frames("top").document the second time i try running Gather()."
This is probably one of three things:
CAS is no longer an object
To check this, set a breakpoint on the line, press ctr+G in the VBA Editor and type ?CAS Is Nothing in the Immediate Window; the result should be False; if it is True CAS is no longer an object
Like Daniel Dusek suggested, make sure CAS.document.frames("top") is an actual element on the page.
To check this, open the webpage you are trying to script, press F12 in Internet Explorer, click on the arrow in the toolbar and click on the "top" frame element in the webpage, switch back to the Developer Tool and look at the line highlighted. Make sure the frame element is named "top".
The HTML hasn't fully loaded when you try to reference the frame element. Set a longer delay or a loop.
i.e. (untested):
Do Until HtmlDoc2 Is Nothing = false
Set HTMLDoc2 = CAS.document.frames("top").document
Loop
Maybe the more important question is why manually navigate to another page? Can't you automate that part of your process too?
I have this short piece of code
Public n As Integer
Public Sub Foo()
For i = 0 To 4
MyModule.n = MyModule.n + 1
Next i
End Sub
which is defined in a Module named MyModule. This code is working as expected: After executing for the first time 'MyModule.n' has the value 5. After executing the second time it has the value 10 and so on.
When I extend the code, to add a CommandButton and place it onto the working sheet, the global variable MyModule.n loses it's value on every new call of Foo:
Public n As Integer
Public Sub Foo()
Dim btn As OLEObject
For i = 0 To 4
Set btn = Worksheets("Aufträge").OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Left:=122, Top:=321, Width:=30, Height:=30)
MyModule.n = MyModule.n + 1
Next i
End Sub
The code seems to work because the command button is created and placed correctly. Why does the global variable reset if executing the second code fragment?
Furthermore I can't place a break point after or inside the For-Loop in the second code fragment. I get the message Can't enter break mode at this time.
Based on the search I did & my conclusion is that you can't add controls dynamically to the worksheet and retain the state of variables.
Here is why: Adding a button will force the sheet to goto design mode & hence reset of variables.
Supporting links
http://www.pcreview.co.uk/forums/dynamically-adding-activex-controls-via-vba-kills-global-vba-heap-t3763287p2.html
https://web.archive.org/web/20101215134333/http://support.microsoft.com/kb/231089 (originally //support.microsoft.com/kb/231089)
You don't need to use a for next loop for the first example. Why don't you do that :
Public n As Integer
Public Sub Foo()
MyModule.n = 5
End Sub
This will do the thing in one operation.
In the second example you don't add one button, you add 5 of them. Check the help fo "For Next".