|
-
Jun 23rd, 2006, 04:03 AM
#1
Thread Starter
Member
[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:
Private Function doPS(nameofFile As String) As String
'
'
Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
True, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0, OutputFileName:=nameofFile, Append:=False
doPS = nameofFile
End Function
Sub test()
Dim f As String
'f = doPS("C:\test.txt") '..................(1)
'f = "C:\test.txt" '..................(2)
Shell "notepad.exe " & f
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.
-
Jun 23rd, 2006, 07:31 AM
#2
Re: a race condition???
try this:
VB Code:
'General Declaration Section
Private Declare Function ShellEx Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As Any, _
ByVal lpDirectory As Any, ByVal nShowCmd As Long) As Long
'Then your code:
Private Function doPS(nameofFile As String) As String
'
' Macro2 Macro
' Macro enregistrée le 6/16/2006 par UniGe
'
Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
True, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0, OutputFileName:=nameofFile, Append:=False
doPS = nameofFile
End Function
Sub test()
ShellEx Application.hwnd, "open", doPS("C:\test.txt") , "", "", 1
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"
-
Jun 23rd, 2006, 09:34 AM
#3
Thread Starter
Member
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.
-
Jun 23rd, 2006, 10:21 AM
#4
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"
-
Jun 24th, 2006, 01:28 AM
#5
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
-
Jun 24th, 2006, 09:38 AM
#6
Thread Starter
Member
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.
-
Jun 24th, 2006, 09:11 PM
#7
Re: a race condition???
VB Code:
Sub test()
Dim f As String
f = doPS("C:\test.txt") '..................(1)
do while Dir(f) = ""
loop
Shell "notepad.exe " & f
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
-
Jun 26th, 2006, 04:34 AM
#8
Thread Starter
Member
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
-
Jun 26th, 2006, 05:03 AM
#9
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
-
Jun 26th, 2006, 05:12 AM
#10
Thread Starter
Member
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.
-
Jun 26th, 2006, 05:42 AM
#11
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.
-
Jun 26th, 2006, 06:00 AM
#12
Thread Starter
Member
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...
-
Jul 5th, 2006, 07:11 AM
#13
Thread Starter
Member
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
-
Jul 6th, 2006, 10:00 AM
#14
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|