Get value from web document (intranet) input element with VBA

Gregor_1

New Member
Joined
Oct 12, 2023
Messages
11
Office Version
  1. 2021
Platform
  1. Windows
0
I'm having trouble getting the value from an input field called "due date"
Here is my HTML and VBA code.
I resolved error 462, but I can't retrieve certain data.
Actually I'm getting an error: Run-time error '91: Object variable or With block variable not set
Error appears in any code starting with TerminPlatnosc

VBA:
[KOD=vba]
Sub Dane_z_www()

Range("A1").Clear

Dim appIE As InternetExplorerMedium
Dim my_url As String
Dim TerminPlatnosci As String
Dim shellWins As SHDocVw.ShellWindows
Dim IE As SHDocVw.InternetExplorer
Dim Doc As HTMLDocument

my_url = "Example Domain"
Set appIE = New InternetExplorerMedium

With appIE
.Visible = True
.Navigate my_url
End With

Set shellWins = New SHDocVw.ShellWindows

For Each IE In shellWins
If IE.Name = "Internet Explorer" Then
Set IEObject1 = IE
Debug.Print IE.LocationURL
Debug.Print IE.LocationName
End If
Next

Set shellWins = Nothing
Set IE = Nothing


Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop

Set Doc = appIE.Document

'TerminPlatnosci = Trim(Doc.getelementsByName("section-36-control$xf-771$xf-784$xml_termin_platnosci-control$xforms-input-1")(0).Value)
'TerminPlatnosci = Trim(Doc.getElementsByTagName("tr")(0).getElementsByName("input id")(0).getAttribute("value"))
TerminPlatnosci = Trim(Doc.querySelector("input[name='section-36-control$xf-771$xf-784$xml_termin_platnosci-control$xforms-input-1']").getAttribute("value"))
Range("A1").Value = TerminPlatnosci

appIE.Quit
Set appIE = Nothing

MsgBox "Daty zostały wczytane", , "Komunikat"

End Sub
[/KOD]

HTML:


The code provides three variants that I used when trying to download the data.

  1. TerminPlatnosci = Trim(Doc.getelementsByName("section-36-control$xf-771$xf-784$xml_termin_platnosci-control$xforms-input-1")(0).Value)
  2. TerminPlatnosci = Trim(Doc.getElementsByTagName("tr")(0).getElementsByName("input id")(0).getAttribute("value"))
  3. TerminPlatnosci = Trim(Doc.querySelector("input[name='section-36-control$xf-771$xf-784$xml_termin_platnosci-control$xforms-input-1']").getAttribute("value"))
Range("A1").Value = TerminPlatnosci
 

Attachments

  • HTML code.jpg
    HTML code.jpg
    152 KB · Views: 13

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi @Gregor_1

Are you sure the HTML source is static like in those three examples? It may be dynamically created contend with JS etc.
Could you provide more details or the link to the website you're trying to scrape.

__________
Dobry wieczór i pozdrawiam :)
 
Upvote 0
Cześć @Gregor_1

Czy jesteś pewien, że źródło HTML jest statyczne, jak w tych trzech przykładach? Może być dynamicznie tworzony, rywalizować z JS itp.
Czy możesz podać więcej szczegółów lub link do strony internetowej, którą próbujesz zeskrobać.

__________
Dobry wieczór pozdrawiam:)
Cześć,
Jestem początkujący i nie wiedziałem, że kod VBA zależy od rodzaju strony. Niestety nie mogę podać linku do tej strony, ponieważ jest to wewnętrzna strona firmowa (intranet). Z uwagi na to, że aplikacja ta służy do dodawania dokumentów przez zwykłych użytkowników, będzie to strona dynamiczna. Jak więc powinien wyglądać kod skrobania daty?

Dziękuję za Twoje wsparcie
 
Upvote 0
Hi @Gregor_1

Are you sure the HTML source is static like in those three examples? It may be dynamically created contend with JS etc.
Could you provide more details or the link to the website you're trying to scrape.

__________
Dobry wieczór i pozdrawiam :)

Click to expand...

Hi,
I'm a beginner and I didn't know that VBA code depends on the type of page. Unfortunately, I cannot provide a link to this page because it is an internal company website (intranet). Due to the fact that this application is used for adding documents by ordinary users, it will be a dynamic page. So what should the date scraping code look like?

Thanks for your support.
 
Upvote 0
The VBA code does not depend on the type of the page, but since the HTML attributes may differ it's barely possible to produce some working code.
If it's dynamic content, the attributes will most likely be created via a script (server or client side, not sure about that) and if you don't know the algorithm or the "logic" I think I cannot help you, sorry.

Maybe there is another approach to your problem, so I ask you to provide as much information as possible.
I can imagine to gather the needed information using JavaScript only injecting some custom JavaScript code to the website that would output the desired data.
What exactly do you wish to accomplish? Does it need to be VBA in Excel?

Please tell me some more details.
 
Upvote 0
The VBA code does not depend on the type of the page, but since the HTML attributes may differ it's barely possible to produce some working code.
If it's dynamic content, the attributes will most likely be created via a script (server or client side, not sure about that) and if you don't know the algorithm or the "logic" I think I cannot help you, sorry.

Maybe there is another approach to your problem, so I ask you to provide as much information as possible.
I can imagine to gather the needed information using JavaScript only injecting some custom JavaScript code to the website that would output the desired data.
What exactly do you wish to accomplish? Does it need to be VBA in Excel?

Please tell me some more details.
Thank you for your willingness to help.
The data (dates) that I want to scrape from this page are needed for further use in Excel. Therefore it must be VBA.
I am attaching a graphic file with a fragment of the code of this page. I have marked in red (bottom) the date I need to scrape. I cannot add a larger code file due to file size limitations.
I checked that the input id="section-36-control$xf-771$xf-784$xml_termin_platnosci-control$xforms-input -1" appears only once in the code.
Obraz 01 minis.jpg
 
Upvote 0
Hi @Gregor_1
are you able to copy the complete HTML source code between those TD tags showed in the screenshot below?

1697998423023.png


I could then try something out to be able to scrape the part you need.

Greetings
 
Upvote 0
Hi @Gregor_1
are you able to copy the complete HTML source code between those TD tags showed in the screenshot below?

View attachment 100791

I could then try something out to be able to scrape the part you need.

Greetings
Hi PeteWright

The attached HTML screenshot includes everything between the TD tags.
I understand you want it in text form instead of a screenshot?
Tomorrow I will be able to post the HTML code as text

Regards,
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,166
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top