ATTN: VB Script experts *CRITIQUE MY CODE PLZ*

Discussion in 'OT Technology' started by Black Light, Aug 17, 2004.

  1. Black Light

    Black Light Guest

    Code:
    '*******************************************************************************
    '* creates IE browser window object
    '*******************************************************************************
    Set objExplorer = CreateObject("InternetExplorer.Application")
    
    '*******************************************************************************
    '* actually open your HTML file
    '*******************************************************************************
    objExplorer.Navigate "file:///c:\TestPrint.html"
    
    '*******************************************************************************
    '* set this to False to make it truly invisible
    '* however this may make grabbing 
    '* window focus below impossible
    '*******************************************************************************
    objExplorer.Visible = False
    
    '*******************************************************************************
    '* pause to wait until browser is done loading page
    '*******************************************************************************
    Do While (objExplorer.Busy)
       Wscript.Sleep 250
    Loop
    
    '*******************************************************************************
    '* Pick which interaction mode you prefer
    '* and comment one out if not needed
    '*******************************************************************************
    OLECMDEXECOPT_PROMPTUSER = 1
    OLECMDEXECOPT_DONTPROMPTUSER = 2
    
    '*******************************************************************************
    '* this is where it actually tells IE browser to open Print dialog
    '*******************************************************************************
    objExplorer.Document.ExecCommand "PRINT", OLECMDEXECOPT_DONTPROMPTUSER
    
    '*******************************************************************************
    '* creates Shell object
    '*******************************************************************************
    Set objShell = CreateObject("WScript.Shell")
    
    '*******************************************************************************
    '* grabs focus of Print window
    '*******************************************************************************
    objShell.AppActivate "Print"
    
    
    '*******************************************************************************
    '* Waits for Print window to load (1500 milliseconds; adjust as needed)
    '*******************************************************************************
    Wscript.Sleep 1500
    
    '*******************************************************************************
    '* Attempts Alt+P to print
    '*******************************************************************************
    objShell.SendKeys "%P"
    
    '*******************************************************************************
    '* Attempts to press Enter key (uncomment this if line above doesn't seem to work)
    '* objShell.SendKeys "{ENTER}"
    '*******************************************************************************
    
    '*******************************************************************************
    '* closes Internet Explorer
    '*******************************************************************************
    objExplorer.Quit
    Set objExplorer = Nothing
    
    '*******************************************************************************
    '* destroys Shell object instance
    '*******************************************************************************
    Set objShell = Nothing
    This is supposed to invisibly open IE and print HTML file c:\TestPrint.html. Any way to make it more elegant? :o
     
  2. Black Light

    Black Light Guest

  3. carlin

    carlin Guest

    not a expert but every line of u're code doesn't require comments...
     
  4. Black Light

    Black Light Guest

    I know. I just wanted to clarify what everything was for :o
     
  5. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    looks pretty straight forward to me... are you sure that sending the Alt-P key combo is the only way to get it to print?
     
  6. Black Light

    Black Light Guest

    That's the best option I could find. I wonder if there's one that can be done without sending any sendkeys :hs: :x:
     
  7. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Overcommenting > undercommenting. Ever tried to edit or update something that's not properly commented? It's absolute hell, almost easier just to start over and throw out the old one.
     
  8. WERUreo

    WERUreo Imua!

    Joined:
    Oct 15, 2003
    Messages:
    566
    Likes Received:
    0
    Location:
    Daytona Beach, Florida
    :werd:
     
  9. Black Light

    Black Light Guest

    :o :)
     
  10. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    I made some changes on an Access application at work last year that was terribly commented, like no comments at all. This year I went to make more changes and decided rather than figuring out what the fuck the guy who wrote it was thinking, it would be easier to start over. It was much easier :embd:
     
  11. Black Light

    Black Light Guest

    I find that inserting random comments for things I understand and then using consistent indentation can do miracles. After you are done doing that, all of a sudden u start seeing the bigger picture and things just fall in together :embd:
     
  12. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Meh, it was easier to add the new features that were needed by starting over anyway.

    That and I was tired of typing "descreption" every time I meant "description" because some ESL fucker messed up when he was designing the tables :mad:
     
  13. Nocera

    Nocera ...

    Joined:
    Aug 9, 2000
    Messages:
    1,307
    Likes Received:
    0
    Location:
    Long Island, NY
    :werd:

    Comments do more harm than good when they redundantly restate what the code says.

    Ex:
    comment: creates IE browser window object
    code: Set objExplorer = CreateObject("InternetExplorer.Application")

    I would limit the comments to this:
    Code:
    Set objExplorer = CreateObject("InternetExplorer.Application")
    
    objExplorer.Navigate "file:///c:\TestPrint.html"
    
    objExplorer.Visible = False
    
    '*************************************************  
    '* pause to wait until browser is done loading page
    '*************************************************  
    Do While (objExplorer.Busy)
       Wscript.Sleep 250
    Loop
    
    '*************************************************  
    '* Pick which interaction mode you prefer and comment one out if not needed
    '*************************************************  
    OLECMDEXECOPT_PROMPTUSER = 1
    OLECMDEXECOPT_DONTPROMPTUSER = 2
    
    objExplorer.Document.ExecCommand "PRINT", OLECMDEXECOPT_DONTPROMPTUSER
    
    Set objShell = CreateObject("WScript.Shell")
    
    objShell.AppActivate "Print"
    
    
    '*************************************************  
    '* Waits for Print window to load (1500 milliseconds; adjust as needed)
    '*************************************************  
    Wscript.Sleep 1500
    
    objShell.SendKeys "%P"
    
    objExplorer.Quit
    Set objExplorer = Nothing
    Set objShell = Nothing
     
  14. Black Light

    Black Light Guest

    good point :coold:
     
  15. Black Light

    Black Light Guest

    :rofl: :rofl: :rofl: :rofl: :ugh: :rofl:

    have u tried doing global replace? :hsugh:
     
  16. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    I would, but it was throughout the code, tables, forms, queries, reports, everything. I changed the ones where it was obvious and visible (mostly on the forms and reports) but decided it wasn't worth the effort to go through every single thing and change it everywhere :o
     
  17. Black Light

    Black Light Guest

    u know u can export everything as plain text, right? :hsugh:
     
  18. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Didn't know that actually :dunno:

    The rewrite >>> old one anyway.
     
  19. Black Light

    Black Light Guest

    now u do... next time try the export in VBA editor :hs:
     

Share This Page