Need to closing running app?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drhowarddrfine
    Recognized Expert Expert
    • Sep 2006
    • 7434

    Need to closing running app?

    In VBScript I have this:
    Code:
    'define vars
    url = "http://bytes.com" 
    path = "C:\dev\vbs\temp.txt"
    'instantiate
    Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set txtfile = fso.OpenTextFile(path, 2, True)
    Set oWS = WScript.CreateObject("WScript.Shell")
    'make request
    xmlhttp.open "GET", url, false 
    xmlhttp.send ""
    'wait for response
    xmlhttp.waitForResponse()
    'if status is 200, then it's OK
    if xmlhttp.status = 200 then
        txtfile.WriteLine(xmlhttp.responseText)
        txtfile.Close
        'enable this to print
        oWS.Run "NotePad.exe /p " + path
        'enable this to just display
        'oWS.Run "notepad.exe " + path    
    else
        'popup bad response, or just omit to end
        WScript.Echo("bad response")
    end if
    'destroy objects.  I'm not sure this is necessary
    Set txtfile = nothing
    Set xmlhttp = nothing
    Set oWS = nothing
    Set fso = nothing
    which will print the file defined in "path". I got a phone call today saying they were unable to print other documents on the printer and had to restart the computer to get it to work. Grasping at straws I need to ask if notepad might still be open and need to be closed?
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    This is a first! Is this the first time you've posted in the ASP forum?

    In answer to your question, the last four lines you posted are used to reclaim memory because ASP has no built in garbage collection mechanism, and failing to put those lines in could cause memory leakage. note this line:
    Code:
    Set oWS = nothing
    This line should close the object that was using notepad, so from the point of view of the server, notepad is done. It sure looks to me like you took care of everything very thoroughly, but it is possible that something prevented notepad from closing for example an error message or a user dialog. And if these interfered with the printing, then that could cause your problem. It seems likely to me that some error or dialog brought on by system settings interfered with the printing and that the error was not caused by the script (which was working up to recently, right?). Give me a while and I can test on my box - notepad should close automatically.

    I think I would ask to clear all print jobs, then watch their server when a request comes in.

    Jared

    Comment

    • drhowarddrfine
      Recognized Expert Expert
      • Sep 2006
      • 7434

      #3
      Originally posted by jhardman
      It sure looks to me like you took care of everything very thoroughly
      (covers insertAlias' eyes) I did do an excellent job, yes.

      What happened is I ran this one night, a few times, and it worked well, but the phone call came the next day, which leads me to wonder if that script caused it.

      The reason for the script is I have a cash register PC where I want it to check for a generated web page on a schedule and print it. I just set WindowsXP scheduler to run the script every minute.

      The only problem I have, right now, is it prints on the default receipt printer instead of the second letter printer. Frinny gave me a link of stuff to read on how to fix that. Haven't done more than glanced at it so if you already know how, I'd appreciate it.

      Comment

      • jhardman
        Recognized Expert Specialist
        • Jan 2007
        • 3405

        #4
        hmm, this script just prints to the default printer. I don't know of a way to change that behavior, but if you could change the default printer for a given application, that might solve the issue. I'm curious though, post Frinny's link here and I'll skim through it, see if I see something obvious.

        Jared

        Comment

        • drhowarddrfine
          Recognized Expert Expert
          • Sep 2006
          • 7434

          #5
          That which shall be known, now and forever, as:
          Frinny's Link

          Comment

          • jhardman
            Recognized Expert Specialist
            • Jan 2007
            • 3405

            #6
            OK, try this:
            Code:
            if xmlhttp.status = 200 then
                '...
                Dim objNetwork, strUNCPrinter
                strUNCPrinter = "\\LittleServer\HP office printer"
                Set objNetwork = CreateObject("WScript.Network")
                objNetwork.SetDefaultPrinter strUNCPrinter
            
                oWS.Run "NotePad.exe /p " + path
            
                strUNCPrinter = "\\cashRegister\receipt printer"
                objNetwork.SetDefaultPrinter strUNCPrinter
            else
                'popup bad response, or just omit to end
                WScript.Echo("bad response")
            end if
            '...
            Let me know if this helps, there is still something else I'm going to look up, might give a better solution.

            Jared

            Comment

            • drhowarddrfine
              Recognized Expert Expert
              • Sep 2006
              • 7434

              #7
              Take your time. I have to make a roadtrip to try it and it won't be today.

              Something I forgot to mention. The receipt printer cannot be interrupted for this printing. It's OK to delay it while the other finishes, though. Also, I'm concerned about changing the default printer for this. If the cash register program wants to print a receipt, will it get messed up as this routine changes it?

              Comment

              • jhardman
                Recognized Expert Specialist
                • Jan 2007
                • 3405

                #8
                Try this variation (specifying the printer to use without changing the default):
                Code:
                'define vars
                dim officePrinter
                officePrinter = "\\server\printer name"
                url = "http://bytes.com" 
                path = "C:\dev\vbs\temp.txt"
                'instantiate
                Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
                Set fso = CreateObject("Scripting.FileSystemObject")
                Set txtfile = fso.OpenTextFile(path, 2, True)
                Set oWS = WScript.CreateObject("WScript.Shell")
                'make request
                xmlhttp.open "GET", url, false 
                xmlhttp.send ""
                'wait for response
                xmlhttp.waitForResponse()
                'if status is 200, then it's OK
                if xmlhttp.status = 200 then
                    txtfile.WriteLine(xmlhttp.responseText)
                    txtfile.Close
                    'enable this to print
                    oWS.Run "NotePad.exe /p " + path + " " + officePrinter
                    'enable this to just display
                    'oWS.Run "notepad.exe " + path    
                else
                    'popup bad response, or just omit to end
                    WScript.Echo("bad response")
                end if
                'destroy objects.  I'm not sure this is necessary
                Set txtfile = nothing
                Set xmlhttp = nothing
                Set oWS = nothing
                Set fso = nothing

                Comment

                • drhowarddrfine
                  Recognized Expert Expert
                  • Sep 2006
                  • 7434

                  #9
                  Cool! I really appreciate it. I'll get down there sometime late tomorrow.

                  Comment

                  • drhowarddrfine
                    Recognized Expert Expert
                    • Sep 2006
                    • 7434

                    #10
                    Finally got around to testing this morning. I had to use a network printer but I don't think that makes any difference. The text file it creates is correct but I get this error:
                    Cannot open the C:\Users\Store\ Desktop\temp.tx t
                    \OFFICE\HPOffic e.txt file
                    Make sure a disk is in the drive you specified.
                    Obviously I changed the location of the file and printer in the script. I can print to that printer from the computer. Perhaps an option is not set right or in the correct place? Haven't looked yet but posting in case someone catches it right away.

                    Comment

                    • drhowarddrfine
                      Recognized Expert Expert
                      • Sep 2006
                      • 7434

                      #11
                      I couldn't even get this to print when I removed everything except the print part. Finally it worked when I made '/p' uppercase. When I went back to the complete script and changed it there, I get the same error above.

                      Comment

                      • jhardman
                        Recognized Expert Specialist
                        • Jan 2007
                        • 3405

                        #12
                        OK, try this line:
                        NotePad.exe /p " + path + " " + officePrinter
                        from the command prompt (where path is the file path and officePrinter is the network address of the printer.

                        Comment

                        • drhowarddrfine
                          Recognized Expert Expert
                          • Sep 2006
                          • 7434

                          #13
                          I think you have a stray ". I removed the first one after /p. I get "Not a valid filename" for: NotePad.exe /p + temp.txt + " " + \\OFFICE\HPOffi ce . This file is on the desktop and I am on the desktop.

                          Comment

                          • drhowarddrfine
                            Recognized Expert Expert
                            • Sep 2006
                            • 7434

                            #14
                            This:
                            Code:
                            notepad.exe /P temp.txt
                            appears to work but I don't have an attached printer. I think it gets its first error with the '+'.

                            Comment

                            • jhardman
                              Recognized Expert Specialist
                              • Jan 2007
                              • 3405

                              #15
                              Originally posted by drhowarddrfine
                              I think you have a stray ". I removed the first one after /p. I get "Not a valid filename" for: NotePad.exe /p + temp.txt + " " + \\OFFICE\HPOffi ce . This file is on the desktop and I am on the desktop.
                              Shouldn't have any pluses in the final version, that is just concatenation left over from the vbscript:
                              NotePad.exe /p temp.txt \\OFFICE\HPOffi ce

                              Comment

                              Working...