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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davidson1
    New Member
    • Feb 2008
    • 144

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

    Hi friends,

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

    Code:
    <script language="JavaScript">
    var myApp = new ActiveXObject("Excel.Application");
    
                if (myApp != null)
    
                {
                      myApp.visible = true;
                      myApp.workbooks.open("C:\Spreadsheet.xls");
    
    
    
                }
    <script>
    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?
    .
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    I don't know if this is possible with JScript, but have you tried SendKeys?

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      You'd have to instantiate a WScript object which will allow you to use SendKeys.

      Comment

      • davidson1
        New Member
        • Feb 2008
        • 144

        #4
        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

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          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.

          Comment

          • davidson1
            New Member
            • Feb 2008
            • 144

            #6
            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



            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.

            Code:
            <script language="JScript">
            var myApp = new ActiveXObject("Excel.Application");
            myApp.visible = true;
            myApp.Workbooks.Open("C:\\ROME Literature Spreadsheet.xls");
            Set WshShell = WScript.CreateObject("WScript.Shell")
            WshShell.SendKeys "Y"
            </script>
            Last edited by Rabbit; Mar 7 '12, 05:17 PM. Reason: Fixed code tags.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              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.

              Comment

              • davidson1
                New Member
                • Feb 2008
                • 144

                #8
                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"

                Code:
                var myApp = new ActiveXObject("Excel.Application"); 
                myApp.visible = true; 
                myApp.Workbooks.Open("C:\\ROME Literature Spreadsheet.xls");
                var shell = new ActiveXObject("WScript.shell");
                Wscript.sleep(8000);
                shell.AppActivate("C:\\ROME Literature Spreadsheet.xls");
                shell.SendKeys('Y');
                I am accessing this file from Command Line like below

                C:\> cscript keywsh.js

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  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.

                  Comment

                  • davidson1
                    New Member
                    • Feb 2008
                    • 144

                    #10
                    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.
                    Code:
                    var myApp = new ActiveXObject("Excel.Application");
                    myApp.visible = false;
                    var shell = new ActiveXObject("WScript.shell");
                    shell.run('"C:\\ROME Literature Spreadsheet.xls"',1);
                    WScript.sleep(5000);
                    shell.SendKeys("y");
                    myApp.ActiveWorkbook.SaveCopyAs("C:\\goon\\today.xls"); 
                    shell.SendKeys("%{F4}");

                    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

                    Code:
                    import java.io.FileReader;
                    import javax.script.ScriptEngine;
                    import javax.script.ScriptEngineManager;
                    import javax.script.Bindings;
                    import javax.script.ScriptContext;
                    import javax.script.ScriptException;
                    
                    
                    public class script1 {
                      public static void main(String[] args) {
                        ScriptEngineManager manager = new ScriptEngineManager();
                        ScriptEngine engine = manager.getEngineByName("js");
                        try {
                          FileReader reader = new FileReader("C:\\keywsh.js");
                          engine.eval(reader);
                          reader.close();
                        } catch (Exception e) {
                          e.printStackTrace();
                        }
                      }
                    }
                    It is throwing this Error


                    javax.script.Sc riptException: sun.org.mozilla .javascript.int ernal.EcmaError : Ref
                    erenceError: "ActiveXObj ect" is not defined. (<Unknown source>#4) in <Unknown so
                    urce> at line number 4
                    at com.sun.script. javascript.Rhin oScriptEngine.e val(RhinoScript Engine.ja
                    va:110)
                    at javax.script.Ab stractScriptEng ine.eval(Abstra ctScriptEngine. java:232)

                    at script1.main(sc ript1.java:18)


                    Can you help me to solve this?

                    3) When I use the below script code

                    myApp.ActiveWor kbook.SaveCopyA s("C:\\goon\\to day.xls");

                    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

                    Comment

                    • Rabbit
                      Recognized Expert MVP
                      • Jan 2007
                      • 12517

                      #11
                      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.

                      Comment

                      Working...