How would I insert data from excel into a html table? - html

I have an excel file which contains data for football statistics and I want to be able to change the excel numbers, and in doing so the website updates its table automatically.
Is there a possible way to do this?

This doesn't completely answer your question, but I thought to include it as a partial answer. I have a generic procedure (method) I use from time to time to convert an Excel Table (aka ListObject) into an HTML table. Perl has a module that does this brilliantly, but I've never found anything similar for VBA, hence my hand-rolled code.
The only caveat is that your data has to be in a table. If it's coming from an RDBMS, then the easiest thing to do is to bring it in via MS Query, which automatically renders the output as a Table/ListObject.
I know you are talking about Web and made no mention of VBA -- just take this for what it's worth. I'm hopeful it has some useful components you can glean.
Function TableToHtml(ByRef Table As ListObject, Title As String) As String
Dim row As ListRow
Dim header, col As range
Dim output As String
output = _
"<html>" & vbCrLf & _
" <head>" & vbCrLf & _
" <title>" & vbCrLf & _
Title & vbCrLf & _
" </title>" & vbCrLf & _
" </head>" & vbCrLf & _
"<body>" & vbCrLf & _
"<font size=""5"">" & Title & "</font><br><br>" & vbCrLf & _
"<table border='1px' cellpadding='5' cellspacing='0' style='border: solid 1px Black; font-size: small;'>"
output = output & "<tr align='center' valign='top'>" & vbCrLf
Set header = Table.HeaderRowRange
For Each col In header.Columns
output = output & " <td align='center' valign='top'>" & col.Value & "</td>" & vbCrLf
Next col
output = output & "</tr>" & vbCrLf
For Each row In Table.ListRows
output = output & "<tr align='left' valign='top'>" & vbCrLf
For Each col In row.range.Columns
output = output & " <td align='center' valign='top'>" & col.Value & "</td>" & vbCrLf
Next col
output = output & "</tr>" & vbCrLf
Next row
Dim o As Object
output = output & "<tr align='left' valign='top'>" & vbCrLf
For Each header In Table.TotalsRowRange
output = output & " <td align='center' valign='top'>" & header.Value & "</td>" & vbCrLf
Next header
output = output & "</tr>" & vbCrLf
output = output & "</table>" & vbCrLf & "</body>" & vbCrLf & "</html>"
TableToHtml = output
End Function
Yes, this is pretty brute-force.

Related

Structuring HTML so text displays in one paragraph

I'm working on a VBA code in Excel to get input from reports and send emails.
I'm trying to avoid line breaks in my email body.
With olMail
.To = rep
.Subject = title & " - " & EndTitle
'.Recipients.Add rep
.Attachments.Add filePath & "\" & title & " - " & EndTitle & ".pdf"
.htmlBody = "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an email</u></b></BODY>" & "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an email</u></b></BODY>" _
& "<br>" & "<b>HK</b>" & "<b>" & amt & "</b>" _
& "<br>" & "Value date" & "<b>" & Trans & "</b>" & "<br>" &
initial
.Display
'.Send
End With
My email looked like this
Here is the email
Here is the email
What I want the email body to look like
Here is the email Here is the email
(on the same line)
Use this instead:
.htmlBody = "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an emailHere is an email</u></b>" _
& "<br>" & "<b>HK" & amt & "</b>" _
& "<br>" & "Value date" & "<b>" & Trans & "</b>" & "<br>" & initial & "</BODY>"
Or this, depending on which line breaks you want gone:
.htmlBody = "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an emailHere is an email</u></b>" _
& "<b>HK" & amt & "</b>" _
& "Value date" & "<b>" & Trans & "</b>" & initial & "</BODY>"
See how I encapsulated all the HTML between the opening and closing BODY tags?

VB.NET > SQL > HTML Tables, how to work with functions

On this form, the user performs an inventory search. I have the results of that search displayed in an HTML table.
If rsMPCS.HasRows = False Then
MessageBox.Show("No Item Found")
Else
varHTMLBody = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">" & vbCrLf & "<html>" & vbCrLf & "<head>" & vbCrLf & " <title></title>" & vbCrLf
varHTMLBody = varHTMLBody & " <meta http-equiv=Content-Type content=""text/html; charset=iso-8859-1"">" & vbCrLf & "</head>" & vbCrLf
varHTMLBody = varHTMLBody & "<body bgcolor=""#FFFFFF"">" & vbCrLf
varHTMLBody = varHTMLBody & "<table border=""0"" cellpadding=""0"" cellspacing=""0"" style=""border-collapse: collapse"" bordercolor=""#000000"" width=""100%"" id=""AutoNumber1" & vbCrLf
varHTMLBody = varHTMLBody & "<tr>" & vbCrLf
varHTMLBody = varHTMLBody & "</tr>" & vbCrLf
varHTMLBody = varHTMLBody & "<tr>" & vbCrLf
varHTMLBody = varHTMLBody & "<tr>" & vbCrLf
varHTMLBody = varHTMLBody & "<table border=0 width=""100%"" height=""200"" cellspacing=""0"" cellpadding=""0"">" & vbCrLf
varHTMLBody = varHTMLBody & "<table width=""100%"" border=1 cellspacing=0 cellspadding=0>" & vbCrLf
varHTMLBody = varHTMLBody & "<td bgcolor=""#000000""><b><font size=4 color=""#FFFFFF"">Part Number: </b></td>" & vbCrLf
varHTMLBody = varHTMLBody & "<td bgcolor=""#000000""><b><font size=4 color=""#FFFFFF"">Description: </b></td>" & vbCrLf
varHTMLBody = varHTMLBody & "<td bgcolor=""#000000""><b><font size=4 color=""#FFFFFF"">Quantity: </b></td>" & vbCrLf
varHTMLBody = varHTMLBody & "<td bgcolor=""#000000""><b><font size=4 color=""#FFFFFF"">Minimum: </b></td>" & vbCrLf
varHTMLBody = varHTMLBody & "<td bgcolor=""#000000""><b><font size=4 color=""#FFFFFF"">Cart </b></td>" & vbCrLf
varHTMLBody = varHTMLBody & "<tr>" & vbCrLf
varHTMLBody = varHTMLBody & "</tr>" & vbCrLf
While rsMPCS.Read
varHTMLBody = varHTMLBody & "<td><font size=2>" & rsMPCS("MTI_PART_NO") & "</td>" & vbCrLf
varHTMLBody = varHTMLBody & "<td><font size=2>" & rsMPCS("Description") & "</td>" & vbCrLf
varHTMLBody = varHTMLBody & "<td><font size=2>" & rsMPCS("STOCKROOM_QTY") & "</td>" & vbCrLf
varHTMLBody = varHTMLBody & "<td><font size=2>" & rsMPCS("MIN_QTY") & "</td>" & vbCrLf
varHTMLBody = varHTMLBody & "<td><img src=""k12169922.jpg"" width=""100"" height=""100""> " & "</td></tr>" & vbCrLf
End While
In the "cart" column is a picture of a shopping cart so they can click the one next to the item they need the inventory dept to fill (this is all internal dept supplies, not external customers)
I'm not sure 1) how to make the image in the cart column launch a function. And 2) how to carry only the information in that particular row over to use on the cart form that I want clicking that image to launch.

Apply Bold to Cell Value inside the text string of a HTMLbody

I am having trouble with applying Bold on a part of a sentence for an automatically generated email.
In my VBA script:
Public strBody As String
.HtmlBody = strBody
Dim FormulaCell As Range
Dim FormulaRange As Range
For Each FormulaCell In FormulaRange.Cells
With FormulaCell
I am able to Bold a whole sentence using "< b >" & "**Text Here**" & "< /b >" &
But it seems impossible to apply the same logic to a more complex sentence, or a referenced value.
strBody = "Hello, " & vbNewLine & vbNewLine & _
"Your task : " & Cells(FormulaCell.Row, "B").Value & " with the mention: " & Cells(FormulaCell.Row, "C").Value & " is nearing its Due Date: "
What I would like to Bold in the above sentence is the value of the FormulaCells.
strBody = "Hello, " & vbNewLine & vbNewLine & _
"Your task : " & **Cells(FormulaCell.Row, "B").Value** & " with the mention: " & **Cells(FormulaCell.Row, "C")**.Value & " is nearing its Due Date: "
So it would look something like that in my email:
Hello
Your task : Eat Potatoes with the mention: Potatoes are Delicious is nearing its Due Date:
Is this something that can be done?
edited
strBody = "<p>Hello</p>, " & vbNewLine & vbNewLine & _
"<p>Your task : <b>" & Cells(FormulaCell.Row, "B").Value & _
"</b> with the mention: <b>" & Cells(FormulaCell.Row, "C").Value & _
"</b> is nearing its Due Date: </p>"

Why is my vbCrLf not working?

I have to declare a variable in which I need to keep multiple sentences (vbscript is the language I am using). So, I put vbCrLf at the end of sentences in order to get a break. But it is not giving me the break instead it is giving me a whole paragraph.
strHtmlBody = "<div><font face='Arial' size='-1'>Greetings "& RS("firstname") &":"& vbCrLf & _
"Here is the email that contains your account information to access your account at www.example.com." & VbCrLf & VbCrLf & _
"Your Email Address: " & RS("Email") & VbCrLf & _
"Your Password : " & RS("Password") & VbCrLf & vbCrLf & _
"Remember, if you have any questions or concerns, please let us know, simply reply to this email." & VbCrLf & _
"ABC Company" & vbCrLf & _
"http://www.example.com" & vbCrLf & vbCrLf
strHtmlBody = strHtmlBody & "</div>"
HTML ignores most whitespace which vbCrLf represents. Use <br> tags in its place.

Problems in HTA

So I'm trying to use ScriptGUI (from here) for my .bat files, but it doesn't have a file selector, so I attempted to add one.
I just copied and renamed some code around and it seems to work, apart from adding another variable for the subfunction thing.
' build a file selector
ElseIf UCase(strSplit(0)) = "FILE" Then
strHTML = strHTML & Build_File(strLabel,id,Replace(strLabel," ",""),strSplit(2))
' store the batch file in the arrControls array
arrControls(id) = "file,none"
id = id + 1
>
the problem seems to be the code under here "Click_File(" & fiId & "," & fiFilter & ")"
Function Build_File(fiLabel, fiId, fiName, fiFilter)
' Construct a file selector
Dim strHTML
strHTML = "<input class='button' type='button' name='" _
& fiName & "' value='" & fiLabel & "' id='" & fiId _
& "' onClick=" & chr(34) & "Click_File(" & fiId & "," & fiFilter & ")" & chr(34) _
& " onMouseOver=" & chr(34) & fiName & ".className='button btnhov'" & chr(34) _
& " onMouseOut=" & chr(34) & fiName & ".className='button'" & chr(34) _
& ">"
strHTML = strHTML & " <input type='text' readonly='readonly' value='none' name='fi" & fiName & "' id='fi" & fiId & "'/> "
Build_File = strHTML
End Function
>
Sub Click_File(strId, fiFilter)
' open a file selector
set objShell= CreateObject("WSCript.Shell")
myCur = objShell.CurrentDirectory
Dim file
file = GetFileName(myCur, fiFilter)
arrControls(strId) = "file," & file
document.getElementById("fi" & strId).value = file
document.getElementById("fi" & strId).size = Len(file) + 2
End Sub
It tells me I can't use parenthesis when calling a sub.
Any ideas?
EDIT: new problem,
I'm using a script from Rob van der Woude for the open file dialog (top post from here) which is apparently supposed to work in hta but I get an error saying "ActiveX component can't create object: 'UserAccounts.CommonDialog'"
Well, the message isn't wrong. You can't use parentheses when calling a Sub.
Change this line from:
& "' onClick=" & chr(34) & "Click_File(" & fiId & "," & fiFilter & ")" & chr(34) _
to:
& "' onClick=" & chr(34) & "Click_File " & fiId & "," & fiFilter & chr(34) _
and see if that solves your problem.