1

I want to use CallByName in VBA to read a specific data from such webpages. Those webpages have different html structures. In my case, there is a element that I need to refer 2 or 3 parent nodes and get an element with or tags. See the code:

element in all webpages I named MyElem

in one webpage I need this code:

MsgBox MyElem.parentElement.parentElement.parentElement.getelementsbytagname("tr")(3).innertext

in another webpage I need this code:

MsgBox MyElem.parentElement.parentElement.getelementsbytagname("div")(2).innertext

and so on ...

I want to write in VBA as below:

Select Case Webpage
     Case "webpage_1"
        property ="parentElement.parentElement.parentElement.getelementsbytagname("tr")(3).innertext"
     Case "webpage_2"
        property = "parentElement.parentElement.getelementsbytagname("div")(2).innertext"

      ' and so on ...

 End Select

MsgBox CallByName(MyElem, property, VbGet)

The problem is that CallByName don't support multiple levels of properties. I read similar topic here but it doesn't help to my case. Is there any idea?

AliM67
  • 123
  • 12
  • It would help if you share HTML samples and expected outcomes. There may be a better way. And as you already have a Select Case why not just put your selector direct in there? – QHarr Dec 05 '18 at 22:04
  • Unfortunately the webpages are in private. Your idea about css selector is good and I think it is better. THANKS! – AliM67 Dec 05 '18 at 22:10

2 Answers2

1

Why not this?

Dim res

Select Case Webpage
     Case "webpage_1"
        res = MyElem.parentElement.parentElement.parentElement.getelementsbytagname("tr")(3).innertext
     Case "webpage_2"
        res = MyElem.parentElement.parentElement.getelementsbytagname("div")(2).innertext

      ' and so on ...

 End Select

MsgBox res
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • I can't use this way, because the element "MyElem" created after Case. – AliM67 Dec 05 '18 at 23:31
  • 1
    So create it before ? Or wrap the select in a function, passing in MyElem as an argument. – Tim Williams Dec 05 '18 at 23:38
  • I should try it. Is there any way to use CallByName to set an object to another? Like this: `Set obj = CallByName(MyElem,property,VbSet)` If it is possible I can make final object which has multiple levels of properties. – AliM67 Dec 05 '18 at 23:46
  • Yes - it would be `Set obj = CallByName(MyElem,property, VbGet)` – Tim Williams Dec 06 '18 at 00:36
0
Function PropDotVal(wObjName$, PropNa$)

Dim wObj As Object: Set wObj = ObjFromStr(wObjName)

' ObjFromStr in module with lines like ' there has got to be an inbuilt vba function that does not need many case functions ' str obj 'Case "sTable": Set ObjFromStr = sTable ' ShapePlus 'Case "WHCages": Set ObjFromStr = WHCages ' spt

Dim Si%, NaA$() PropDotVal = "NA" On Error GoTo NotAvailable NaA = Split(PropNa, ".")

If UBound(NaA) > 0 Then

  ' PropNa format multi like  wObj.Objb.objc  etc  .prop
  '  convert wObj as  FredsFarm      with     PropNa as TopEnd.fill.backcolor.rgb
  ' to wobj as FredsFarm.TopEnd.fill.backcolor
  'and  Nsa(si) as "rgb" to CallByName from it

  For Si = 0 To UBound(NaA) - 1
     Set wObj = CallByName(wObj, NaA(Si), VbGet)
  Next Si

End If

PropDotVal = CallByName(wObj, NaA(Si), VbGet) NotAvailable:

End Function

enter code here
Harry S
  • 481
  • 6
  • 5