Search code examples
htmlvbadouble-quotes

How to edit the HTML of a webpage


After posting my question, I found the solution, so instead of deleting it, I rather post the solution. It may be helpful for someone else.

I want to modify some elements in an HTML webpage that I have opened with VBA as an IE Object. Specifically, I want to change the background color of a title element (the day number in a multiline calendar), so that it is more easily distinguished from the other day numbers. The server sends all day numbers with a dark grey background but I want to see today's number with a bright yellow background on my screen. I found the relevant HTML code is like this:

<th class="daynumber">24</th>
<th class="daynumber">25</th>
<th class="daynumber">26</th>
<th class="daynumber">27</th>

and in VBA I can retrieve day 26 with

a = IE.Document.getelementsbyclassname("daynumber").Item(25).outerHtml

Then I use

IE.Document.getelementsbyclassname("titel oben daynumber").Item(25).outerHtml = "<th>bgcolor=""yellow"">26</th>"

to edit the code after retrieving the element. The first problem I encountered was how to created a text variable with nested quotes. This is resolved by replacing the inner quotes with double-quotes. The second was that during my trials extra HTML lines were created, but I understood now that I cannot repeat the second line without repeating also the first. So the complete code is (after opening the webpage as an IE Object):

a = IE.Document.getelementsbyclassname("titel oben daynumber").Item(25).outerHtml
IE.Document.getelementsbyclassname("titel oben daynumber").Item(25).outerHtml = "<th bgcolor=""yellow"">26</th>"

where 25 and 26 will have to be replaced by a variable concatenated to the text.


Solution

  • You will have to use the following to compile the above properly.

    IE.Document.getelementsbyclassname("titel oben daynumber").Item(25).outerHtml = "<th>bgcolor=""yellow"">26</th>"
    

    Note the Double Double Quotes before and after Color code.

    Or Just the below as suggested by braX

    IE.Document.getelementsbyclassname("titel oben daynumber").Item(25).outerHtml = "<th>bgcolor=yellow>26</th>"