Results 1 to 14 of 14

Thread: [RESOLVED] a race condition???

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Resolved [RESOLVED] a race condition???

    Hi

    I want to use a macro which generates a postscript file and then want to use this created file. For the example i have the folowing code, which first generates a ps file and call it (test.txt) and then wants to open it with notepad.exe.
    VB Code:
    1. Private Function doPS(nameofFile As String) As String
    2. '
    3.  
    4. '
    5.     Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
    6.         wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
    7.         ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
    8.         True, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
    9.         PrintZoomPaperHeight:=0, OutputFileName:=nameofFile, Append:=False
    10.        
    11.     doPS = nameofFile
    12. End Function
    13.  
    14. Sub test()
    15.     Dim f As String
    16.     'f = doPS("C:\test.txt")   '..................(1)
    17.     'f = "C:\test.txt"            '..................(2)
    18.     Shell "notepad.exe " & f
    19. End Sub

    When executing this macro on a .doc file (which contains some text): uncommenting (1): opens notepad with an empty file (so it does not considerate the generated file test.txt)
    now comment (1 ) and uncomment (2) (C:\text.txt exists!) and notepad opens the test.txt perfectly!

    I think it may be a synchronisation problem: shell() executes before doPS() finishes, I searched if there was a wait() function in VBA which waits until doPS() finishes and the Shell() begins...Is there a way to cure this problem? (of course I want to use one and only one macro to do these tasks! )

    thanx for reading this thread
    Last edited by waiso; Jun 26th, 2006 at 04:34 AM.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: a race condition???

    try this:
    VB Code:
    1. 'General Declaration Section
    2. Private Declare Function ShellEx Lib "shell32.dll" Alias _
    3. "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As Any, _
    5. ByVal lpDirectory As Any, ByVal nShowCmd As Long) As Long
    6.  
    7. 'Then your code:
    8. Private Function doPS(nameofFile As String) As String
    9. '
    10. ' Macro2 Macro
    11. ' Macro enregistrée le 6/16/2006 par UniGe
    12. '
    13.     Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
    14.         wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
    15.         ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
    16.         True, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
    17.         PrintZoomPaperHeight:=0, OutputFileName:=nameofFile, Append:=False
    18.        
    19.     doPS = nameofFile
    20. End Function
    21.  
    22.  
    23. Sub test()
    24.     ShellEx Application.hwnd, "open", doPS("C:\test.txt") , "", "", 1
    25. End Sub
    it will open the text file with the associated text viewer...(Whatever your default is)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: a race condition???

    compile error: does not recognise .hwnd...

    But I actuallly want to execute a shell command (a command line application) so not an "open"....
    Last edited by waiso; Jun 23rd, 2006 at 10:24 AM.

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: a race condition???

    hmmm.. you are correct!

    Word has NO hwnd anywhere in it... sorry.

    It would work in excel! lol
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: a race condition???

    put in a loop and wait for a boolean set when dops finished

    or use the sleep api call to wait for a while

    pete

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: a race condition???

    thanx pete,
    but I tried with the loop and the boolean condition...neither worked!
    but how can I use sleep api call? can I do it from Word?

    edited:
    I think i found it, I'wll try it later...

    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    ...
    sleep(1000)
    ...
    right?
    Last edited by waiso; Jun 24th, 2006 at 09:48 AM.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: a race condition???

    VB Code:
    1. Sub test()
    2.     Dim f As String
    3.     f = doPS("C:\test.txt")   '..................(1)
    4.     do while Dir(f) = ""
    5.     loop
    6.     Shell "notepad.exe " & f
    7. End Sub

    this should make sure that the file is completed before notepad tries to open it, you could also put a short sleep in the loop

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Exclamation Re: a race condition???

    thanx guys for the proposed solution but none works!
    I tried it with a sleep, even til 6 sec ,sleep(6000)!
    and I tried it with the loop testing for Dir(f)...

    please help, it's very important!
    thanx

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: a race condition???

    i am unable to test what is happening as i am not sure what you are printing out to, is it a special printer driver??

    i have tried your code using my pdf driver and it works perfectly everytime i even tried killing the file on each loop to make sure it was not reading old info

    pete

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: a race condition???

    so you think it might be a problemof printer?
    well it's a hp laserjet 4200 ps printer and when i am printing, I print it in a file, which gives me the .ps file...
    what's your pdf driver, the adobe distiller?
    thanx for your quick reply.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: a race condition???

    i use cute pdf, to my suprise it worked perfectly with just your code, i didn't even know i could pass it the filename.

    Edit:
    on further examination i find that the file it creates is not actually a pdf file, but a text file

    can you open a normal ps file in notepad and what do you get??

    pete
    Last edited by westconn1; Jun 26th, 2006 at 05:48 AM.

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: a race condition???

    thanx Pete, but it isn't working with cutepdf either, I think I should try it on some other PC to see the result...

  13. #13

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: a race condition???

    OK, here I found a solution in case I have two shell commands and I want them to perform synchronously,i.e., command2 (process 2) will begin to execute once process 1 (command1) terminates:
    http://www.vbforums.com/showthread.php?t=74473

    Anyone knows what to do if instead of command1 I have a function, like in my case (see above!)?

    Thanx

  14. #14

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: a race condition???

    a little lecture isn't bad... I just found out that in my particular problem setting Background:=False will solve my problem!
    Thanx you guys who tried to help me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width