473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Need JavaScript Code to Press Letter "Y" by default not by Keyboard

144 100+
Hi friends,

I have a below JavaScript code which will open an excel sheet.

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2. var myApp = new ActiveXObject("Excel.Application");
  3.  
  4.             if (myApp != null)
  5.  
  6.             {
  7.                   myApp.visible = true;
  8.                   myApp.workbooks.open("C:\Spreadsheet.xls");
  9.  
  10.  
  11.  
  12.             }
  13. <script>
  14.  
  15.  
After opening a Excel sheet. I need a Javascript code which need to Press letter Y(ascii code = 89) defaultly through Javascript Code not by keyboard.

Can anyone tell me that Javascript code?
.
Feb 13 '12 #1
10 3244
acoder
16,027 Expert Mod 8TB
I don't know if this is possible with JScript, but have you tried SendKeys?
Feb 27 '12 #2
Rabbit
12,516 Expert Mod 8TB
You'd have to instantiate a WScript object which will allow you to use SendKeys.
Feb 27 '12 #3
davidson1
144 100+
Hi acoder/Rabbit,

Thanks for your Reply.. I tried by Java Script and JQuery but it is not working. I dont have any idea about "SendKeys". Can you please provide me code for it, I will try in my end and let you know for it.

Thanks in Advance
Mar 6 '12 #4
Rabbit
12,516 Expert Mod 8TB
First of all, it sounds like you're trying to run this through a browswer, you can't. That kind of stuff is blocked for security reasons. If you need it to run in a browser, forget it, it's not going to happen.

For it to run, you would have to use the windows script host.

And it's only 3 steps.
1) Declare a variable to hold a WScript.Shell object.
2) Instantiate the object.
3) Call the send keys method.

I don't give code unless the poster has shown some effort on their part, usually by posting their attempt.
Mar 6 '12 #5
davidson1
144 100+
Hi Rabbit,

Sorry.. I will so some effort... I want to make My requirement clear

1) A one Particular Excel sheet should be opened
2) Once it is opened there will be a Message Box displayed as shown below

http://www.freeimagehosting.net/8cdn9

If we press "YES" button, it will show Excel Data. If we also Press "Y" Keyword ( Shortcut Key for "YES" Button) , it will Show Excel Data.

Everytime we need to Press it Manually. So it order to avoid that I want to make it automated.So no need to press manually.

So As you suggested , it can be done with Windows Script Host. I am sorry I dont have knowledge about it. But I searched in Google. I tried to install Windows Script Host 5.6. But it said Setup Error. It said Service Pack Version of this system is new.

I am using "Windows XP Service Pack 3". Please guide me to istall which sofware for it.


I searched in Google and found the below Code Which I am going to execute.. Please correct me if it is wrong.

Expand|Select|Wrap|Line Numbers
  1. <script language="JScript">
  2. var myApp = new ActiveXObject("Excel.Application");
  3. myApp.visible = true;
  4. myApp.Workbooks.Open("C:\\ROME Literature Spreadsheet.xls");
  5. Set WshShell = WScript.CreateObject("WScript.Shell")
  6. WshShell.SendKeys "Y"
  7. </script>
Mar 7 '12 #6
Rabbit
12,516 Expert Mod 8TB
The scripting host is installed by default, you dont' have to install it, you just need to use it instead of a browser. Your code won't work because you're mixing html syntax with javascript syntax with vbscript syntax. Drop the html tags, lines 1 and 7. Port the vbscript to javascript, lines 5 and 6. You'll also need a delay in there before the send keys, otherwise it won't send at the right time. You may also want to activate the application before sending it in case the user clicks elsewhere in the mean time.
Mar 7 '12 #7
davidson1
144 100+
Hi Rabbit,

Thanks a lot for your Suggestion.. When I used this below code..Still it is not Working... I need your help to acheive this successfully. The below file name is "keywsh.js"

Expand|Select|Wrap|Line Numbers
  1.  
  2. var myApp = new ActiveXObject("Excel.Application"); 
  3. myApp.visible = true; 
  4. myApp.Workbooks.Open("C:\\ROME Literature Spreadsheet.xls");
  5. var shell = new ActiveXObject("WScript.shell");
  6. Wscript.sleep(8000);
  7. shell.AppActivate("C:\\ROME Literature Spreadsheet.xls");
  8. shell.SendKeys('Y');
  9.  
  10.  
I am accessing this file from Command Line like below

C:\> cscript keywsh.js
Mar 7 '12 #8
Rabbit
12,516 Expert Mod 8TB
It's probably because you're trying to use the WScript access when you called it from CScript. But I have no idea because you didn't say what the error message was.

But you don't have to run it from the command prompt, you can just double click the file and it will run in wscript.
Mar 7 '12 #9
davidson1
144 100+
Hi Rabbit,

Thanks a lot for ur Suggestion.It is working now. I directly double clicked the keywsh.js file. I have been trying this for long days.I have acheived this through your Help. Many Many Credits to you.The below is the code of "keywsh.js".The below code opens a Excel file and a Message Box appears and it is bypassed by "Y" Sendkey and after that data is displayed. and I am doing "Save as" option and saving it in New Excel file "today.xls" with data.
Expand|Select|Wrap|Line Numbers
  1. var myApp = new ActiveXObject("Excel.Application");
  2. myApp.visible = false;
  3. var shell = new ActiveXObject("WScript.shell");
  4. shell.run('"C:\\ROME Literature Spreadsheet.xls"',1);
  5. WScript.sleep(5000);
  6. shell.SendKeys("y");
  7. myApp.ActiveWorkbook.SaveCopyAs("C:\\goon\\today.xls"); 
  8. shell.SendKeys("%{F4}");
  9.  

I have still some doubts, I will be grateful if you clear it.

1)

I have a below partial code.It is opening the Excel file Visibly

shell.run('"C:\\ROME Literature Spreadsheet.xls"',1);

Is it possible to make the opening of excel file invisible or hide it?

2)

I want to access this "keywsh.js" file from Java..
when I tried to run from below Java code

Expand|Select|Wrap|Line Numbers
  1. import java.io.FileReader;
  2. import javax.script.ScriptEngine;
  3. import javax.script.ScriptEngineManager;
  4. import javax.script.Bindings;
  5. import javax.script.ScriptContext;
  6. import javax.script.ScriptException;
  7.  
  8.  
  9. public class script1 {
  10.   public static void main(String[] args) {
  11.     ScriptEngineManager manager = new ScriptEngineManager();
  12.     ScriptEngine engine = manager.getEngineByName("js");
  13.     try {
  14.       FileReader reader = new FileReader("C:\\keywsh.js");
  15.       engine.eval(reader);
  16.       reader.close();
  17.     } catch (Exception e) {
  18.       e.printStackTrace();
  19.     }
  20.   }
  21. }
  22.  
It is throwing this Error


javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: Ref
erenceError: "ActiveXObject" is not defined. (<Unknown source>#4) in <Unknown so
urce> at line number 4
at com.sun.script.javascript.RhinoScriptEngine.eval(R hinoScriptEngine.ja
va:110)
at javax.script.AbstractScriptEngine.eval(AbstractScr iptEngine.java:232)

at script1.main(script1.java:18)


Can you help me to solve this?

3) When I use the below script code

myApp.ActiveWorkbook.SaveCopyAs("C:\\goon\\today.x ls");

sometimes if there are Multiple Excel file is opened..it is getting confused and pointing to some other Excel file.

I want to point to "ROME Literature Spreadsheet.xls" only which is opened at that time

is that possible?


Thanks a lot in Advance
Mar 8 '12 #10
Rabbit
12,516 Expert Mod 8TB
These questions are no longer within the scope of the original post. It is forum policy to have one question per thread. Please create a new thread for each question. Your second question should go in the Java forum and not the javascript forum as they are different languages.
Mar 8 '12 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: midiweb01 | last post by:
Sorry for my bad english here! I try my best. I would like to make a slideshow for my site about paintings. I have four menus: Portraits-landscapes-Still Life-Animals Each Menu has his own...
3
by: TC | last post by:
Hello, I am using the script below to open a new window and once opened, redirect to that open window from the original window: private void btnNewPDFWindow_Click(object sender,...
2
by: marcia | last post by:
How can I capture (i.e., write an event handler) that runs after I press the "Enter" key? I'd like to prevent a Web Form from being submitted when the "Enter" key is pressed following the typing...
1
by: Andrea Williams | last post by:
I've created a simple search form and a submit button. On click of the button, I have code-behind that runs and fills a datagrid. However, it the person is focused on one of the other controls...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
8
by: Rasmus Kromann-Larsen | last post by:
The With Conundrum I'm currently writing a master thesis on (preparations for) static analysis of JavaScript, and after investigating the with statement, it only even more evident to me that the...
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
12
by: Uwe Braunholz | last post by:
Hello, working on a asp.net Website brought me to a strange problem. I want to enable my users to pass a search string via the query string of an url. It works if the user calls the URL...
3
by: neelsfer | last post by:
I have this timing program for sport and have a "button" method to add times, but also need to add finish times using the keyboard, under certain circumstances. Its working fine, but i currently...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.