Searching for DATA (VBA+Web) - excel

I'm trying to create a macro that fetch for specific DATA in URL from Excel sheet and when the DATA is found , it copy the values and past it into the Excel File andmove on to the next DATA
I'm a little bit new with SCRAPPING WEB via EXCEL VBA so Anyone can light me and helps me to carry on ?
I made a lot of searches here in stack over flow but I didn't understand too much
I will explain below with Image what I want to Do and I will show My code which is nothing :(
First URL to use to access to MP :http://XXXX-XXXXX.eu.airbus.XXXX:XXXXX/XXXX/consultation/preSearchMP.do?clearBackList=true&CMH_NO_STORING_fromMenu=true
then Take a value from EXCEL FILE and put it in that Path to make search and tape enter
The last Picture IT will loop for MOD and Copy Value and Paste it into Excel File
Can Anyone guide me please with something
this is my first Code
Dim str As String: str = ThisWorkbook.Sheets("Feuil1").Range("A1").Value
Dim myURL as String
' The & symbol concatenates strings. The _ symbol is for line continuation.
myURL =
"http://Confidential.eu.airbus.Confidential:Confidential/Confidential/" _
& "consultation/preViewMP.do?" & str

If you have your URL then the next stage is to send it. To send it you need something to act as a connector, like Internet Explorer, or a background connector without the browser like XMLHttpServer objects - THIS is a good guide to hopefully get you started.

Related

Excel Hyperlink function to open a specific page

Could anyone from this group can offer some advice on the problem I'm trying to solve? I was attempting to open a PDF document to a specific page using the Excel Hyperlink function. I tried to solve this mystery but to no avail. Hope someone can provide me their thoughts and ideas.
I tried Google and Youtube but only few were related to my issue and the solution discussed was not working on my situation... or maybe i missed something. This will be a great help for me once the issue is solved. Thank you in advance.
Using a macro enabled workbook you can hi-jack the hyperlink open event but it isn't as dynamic as we would hope it to be. Unfortunately, we cannot cancel any followed hyperlink using Worksheet_FollowHyperlink so we need to find a way around following a direct hyperlink.
We can create a hyperlink which goes nowhere by referencing itself then set the text to display as "Follow Link". Because the cell reference is then in the hyperlink, we can use an offset from that to get the desired address and sub address from the column before it; which then allows us to open the PDF at the desired place.
Unfortunately, the hyperlink if copied down will still reference the original cell, so each link would need to be edited separately. To date I haven't found a way to use the HYPERLINK function successfully.
The hyperlink format in the cell to the left would need to be the full path appended with "#page=" and the relevant page; e.g. C:\links\blah.pdf#page=6
In the worksheet module
Const AcrobatReader = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.TextToDisplay = "Follow Link" Then
Dim Ref As String: Ref = Range(Target.SubAddress).Offset(0, -1).Text
Dim URL() As String: URL = Split(Ref, "#page=")
If UBound(URL) > 0 Then Call OpenPDFtoPage(URL(0), CLng(URL(1)))
End If
End Sub
Function OpenPDFtoPage(FilepathPDF As String, page As Long)
Dim Path As String: Path = AcrobatReader & " /A ""page=" & page & """ " & FilepathPDF
Shell Path, vbNormalFocus
End Function
The AcroRd32.exe path may need updating for specific installations
Maybe having just one "Follow Link" hyperlink where the URL in the cell next to it is more dynamic may allow this to be a bit more useable.

Reference cell values in Json.Document() - EXCEL Data sources

I used the From Web function in EXCEL to load a a JSON document from the Web.
If I then Show Query, EXCEL has generated this call:
Json.Document(Web.Contents("http://blah.com/stuff"))
I'd like to edit that so rather than hard coding "http://blah.com/stuff" I'd like to pull the address from a cell in a WorkSheet.
Can anyone help me with what to put here?
Json.Document(Web.Contents(<<>>))
Give the spreadsheet cell with your URL a Name (using Name Manager) eg MyUrl
Insert a line above your Json.Document call (don't forget the comma at the end)
JsonURL = Excel.CurrentWorkbook(){[Name="MyUrl"]}[Content]{0}[Column1],
Then amend the next line to:
Json.Document(Web.Contents(JsonURL))

Using user input in VBA code and extracting data into workbook

I am exploring webscraping to try and improve efficiency when inputting data. Unfortunately the website I wish to extract data from is now in tabular format and so I wish to use VBA to manipulate the website to the desired result.
I'm not very familiar with coding/VBA but so far I have got VBA to open a website and search for a provided value. In this case the CAS number 67-64-1 refs Acetone on the website.
The code for this is:
Sub BrowseToSite()
Dim IE As New SHDocVw.InternetExplorer
IE.Visible = True
IE.Navigate "https://apps.who.int/food-additives-contaminants-jecfa-database/Search.aspx#"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
IE.Document.forms("form1").elements("ctl00$ContentPlaceHolder1$txtSearch").Value = "67-64-1"
IE.Document.forms("form1").elements("ctl00$ContentPlaceHolder1$btnSearch").Click
End Sub
Ultimately I wish to create a list in an excel sheet of CAS numbers that this code can loop through and return either the found phrase (in this case No safety concern at current levels of intake when used as a flavouring agent) or simply return a "Not Found". Sometimes the search returns multiple results, for the time being I just wish to take the first result.
This raises 2 problems I'm not sure how to solve:
How can I modify my code to loop through values within a column of a worksheet instead of having to explicitly give each one.
2.I'm unsure how to pull the data into the adjacent column.
Below is an image of the desired output. Column A is inputted by the user and hopefully column B is created by VBA code.
Any help would be appreciated.
what you need is a step by step process for web scraping.
i highly recommend you get familiar with seleniumbasic for vba https://florentbr.github.io/SeleniumBasic/
you need to loop on your excel rows using Range() or Cells(i,1) to read the row.
you check the number of search results using collections
save as many results as you wish in the excel in front of the row using cells(i, k) k being number of returned search results.
unfortunately the website did not load for me to help you further

VBA: xmlhttp response paste is sometimes omitting tabs?

EDIT2: I can't find why it might be pasting it incorrectly but the responseText reads as tab every time and I can just split it into an array and avoid the clipboard altogether. I'd still appreciate if someone has any ideas as to why this might've happened.
Sub example()
Dim clip As Object, D As Object, H As Object, URL As String
Set clip = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set H = CreateObject("WinHTTP.WinHTTPRequest.5.1")
Set D = CreateObject("HTMLFile")
'authentication
'xmlrequest
'here
URL = "https:/someinternal.site/tsv?filter=querystring"
H.Open "GET", URL
H.send
clip.SetText H.responseText
clip.PutInClipboard
Sheet1.Range("A1").PasteSpecial
End Sub
I'm trying to query an internal site that responds with a .TSV and I'm getting inconsistent results. Sometimes it pastes in properly split into columns, other times it ommits all of the deliminators and drops it in like this:
FullNameFirstLast
John SmithJohnSmith
When it should be:
FullName First Last
John Smith John Smith
I've found resources where people talk about using the TextToColumns manually to set the default for the deliminator, but when I paste into Excel, it looks the same - all tabs are taken out. If I paste the website (just a raw .TSV) into notepad++ and from there into Excel, it properly shows the tabs and line breaks on the editor and works in Excel. If I debug.Print H.responseText, the response it gives me replaces all of the tabs with 4 spaces which I guess is pretty normal for the immediate window but it doesn't help me.
Elsewhere in the code, I also query a .CSV file using the same method which I think is breaking it. Are there any parameters I can set to properly parse the response, or another method to use? For this I could use a querytable connection, but due to the authentication that needs to happen first, this has its own problems and I try to avoid them like the plague.
EDIT: paste dump from Excel pasted as an image instead of text. Even more confusing is when I copy and paste the Excel output into notepad or notepad++, it properly displays the tabs. Pasting it back into Excel produces the same result. This means the response is coming out right, but Excel itself is doing something strange with the tabs... Manual TextToColumns worked when it wasn't finding the deliminator earlier. Sometimes it comes out as four spaces and other times, invisible tabs - this is the issue.

Reading sheet contents for Stored procedure body and creating in oracle

My stored procedure contains 13000+ characters, I am trying to create this SP in oracle using excel VBA.
I placed full SP body in single cell in excel sheet then I read and stored in string
sp_code = Worksheets("properties").Cells(2, 1).Value
Later using ADODB.Command I pushed code to oracle but it give error like below
identifier too long
I came across through web docs like vba won't handle more than 255 characters in a identifier, So I break the sp code and placed in each cell and declared a variant then push the code like below.
sp_code = Worksheets("properties").Range("B2:J2").Value
cmd_meta.CommandText = sp_code(1, 1) & sp_code(1, 2) & sp_code(1, 3) & sp_code(1, 4)
But still got same error.
Please share your thoughts and possible ways to get solve this issue.
Thanks in advance.
#duplicate markers - please tag the link of duplicate before marking mine.

Resources