Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Document.Head.InnerHTML ??

539 views
Skip to first unread message

McKirahan

unread,
Aug 26, 2004, 3:42:01 PM8/26/04
to
Is there a way to change the "innerHTML" of the "<head>" tag?

Basically, I want to use <style>...</style>.

Below is an example that fails because "Document.Head" is invalid.

Thanks in advance.

Option Explicit
Dim strIE1
strIE1 = ""
strIE1 = strIE1 & "<style type='text/css'>"
strIE1 = strIE1 & " td { font-size:64pt }"
strIE1 = strIE1 & " th { font-size:64pt; font-weight:bold }"
strIE1 = strIE1 & ".xx { font-size:64pt; font-weight:bold }"
strIE1 = strIE1 & "</style>"
Dim strIE2
strIE2 = ""
strIE2 = strIE2 & "<span class='xx'>XX</span>"
strIE2 = strIE2 & "<br>"
strIE2 = strIE2 & "<table border='1'>"
strIE2 = strIE2 & "<tr><th>TH</th></tr>"
strIE2 = strIE2 & "<tr><td>TD</td></tr>"
strIE2 = strIE2 & "<tr><td style='font-size:64pt'>TH</td></tr>"
strIE2 = strIE2 & "</table>"
Dim objIEA
Set objIEA = CreateObject("InternetExplorer.Application")
objIEA.Navigate "about:blank"
While objIEA.Busy
Wend
objIEA.Visible = True
objIEA.Document.Title = "about_blank.htm"
objIEA.Document.Head.InnerHTML = strIE1
objIEA.Document.Body.InnerHTML = strIE2
Set objIEA = Nothing

[ I posted a variation of this over 5 hours ago but didn't see it in the
ng. ]


mayayana

unread,
Aug 26, 2004, 7:52:55 PM8/26/04
to
There is no document.Head. I'm not sure what would work
but you might try document.styleSheets(0) to get the Style
tag if it already exists. You can also use document.all("HEAD")
to access the HEAD tag, though I'm not sure exactly what
you get that way. If that doesn't work you could write the HEAD
using document.write and then just add the BODY, since you're
writing a new page anyway.

mayayana

unread,
Aug 26, 2004, 10:50:57 PM8/26/04
to
Woops.
document.all("HEAD") should be document.all.tags("HEAD")
--
--
mayayana <mayaXX...@mindYYspring.com> wrote in message
news:rJuXc.13907$2L3....@newsread3.news.atl.earthlink.net...

McKirahan

unread,
Aug 27, 2004, 5:40:05 AM8/27/04
to
"mayayana" <mayaXX...@mindYYspring.com> wrote in message
news:lkxXc.5032$Y%3....@newsread2.news.atl.earthlink.net...

> Woops.
> document.all("HEAD") should be document.all.tags("HEAD")
> --
> --
> mayayana <mayaXX...@mindYYspring.com> wrote in message
> news:rJuXc.13907$2L3....@newsread3.news.atl.earthlink.net...
> > There is no document.Head. I'm not sure what would work
> > but you might try document.styleSheets(0) to get the Style
> > tag if it already exists. You can also use document.all("HEAD")
> > to access the HEAD tag, though I'm not sure exactly what
> > you get that way. If that doesn't work you could write the HEAD
> > using document.write and then just add the BODY, since you're
> > writing a new page anyway.

[snip]

Thanks for your reply.

Your solution does not work in my case.

I want to change declare a <style> section which should go in the <head>
section of a document created when building an "about:blank" page as below:

Option Explicit
Dim strIE1
strIE1 = ""
strIE1 = strIE1 & "<style type='text/css'>"
strIE1 = strIE1 & " td { font-size:64pt }"
strIE1 = strIE1 & " th { font-size:64pt; font-weight:bold }"
strIE1 = strIE1 & ".xx { font-size:64pt; font-weight:bold }"
strIE1 = strIE1 & "</style>"
Dim strIE2
strIE2 = ""
strIE2 = strIE2 & "<span class='xx'>XX</span>"
strIE2 = strIE2 & "<br>"
strIE2 = strIE2 & "<table border='1'>"
strIE2 = strIE2 & "<tr><th>TH</th></tr>"
strIE2 = strIE2 & "<tr><td>TD</td></tr>"
strIE2 = strIE2 & "<tr><td style='font-size:64pt'>TH</td></tr>"
strIE2 = strIE2 & "</table>"
Dim objIEA
Set objIEA = CreateObject("InternetExplorer.Application")
objIEA.Navigate "about:blank"
While objIEA.Busy
Wend
objIEA.Visible = True
objIEA.Document.Title = "about_blank.htm"

''' objIEA.Document.{something} = strIE1

Joe Fawcett

unread,
Aug 27, 2004, 7:19:41 AM8/27/04
to
"McKirahan" <Ne...@McKirahan.com> wrote in message
news:VjDXc.99324$TI1.76970@attbi_s52...
You seem to be following a strange approach. Why not just use normal
techniques, either open the document, documenet.write and close or build
using DOM node methods?

--

Joe


McKirahan

unread,
Aug 27, 2004, 7:33:46 AM8/27/04
to
"Joe Fawcett" <joefa...@hotmail.com> wrote in message
news:ub0wzdCj...@TK2MSFTNGP15.phx.gbl...

Thanks for your reply.

I'm trying to construct and display a Web page from within an HTA without
having to use FSO to write it out first.

"objIEA.document.write" is not a valid method for this.

P.S. I'd also like to be able to have the following work but they don't.
<span onclick="window.close">Close<span>
and
<span onclick="window.print">Print<span>

Perhaps I'll have to use FSO to do what I want after all....


McKirahan

unread,
Aug 27, 2004, 8:12:49 AM8/27/04
to
[snip]

I came up with a solution thanks to concepts at this link:
http://ourworld.compuserve.com/homepages/Guenter_Born/WSHBazaar/WSHInput1.ht
m


Option Explicit


Dim objIEA
Set objIEA = CreateObject("InternetExplorer.Application")
objIEA.Navigate "about:blank"
While objIEA.Busy
Wend

Dim objIED
Set objIED = objIEA.Document
objIED.Open
objIED.Write "<hmtl>"
objIED.Write "<head>"
objIED.Write "<title>about-blank.vbs</title>"
objIED.Write "<style type='text/css'>"
objIED.Write " td { font-size:24pt }"
objIED.Write " th { font-size:24pt; font-weight:bold }"
objIED.Write ".xx { font-size:24pt; font-weight:bold }"
objIED.Write "</style>"
objIED.Write "<span class='xx'>XX</span>"
objIED.Write "</head>"
objIED.Write "<body>"
objIED.Write "<br>"
objIED.Write "<table border='1'>"
objIED.Write "<tr><th>TH</th></tr>"
objIED.Write "<tr><td>TD</td></tr>"
objIED.Write "</table>"
objIED.Write "<br>"
objIED.Write "<button onclick='window.close()'>Close</button>"
objIED.Write " &nbsp; "
objIED.Write "<button onclick='window.print()'>Print</button>"
objIED.Write "</body>"
objIED.Write "</hmtl>"
objIED.Close
Set objIED = Nothing
objIEA.Visible = True
Set objIEA = Nothing


1) Unfortunately, when I click "Close" I get a dialog asking:

"The Web page you are viewing is trying to close this window.
Do you want to close this window?
[Yes] [No]"


2) Also, I'd like to build a variable and write it; such as:

Dim strIED
strIED = "Hello World!"
objIED.Write strIED

But it returns an error.

Oh well, half a loaf ....

Please let me know if you have any additional ideas.


McKirahan

unread,
Aug 27, 2004, 8:33:54 AM8/27/04
to
[snip]

A variation of my last approach is:

Option Explicit
Dim objIEA
Set objIEA = CreateObject("InternetExplorer.Application")

With objIEA
.Navigate "about:blank"
.Document.Open
.Document.Write "<hmtl>"
.Document.Write "<head>"
.Document.Write "<title>about-blank.vbs</title>"
.Document.Write "<style type='text/css'>"
.Document.Write " td { font-size:24pt }"
.Document.Write " th { font-size:24pt; font-weight:bold }"
.Document.Write ".xx { font-size:24pt; font-weight:bold }"
.Document.Write "</style>"
.Document.Write "<span class='xx'>XX</span>"
.Document.Write "</head>"
.Document.Write "<body>"
.Document.Write "<br>"
.Document.Write "<table border='1'>"
.Document.Write "<tr><th>TH</th></tr>"
.Document.Write "<tr><td>TD</td></tr>"
.Document.Write "</table>"
.Document.Write "<br>"
.Document.Write "<button onclick='window.close()'>Close</button> &nbsp;
"
.Document.Write "<button onclick='window.print()'>Print</button> &nbsp;
"
.Document.Write "</body>"
.Document.Write "</hmtl>"
.Document.Close
.Visible = True
End With
While objIEA.Busy
Wend
Set objIEA = Nothing


Though I still don't understand why the following doesn't work:

Dim strIEA
strIEA = "<i>Hello World!</i>"
.Document.Write strIEA

This link suggests that it should work:
http://www.mcse.ms/archive127-2004-1-347542.html
as it uses ".document.Write vHTMLBody".
(I guess it's different when used in a VB macro.)


mayayana

unread,
Aug 27, 2004, 9:01:17 AM8/27/04
to

>
> Though I still don't understand why the following doesn't work:
>
> Dim strIEA
> strIEA = "<i>Hello World!</i>"
> .Document.Write strIEA

Document.write is part of the DOM, as opposed to
TextStream.write, which does require FSO. Document
is a property of the window object. There's no reason
that the above shouldn't work except that the period
creates an undefined object reference. It looks like you're
missing a With/End With that was originally with that sample.
You can use something like:

window.document.write "blah"
OR
With window.document
.write "blah"
.write "more blah"
End With

McKirahan

unread,
Aug 27, 2004, 9:34:12 AM8/27/04
to
"mayayana" <mayaXX...@mindYYspring.com> wrote in message
news:xgGXc.128$JT...@newsread3.news.atl.earthlink.net...

Thanks for your reply.

I hoped that that statement would be read in context of the entire post;
that is, it does not work after inserting it into the script.

Here's a stripped down version that doesn't work:

<html>
<head>
<title>Hello.hta</title>
</head>
<body>
<script type="text/vbscript">
Option Explicit
Dim strIEA
strIEA = "<br>Hello World?"


Dim objIEA
Set objIEA = CreateObject("InternetExplorer.Application")
With objIEA
.Navigate "about:blank"
.Document.Open

.Document.Write "<hmtl><head>"
.Document.Write "<title>Hello World!</title>"
.Document.Write "</head><body>"
.Document.Write "Hello World!"
'* The following line does not work:
.Document.Write strIEA
.Document.Write "</body></hmtl>"


.Document.Close
.Visible = True
End With
While objIEA.Busy
Wend
Set objIEA = Nothing

</script>
</body>
</html>

It also doesn't work as a VBS file; that is, just the script portion, unless
the one line is commented out.


mayayana

unread,
Aug 27, 2004, 7:50:42 PM8/27/04
to
You mean that all the write calls work except with the variable?
I don't know why that might be. If none of them worked it might
be because it's still loading and needs a loop to wait for that.

I don't know if I've ever tried to create an IE instance from
within a webpage, so I'm not sure whether there are any issues
with that. Is there any reason not to just use window.open?

Dim Win1, s
s = "hello world"
Set Win1 = window.open("", null,
"height=400,width=400,status=no,toolbar=yes, etc...")
With Win1.document
.Write "first line"
.Write "second line"
.Write s
.close
End With

That does the same thing except that you have limited access
to the actual IE object once the window is created. The 3rd
parameter takes numerous IE properties when setting up the
window, but what's returned in Win1 is a window object rather
than an IE object.


--
--


McKirahan <Ne...@McKirahan.com> wrote in message

news:nLGXc.324135$%_6.209652@attbi_s01...

McKirahan

unread,
Aug 27, 2004, 9:46:24 PM8/27/04
to
"mayayana" <mayaXX...@mindYYspring.com> wrote in message
news:mNPXc.729$JT3...@newsread3.news.atl.earthlink.net...

> You mean that all the write calls work except with the variable?
> I don't know why that might be. If none of them worked it might
> be because it's still loading and needs a loop to wait for that.
>
> I don't know if I've ever tried to create an IE instance from
> within a webpage, so I'm not sure whether there are any issues
> with that. Is there any reason not to just use window.open?
>
> Dim Win1, s
> s = "hello world"
> Set Win1 = window.open("", null,
> "height=400,width=400,status=no,toolbar=yes, etc...")
> With Win1.document
> .Write "first line"
> .Write "second line"
> .Write s
> .close
> End With
>
> That does the same thing except that you have limited access
> to the actual IE object once the window is created. The 3rd
> parameter takes numerous IE properties when setting up the
> window, but what's returned in Win1 is a window object rather
> than an IE object.

[snip]

Thanks for the suggestion.

Though I couldn't get the example that I posted to work (perhaps it's a
timing issue as you suggested; I'll try again), I did get it (writing a
variable) to work in my production code.

Christoph Basedau

unread,
Aug 28, 2004, 12:26:41 PM8/28/04
to
28.08.2004 03:46, McKirahan schrieb:

> "mayayana" <mayaXX...@mindYYspring.com> wrote in message
> news:mNPXc.729$JT3...@newsread3.news.atl.earthlink.net...

[Trouble with a VBScript-String passed from HTA to
IE-document.write]

> Though I couldn't get the example that I posted to work (perhaps it's a
> timing issue as you suggested; I'll try again), I did get it (writing a
> variable) to work in my production code.

I could repro your problem, the error says that a Variant-Type is used
that isn't supported by VBScript/or a NULL-Poniter was passed to the stub.
So somehow that string is stored by VBScript in a variant-subtype
that document.write can't handle.

Work-around: same script in JScript:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-16">


<title>Hello.hta</title></head><body>

<script type="text/jscript">

var objIEA = new ActiveXObject("InternetExplorer.Application");
var s = "<br>Hello World?";
with (objIEA) {
Navigate ("about:blank");
while (Busy);
Document.open();
Document.write ("<hmtl><head>");
Document.write ("<title>Hello World!</title>");
Document.write ("</head><body>");
Document.write ("Hello World!");
//* The following line does work now:
Document.write (s);
Document.write ("</body></hmtl>");
Document.close();
Visible = true ;
}
</script>
</body>
</html>

--
HAND, Christoph

Rio Riay Riayo - Gordon Sumner, 1979

Christoph Basedau

unread,
Aug 28, 2004, 6:03:14 PM8/28/04
to
28.08.2004 18:26, Christoph Basedau schrieb:
> 28.08.2004 03:46, McKirahan schrieb:
>> "mayayana" wrote:
>
> [Trouble with a VBScript-String passed from HTA to
> IE-document.write]
>
>> Though I couldn't get the example that I posted to work (perhaps it's a
>> timing issue as you suggested; I'll try again), I did get it (writing a
>> variable) to work in my production code.
>
> I could repro your problem, the error says that a Variant-Type is used
> that isn't supported by VBScript/or a NULL-Poniter was passed to the stub.
> So somehow that string is stored by VBScript in a variant-subtype
> that document.write can't handle.
> Work-around: same script in JScript:

The solution is ByVal.
If you want to make your script work in VBScript, you must pass
the String *ByValue*. (that's why it works in JScript, which
doesn't know ByRef).
You have to wrap it into round brackets to do so.
Obviously a string cannot be passed ByRef between *two* processes
in VBScript, maybe because you cannot be sure, what kind of
datatype is returned from the other process, and if it is
automation-compatible.

<head><title>Hello.hta</title>
</head><body><script type="text/vbscript">
Option Explicit
Dim strIEA, objIEA


strIEA = "<br>Hello World?"

Set objIEA = CreateObject("InternetExplorer.Application")
With objIEA
.Navigate "about:blank"
.Document.Open
.Document.Write "<hmtl><head>"
.Document.Write "<title>Hello World!</title>"
.Document.Write "</head><body>"

'wrap the string into round brackets, then it's passed *ByVal*
.Document.Write (strIEA)


.Document.Write "</body></hmtl>"
.Document.Close
.Visible = True
End With

</script></body></html>


If you use roughly the same code, to write to the *same* document
then it's know problem to pass the string ByRef, because there
is not process-boundary to be crossed and no
proxy/stub to be created.

<html><head><title>Hello.hta</title></head><body>
<script type="text/vbscript">

Option Explicit
Dim str
str = "<br>Hello World?"
document.Open
document.Write "<hmtl><head>"
document.Write "<title>Hello World!</title>"
document.Write "</head><body>"
document.Write "Hello World!"
document.Write str 'ByRef works here!
document.Write "</body></hmtl>"
document.Close
</script>
</body>
</html>

--
Gruesse, Christoph

McKirahan

unread,
Aug 28, 2004, 7:51:05 PM8/28/04
to
"Christoph Basedau" <e_t...@hotmail.com> wrote in message
news:41310246$0$6622$9b4e...@newsread4.arcor-online.net...

Excellent! Thanks, I didn't know that.


0 new messages