Sure, here are some code samples:
- Error handling using If statements
Set HTMLDoc = IE.Document
Set IEInput = HTMLDoc.getElementsByClassName("kix-zoomdocumentplugin-mobile-view")
Dim data As Variant
If IEInput.Length > 0 Then ' Check if IEInput array is not empty
Dim gridContainers As Object
Set gridContainers = IEInput(0).getElementsByClassName("grid-container")
If gridContainers.Length > 0 Then ' Check if getElementsByClassName method returned an object
data = Split(gridContainers(0).innerText, vbLf)
Else
MsgBox "Could not find grid-container class in HTML document", vbCritical
End If
Else
MsgBox "Could not find kix-zoomdocumentplugin-mobile-view class in HTML document", vbCritical
End If
- Using getElementsByTagName method to extract data
Set HTMLDoc = IE.Document
Dim allTableTags As Object
Set allTableTags = HTMLDoc.getElementsByTagName("table")
Dim tableData As Variant
For Each tableItem In allTableTags
If tableItem.ParentNode.ClassName = "kix-tablerenderer-evenrow" Then ' Note: this may vary depending on how your Google Sheet is formatted
Dim row As Variant
For r = 0 To tableItem.Rows.Length - 1
ReDim Preserve row(0 To r, 0 To 9) ' Note: 10 columns in the table
Dim column As Variant
For c = 0 To 9 ' Note: 10 columns in the table
column = Trim(tableItem.Rows(r).Cells(c).innerText)
row(r, c) = column
Next c
Next r
tableData = row
End If
Next tableItem
- Using regular expressions to extract data
Set HTMLDoc = IE.Document
Dim jobData As String
jobData = HTMLDoc.body.innerText
Dim dataRegex As Object
Set dataRegex = CreateObject("VBScript.RegExp")
dataRegex.Pattern = "(Title|Department|Location|Status|Type|Shift|Travel|Hours per week)[\s\S]*?(((?!(Title|Department|Location|Status|Type|Shift|Travel|Hours per week)).)*)(?=Title|[\r\n]+\s*$)"
dataRegex.Global = True
Dim matches As Object
Dim dataRows As Variant
Set matches = dataRegex.Execute(jobData)
ReDim dataRows(0 To matches.Count - 1, 0 To 7) ' Note: 8 columns of data
For m = 0 To matches.Count - 1
Dim match As Object
Set match = matches(m)
Dim matchData As String
matchData = match.SubMatches.Item(1)
Dim matchDataLines As Variant
matchDataLines = Split(matchData, vbLf)
For l = 0 To UBound(matchDataLines)
Dim lineData As Variant
lineData = Split(matchDataLines(l), ":")
Select Case UCase(Trim(lineData(0)))
Case "TITLE"
dataRows(m, 0) = Trim(lineData(1))
Case "DEPARTMENT"
dataRows(m, 1) = Trim(lineData(1))
Case "LOCATION"
dataRows(m, 2) = Trim(lineData(1))
Case "STATUS"
dataRows(m, 3) = Trim(lineData(1))
Case "TYPE"
dataRows(m, 4) = Trim(lineData(1))
Case "SHIFT"
dataRows(m, 5) = Trim(lineData(1))
Case "TRAVEL"
dataRows(m, 6) = Trim(lineData(1))
Case "HOURS PER WEEK"
dataRows(m, 7) = Trim(lineData(1))
End Select
Next l
Next m
Note: The regular expression used in the third example assumes that the data in the Google Sheet is formatted as a table with rows and columns, and that each row begins with the header values (Title, Department, Location, etc.) and is followed by the corresponding data for each column. The pattern matches each row of data and separates it into its corresponding columns. You may need to modify the regular expression pattern depending on the specific formatting of your Google Sheet.