Solved Retain Formatting When HTA Displays Text

June 4, 2016 at 05:47:13
Specs: Windows 7, 1.6 gb 4gb
Knowing that vbscript cannot create Msgbox with scrollbars I have been trying to get round this by using HTA but with only partial success. The script I have is not mine but I have modified it slightly. The problem is that although it does produce a window with scrollbars text is displayed in the window as a continuous string with all formatting eliminated.

I have tried to resolve this using ReadLine rather than ReadAll but it makes no difference yet I was sure that ReadLine automatically incorporates a VBCrLf at the end of every line. The other oddity is that wscript.echo does not work. An error is produced requiring an "object". When that is provided, the subsequent error is that Echo is not supported. I am guessing here that this is down this being run as an HTA file not a vbscript one.

If anyone can enlighten me on that and knows how to retain the original file text formatting I would be grateful if they could let me know.

Thank you

HTA Script:


<TITLE>Read File</TITLE>

ID = "oHTA"
APPLICATIONNAME = "Read Text Files with HTA"
BORDER = "thick"
CAPTION = "yes"
ICON = "app.ico"
SYSMENU = "yes"
WINDOWSTATE = "normal"
SCROLL = "yes"
VERSION = "1.0">

<SCRIPT Language="VBScript">
Option Explicit
Dim sContents
Dim oFS
Dim oFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Sub ReadFile
Set oFS = CreateObject("Scripting.FileSystemObject")

If oFS.fileExists(fName.value) Then
Set oFile = oFS.OpenTextFile(fName.value, ForReading)
sContents = oFile.ReadAll

' ReadLine does not seem to work
'Do Until oFile.atEndOfStream
'sContents = oFile.ReadLine

MsgBox "File not found: " & fName.value
End If
End Sub


<BODY scroll="yes" style=
"background:buttonface; color:buttontext;
border:0px; padding:0px; margin:0px">

<table cellspacing=0 cellpadding=0
border=0 width=100% height=100%>

<td height=4></td>

<td height=20>
File name: <INPUT TYPE="text" Size=50 Name="fName">
<BUTTON style="border:1px outset;width:100px" onClick="ReadFile" >
Read File</BUTTON>

<td height=4></td>

<div id="txtbox" style=
border: 1px inset;
font: 8pt Arial; width:100%;




See More: Retain Formatting When HTA Displays Text

Reply ↓  Report •

June 4, 2016 at 06:11:04
Your problem is one of HTML's text rendering. Namely, the specification ignores line endings and leading white space. Easiest fix is to just wrap everything in PRE tags, much like how you do so with this site.

Also note, you've got a pretty severe injection vulnerability, so I hope this is for testing, and not going in to production.

How To Ask Questions The Smart Way

Reply ↓  Report •

June 5, 2016 at 09:20:02
Razor thank you for your interest. This was merely something in HTA that I was exploring as a result of not being able to see the tail end of text in a MsgBox because you can't scroll it down. I know MsgBox is intended for short messages not loads of text.

However, I have been able to resolve this (vbscript below) for my own use. I know it's very "Heath Robinson" and the coding and indentation not up to your high standards but it works. Based on the assumption of 2000 characters (Approx) per message it pages the text and avoids broken words so there is continuity moving from one Msgbox to the next.

Unfortunately the size of the MsgBox can't be controlled as it there are no properties. There's no error checking and if there is an empty file in the folder it will obviously produce an "input past end" error message.


Const ForReading = 1

Set oFS = CreateObject("Scripting.FileSystemObject")

strPath1 = "C:\notes and jottings\quill\temp2\"

'Set the folder
Set FLD = oFS.GetFolder(strPath1)

'Loop through each file in the folder.
For Each fil in FLD.Files
'Get complete file name with path.
strPathway = fil.Path

Set oFile = oFS.OpenTextFile(strPathway, ForReading)
strContents = oFile.ReadAll
FullText = len(strContents)
Counter = int(FullText/2000)+1

'wscript.echo Counter

for n = 1 to Counter
strPage = mid(strContents,k,2000)
Marker = instrRev(strPage," ")

'wscript.echo "Marker: " & marker
strSpace = 2000 - Marker
if n = Counter then
strModPage = mid(strContents,k)
strModPage = mid(strContents,k,2000-strSpace)
end if

wscript.echo strModPage & VBCrLf & VBCrLf & VBCrLf & "Page: " & n & " of " & Counter



Reply ↓  Report •

June 5, 2016 at 11:17:52
✔ Best Answer
Really, when I want to output a large amount of text from a VBScript, I always check to see if the script was running under cscript, and if not, rerun the script through cscript.

There's a few scripts on this site where I've done so, but I can't find any of them so here's the QnD example:

Function ConCheck 'As Boolean
  On Error Resume Next
  WScript.StdOut.WriteLine ""
  ConCheck = Err.Number = 0
  On Error GoTo 0
End Function

If Not ConCheck() Then _
  WScript.Quit CreateObject("WScript.Shell").Run("cmd /c cscript """ _
   & WScript.ScriptFullName & """ //nologo &pause")

WScript.Echo "Hello World"

How To Ask Questions The Smart Way

Reply ↓  Report •

Related Solutions

Ask Question