• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Using a simple text box (in VB) to type in a sentence and add it to a specific point in a text file.

Hi,

First time poster so any help with this would be welcome. Windows XP user here.
I have got a very simple HTML file that reads a second file and prints the contents of each section in 'ticker tape' fashion on a webpage.
I enclose below the 'second file' i.e. the file with the information to be printed. When I run my main HTML file - using the file below as a 'data feed' it will print the following on my webpage...

Test 1 then Test 2 then Test 3 then Test 4 then Test 5

All well and good - but Im looking for a way to dynamically update these fields with information, i.e. change Test 1 to "Leon to buy present for Grandma" or something.
Im looking for a way to:

1) Have a basic 1 line/2 line textbox I can type things into then press RETURN (or a button) to have whatever I've typed go into the file below. Thinking about it - I'd be looking to have 10 'tickers', and have whatever I type go into the next available ticker. If this was over complex then maybe I would have to specify what ticker it would go into. I could live with that e.g. "1 Leon to buy present for Grandma".
2) The file's contents are shown below). Nothing else is trying to open the file other than the main HTML page via a src command.
3) Im looking for the simplest way to dynamically update this, I'm hoping there's some VB command I can use, but if there's another way I'd like to hear it.
Thanks in advance for your help here.
L



-------------- THIS IS THE HTML FOR THE 'DATA FEED' PAGE, I'd like to dynamically change it from the point where it says "ADD YOUR TICKER CONTENT BELOW"---------------------

<script language="JavaScript1.2">
var delay=7000

var ie4=document.all

var curindex=0
var totalcontent=0

function get_total(){
if (ie4){
while (eval("document.all.content"+totalcontent))
totalcontent++
}
else{
while (document.getElementById("content"+totalcontent))
totalcontent++
}
}

function contract_all(){
for (y=0;y<totalcontent;y++){
if (ie4)
eval("document.all.content"+y).style.display="none"
else
document.getElementById("content"+y).style.display="none"
}
}

function expand_one(which){
contract_all()
if (ie4)
eval("document.all.content"+which).style.display=""
else
document.getElementById("content"+which).style.display=""
}

function rotate_content(){
get_total()
contract_all()
expand_one(curindex)
curindex=(curindex<totalcontent-1)? curindex+1: 0
setTimeout("rotate_content()",delay)
}

window.onload=rotate_content

</script>

<BODY bgColor=#000000>

<!--ADD YOUR TICKER CONTENT BELOW, by wrapping each one inside a <DIV> as shown below.-->
<!--For each DIV, increment its ID attribute for each additional content (ie: "content1", "content2" etc)-->


<div id="content0" style="display:''">

<!—this is content#1 even though the div id is “content0”--------------------->

Test 1

<!-- END CONTENT #1----------------->

</div>

<div id="content1" style="display:none">

<!-- ADD TICKER's CONTENT #2 HERE--------------------->
Test 2

<!-- END CONTENT #2----------------->

</div>

<div id="content2" style="display:none">

<!-- ADD TICKER's CONTENT #3 HERE--------------------->
Test 3

<!-- END CONTENT #3----------------->

</div>

<div id="content3" style="display:none">

<!-- ADD TICKER's CONTENT #4 HERE--------------------->
Test 4
</div>
<div id="content4" style="display:none">

<!-- ADD TICKER's CONTENT #5 HERE--------------------->

Test 5

<!-- END CONTENT #5----------------->

</div>

Open in new window

0
leonnelson
Asked:
leonnelson
  • 19
  • 13
  • 2
1 Solution
 
FaustulusCommented:
I know nothing about HTML, and my concern is that your idea to modify the text file may not be the best to have. I see code like "function get_total()" in your HTML text. Couldn't you have a function get_tickers()?
Approaching from the VBA side I would say you need a stock of your 10 - in fact the quantity doesn't matter - tickers. You could keep these in an Excel workbook because you will need some place to keep the VBA code, too. You can change your tickers and perhaps use VBA to manage them; that is another story. The important thing is that you have a button on your worksheet which, when pressed,  constructs a block of HTML code with all your tickers - including modified, not modified, added and deleted. This block replaces the existing block in your HTML text file.
This is what I would be able to do. Whether it is the best solution for your problem I don't know.
0
 
leonnelsonAuthor Commented:
Appreciate the response, have you got a code example I could use ?
0
 
leonnelsonAuthor Commented:
Im thinking the most logical way around this would be.

1) Create text box - and assign what I type into that box to a variable, lets say message$

2) Also have in the text box a variable that increments by 1 each time, lets call it x

3) When I type something in - the following block of code is added to my file (at the end).

<div id="content(x-1)" style="display:none">

<!-- ADD TICKER's CONTENT #x HERE--------------------->
message$
<!-- END CONTENT #x----------------->

</div>

That should work shouldn't it ?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
FaustulusCommented:
There is no such code. It has to be created. So, the method must be determined before the work can be put in. Once you decide that this is the solution you want I may have some more specific questions. Then, it'll take a few days.
0
 
leonnelsonAuthor Commented:
Ok, can we progress using this method ? I'm happy if we can move on ahead using this solution. Ideally it would be in vbs, as hopefully it should be a bit easier for me to follow.
I'm not a programmer by any means - but looking round, something like the InputBox function in vbs looks like it would do the trick.
0
 
leonnelsonAuthor Commented:
I've made (hopefully) a bit of progress...

The following vbs script seems to make a start on getting the information and saving it to a variable called 'summary'

summary=InputBox("Type in the sentence to 

be added to web page","This is the Window 

used to add the additional information")

MsgBox(summary)

Open in new window


I'd like the eventual Input Box to be further down the screen and thinner, but hopefully these are just cosmetics. This currently just prints out what I typed in.
What I'd like is for the script to do the following...

1) Define a loop counter between 1 and 10 - lets call this number n
2) Take my htm file and add the following to the end of if (without disrupting the rest of the file..

<div id="content(n-1)" style="display:none">

<!-- ADD TICKER's CONTENT #n HERE--------------------->
summary
</div>

Open in new window


Remembering that 'summary' was the variable I typed in the InputBox.

To keep it simple I'd like the counter to 'reset' (after it hit 10) back to 1.
0
 
Michel PlungjanIT ExpertCommented:
Please elaborate

1) the ticker is running on the web for all to see
2) you have a server where the file is currently stored
3) you currently edit the file by hand and then upload it to the server, but are looking to how just update the ticker part?
0
 
Michel PlungjanIT ExpertCommented:
Firstly let's bring your ticker into this millenium

DEMO

<html>
<head>
<script type="text/javascript">
var ticker=[
  "Content 1",
  "Content 2",
  "Content 3"    
]; // notice lack of comma on last item

var delay=7000
var curIndex=0
var tId;

function show() {
  document.getElementById("tickerDiv").innerHTML=ticker[curIndex];
  curIndex++;
  if (curIndex>=ticker.length) curIndex=0;
}    
window.onload=function() {
  show()
  tId =setInterval(function() {
    show()   
  },delay);
}    
</script>
<head>
<body>
<div id="tickerDiv"></div>
</body>
</html>

Open in new window

0
 
leonnelsonAuthor Commented:
Hi, and thanks for your help with this - to confirm...

1) the ticker is running on the web for all to see < its currently on my pc which isn't connected to the internet but to a local server. The web page only need be visible on this machine.
2) you have a server where the file is currently stored < the file is literally stored on a local machine. Only people sitting at the machine need to see this.
3) you currently edit the file by hand and then upload it to the server, but are looking to how just update the ticker part?
I'd like to have a VBS Input Box on my screen and type in a sentence - then have that sentence piped through to an htm text file (which is the source of my data). Youre right in that I do currently edit the file by hand.
0
 
leonnelsonAuthor Commented:
great stuff with the updated html code !
Now is there a way I can get a small input box permanently in the corner of my screen and then type stuff in so that it will print up in the ticker instead of just 'Content 1' 'Content 2' etc... ?
0
 
Michel PlungjanIT ExpertCommented:
Then we drag it into the 21st century

DEMO

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Ticker demo by mplungjan</title>
  
  <script type='text/javascript' src='http://code.jquery.com/jquery-git.js'></script>
  
<script type="text/javascript">
var delay=1000
var curIndex=0,tId,ticker=[];

function show() {
  document.getElementById("tickerDiv").innerHTML=ticker[curIndex];
  curIndex++;
  if (curIndex>=ticker.length) curIndex=0;
}    
$(function() {
  $.get("ticker.txt",function(data) {
    ticker=data.split("\n");
    show()
    tId =setInterval(function() {
      show()   
    },delay);
  });    
});    
</script>


</head>
<body>
<div id="tickerDiv"></div>
</body>
</html>

Open in new window

All you have to do now is to upload a text file called ticker.txt with each ticker content on a new line:

Content 1
Content 2
Content 3

So can you tell me how you normally update files on your server?
0
 
Michel PlungjanIT ExpertCommented:
Ah, missed the previous update.

I will see what I can do bout the box
0
 
leonnelsonAuthor Commented:
Thanks - this is great !! Ok so now I've got a vbs script so I can type stuff in and add it to the file

summary=InputBox("Type in the sentence to be added to web page","This is the Window used to add the additional information")

MsgBox(summary)


Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("C:\Documents and Settings\me\Desktop\ticker.txt", ForAppending, True)
    
    objTextFile.WriteLine(summary)

objTextFile.Close

Open in new window


Now is there a way of
1) Keeping the input box permanently on the screen - so its there all the time.
2) Using html to format the display of ticker.txt ?
This is just a memory-aid program that Im trying to put together so there really is no precendent in how files are updated. Thanks to your help though, its pretty near what I was looking for.

Once again, this help is much appreciated.
0
 
Michel PlungjanIT ExpertCommented:
Try this I cannot test it since I do not have a windows box

The html file needs to live in the same directory as the ticker.txt in this case C:\Documents and Settings\me\Desktop\

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Ticker demo by mplungjan</title>
  
  <script type='text/javascript' src='http://code.jquery.com/jquery-git.js'></script>
  
<script type="text/javascript">

function WriteFile(tickerLine) {
  var fso  = new ActiveXObject("Scripting.FileSystemObject");
  var fh = fso.OpenTextFile("C:\Documents and Settings\me\Desktop\ticker.txt", 8);
  fh.WriteLine(tickerLine);
  fh.Close();
  return false;
}

var delay=1000
var curIndex=0,tId,ticker=[];

function show() {
  document.getElementById("tickerDiv").innerHTML=ticker[curIndex];
  curIndex++;
  if (curIndex>=ticker.length) curIndex=0;
}    
$(function() {
  $.get("ticker.txt",function(data) {
    ticker=data.split("\n");
    show()
    tId =setInterval(function() {
      show()   
    },delay);
  });    
});    
</script>


</head>
<body>
<div style="float:right">
<form onsubmit="return WriteFile(this.tickerLine.value)">
    <input type="text" name="tickerLine"/><input type="submit" />
</form>
</div>
<div id="tickerDiv"></div>
</body>
</html>

Open in new window

0
 
leonnelsonAuthor Commented:
The layout is great - I can type stuff in the text box and have checked to make sure all the files are in the same directory but....ticker.txt does not seem to update.
0
 
leonnelsonAuthor Commented:
its very odd, in the (much less functional) vbs script file

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("C:\Documents and Settings\me\Desktop\ticker.txt", ForAppending, True)
    
    objTextFile.WriteLine(summary)

objTextFile.Close

Open in new window


seems to work but your code (which I'd ultimately like to use)

function WriteFile(tickerLine) {
  var fso  = new ActiveXObject("Scripting.FileSystemObject");
  var fh = fso.OpenTextFile("C:\Documents and Settings\me\Desktop\ticker.txt", 8);
  fh.WriteLine(tickerLine);
  fh.Close();
  return false;

Open in new window

}

doesn't write to the file..(ticker.txt)
puzzling...any ideas - this is really close to exactly what Im looking for.
0
 
Michel PlungjanIT ExpertCommented:
Perhaps we need ,true after the 8

Can you hit F12, script, console and see if there is an error?
0
 
leonnelsonAuthor Commented:
sure -  it says

Line: 43
Char 1
Error Expected ')'
Code 0
0
 
leonnelsonAuthor Commented:
it looked like a bracket was missing in the line "<form onsubmit="return WriteFile(this.tickerLine.value">" so I added it (after value).
Now when I try to submit a query I don't get the 'error on page' message at the bottom left hand corner. But I get a message saying an active x control on this page might be unsafe to interact. I say that I want the interaction to happen but...it still doesn;t update the file :(
0
 
Michel PlungjanIT ExpertCommented:
Which is true.

Please add a ) after .value in the onsubmit
0
 
Michel PlungjanIT ExpertCommented:
Ah, we are cross posting.

Can you save the html file with extension .hta just to see if the message goes away?
0
 
leonnelsonAuthor Commented:
yep, added a ) after .value so its now like this...

<form onsubmit="return WriteFile(this.tickerLine.value)">
but it still won't write to the file. No error message though. Is it possible my browser is stopping the control from running (I don't have admin rights to the browser, so can't change the settings).
I mean the file itself must be open for writing - as the vbs script discussed earlier on could add lines to it...
0
 
leonnelsonAuthor Commented:
haha yes we are. Ok I changed it to .hta, but when I tried to add a line I got an error

Line: 13
Char 3
Error: Bad file name or number.
Code 0
0
 
leonnelsonAuthor Commented:
it doesn't seem to like
var fso  = new ActiveXObject("Scripting.FileSystemObject");

Open in new window

0
 
leonnelsonAuthor Commented:
looking around on the web - people have said that "Only IE with setting in low security level and not safe activexobject allowed to run so it works." so I wonder if thats the issue.
Is there another way of doing this ??
0
 
Michel PlungjanIT ExpertCommented:
So how do you call YOUR vb ?
0
 
leonnelsonAuthor Commented:
just save it as a vbs script and double click on it. clearly your one is better.
0
 
Michel PlungjanIT ExpertCommented:
Do you have a real web server with for example PHP?
0
 
leonnelsonAuthor Commented:
no. Its literally for the person sitting at the pc - so no real wish to 'publish' anything over a network
0
 
Michel PlungjanIT ExpertCommented:
Hmm, perhaps it has to do with you not being admin...
0
 
leonnelsonAuthor Commented:
good point - unfortunately, with regulations - they won't give me the admin password :/
0
 
Michel PlungjanIT ExpertCommented:
Then for now, just edit ticker.txt with note pad or put my original file on a web site with a server process to update the tickers.txt
0
 
leonnelsonAuthor Commented:
ok - and thanks very much for all your help.
0
 
Michel PlungjanIT ExpertCommented:
YW. Lt me know if I can be of assistance with a server solution. On the 7th of jan I'll be back on a windows box. I will see if I can get my script to run and if not, why not.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 19
  • 13
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now