|
-
Dec 11th, 2008, 02:01 AM
#1
Thread Starter
Addicted Member
VB6: Unable to open file. Check file name and permissions
Hi everyone,
The problem is that i am creating a tif file at runtime and want to open it with Microsoft Document and Imaging. The File is created No Probs... but the following error comes when i use the shell command to open it with Microsoft Document and Imaging Program:
"Unable to open file. Check file name and permissions".
Otherwise, if i close the form and run again it opens fine.. But thing to note here is that this time the file is already there and not created again.
Thanks a lot
irfi
-
Dec 11th, 2008, 02:32 AM
#2
Re: VB6: Unable to open file. Check file name and permissions
I believe you need to save it first before an external application could open it, are you doing that?
-
Dec 11th, 2008, 04:42 AM
#3
Thread Starter
Addicted Member
Re: VB6: Unable to open file. Check file name and permissions
yes as i mentioned the file is created at runtime and saved in " C:\ "No Problem!!!!
The only prob is it is not opening...............
But if i open it with the program externelly meaning outside VB it opens and working fine..
Thanx a lot!!!
-
Dec 11th, 2008, 04:58 AM
#4
Fanatic Member
Re: VB6: Unable to open file. Check file name and permissions
I think you are not releasing the object that created your file. Try disposing off that object and then open the file. Cheers!
-
Dec 11th, 2008, 06:27 AM
#5
Re: VB6: Unable to open file. Check file name and permissions
Letting us see how you are doing it should let us understand more your problem and probably could show you a workaround.
-
Dec 12th, 2008, 05:50 AM
#6
Thread Starter
Addicted Member
Re: VB6: Unable to open file. Check file name and permissions
how do i release the object can u pls explain.
I need to open file as soon as it is created......
what i am doing is that........\
On click event of the button, a new file is created and the name of the file is same as the textbox.text on the form. "WORKING FINE"
Now to open the file i use the shell command..
Shell "C:\Program Files\Common Files\Microsoft Shared\MSPaper\MSPVIEW.EXE " & "c:\test.tif", vbMaximizedFocus
Here is the problem i get error message. "Unable to open file. Check filename and permission:.
If i reload the form and then use shell command agian its working fine.
Any suggestions.
thanx again
-
Dec 12th, 2008, 06:26 AM
#7
Fanatic Member
Re: VB6: Unable to open file. Check file name and permissions
Pls mention your code for creating the file so that we know what kind of object you are using, which needs to be disposed.
-
Dec 13th, 2008, 02:32 AM
#8
Thread Starter
Addicted Member
Re: VB6: Unable to open file. Check file name and permissions
here the code i used to create a file
Shell "C:\Program Files\PDFMerger\PDFMerge.exe /d c:\PDFdirectory\ /o " & "c:\" & TKNo & ".pdf"
where:
1- PDFMerger is an exe that merges pdf files.
2- TKNo is a new pdf file created after merging pdf files from the pdfdirectory
3- TKNo = txtfilename.text
4- /d will take the files from pdfdirectory to merge them.
5- /o is the output location where i want to save the file.
Now coming back to my inquiry...
1- when this file is created i cannot open it. it gives error message as i mentioned earlier.
2- i would also like to know how to make the "pdfdirectory" name as dynamic instead of me specifying it. I mean to say how can i put a code that will create a folder with the same name as filename. I tried but since the name of the directory is within the two quotes " " it is not recognizing the name. I am using fso to create new folder.
Shell "C:\Program Files\PDFMerger\PDFMerge.exe /d & "c:\" & TKNo \ /o " & "c:\" & TKNo & ".pdf"
Thanking you for all the help!!!!!!!!!!!!!!1
-
Dec 13th, 2008, 05:46 PM
#9
Re: VB6: Unable to open file. Check file name and permissions
Most likely cause is that the PDFMerger exe is still using the file. It may need to be closed/shutdown before another app can access the new file.
Another possible cause is because the Shell command executes programs asyncronously and control returns to VB almost immediately, the Imaging program cannot access the file because the PDFMerger program has yet to finish creating the file. Adding a delay between the two Shell commands migth solve the problem.
As for the dynamic folder problem, try this
Code:
Shell "C:\Program Files\PDFMerger\PDFMerge.exe /d c:\" & TKNo & "\ /o c:\" & TKNo & ".pdf"
-
Dec 13th, 2008, 10:03 PM
#10
Re: VB6: Unable to open file. Check file name and permissions
Could you try opening it like this? (Hand coded so pls. check for any errors)
Code:
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Const INFINITE = &HFFFF 'Wait forever
Private Sub Test()
Dim lPid As Long
lPid = Shell("C:\Program Files\PDFMerger\PDFMerge.exe /d c:\PDFdirectory\ /o " & "c:\" & TKNo & ".pdf")
If lPid <> 0 Then
lHnd = OpenProcess(SYNCHRONIZE, 0, lPid) 'Get a handle to the shelled process.
If lHnd <> 0 Then 'If successful, wait for the
lRet = WaitForSingleObject(lHnd, INFINITE) ' application to end.
CloseHandle (lHnd) 'Close the handle.
End If
End If
End Sub
That should wait for the process until it is finished doing what it is supposed to do.
-
Dec 14th, 2008, 09:13 AM
#11
Thread Starter
Addicted Member
Re: VB6: Unable to open file. Check file name and permissions
Thanx Guyz!!!! I will try and let u know..
Thanx again
irfi
-
Dec 14th, 2008, 12:56 PM
#12
Thread Starter
Addicted Member
Re: VB6: Unable to open file. Check file name and permissions
Thanx a Million GUYZ for all your help!!! It worked for me...
The only prob i am facing is that the error message is still poping-up! "Unable to open file. Check filename and permissions".
I tried the code u gave dee-u....
-----------------------------------------------------------
Private Sub Command1_Click()
Dim lPid As Long
lPid = Shell("C:\Program Files\PDFMerger\PDFMerge.exe /d c:\PDFdirectory\ /o " & "c:\" & TKNo & ".pdf")
If lPid <> 0 Then
lHnd = OpenProcess(Synchronize, 0, lPid) 'Get a handle to the shelled process.
If lHnd <> 0 Then 'If successful, wait for the
lRet = WaitForSingleObject(lHnd, INFINITE) ' application to end.
CloseHandle (lHnd) 'Close the handle.
End If
End If
If CloseHandle(lHnd) = True Then
Shell "c:\Program Files\Adobe\Acrobat 5.0\Acrobat\Acrobat.exe " & "c:\" & TKNo & ".pdf"
Else
MsgBox " Handle not closed"
End If
End Sub
----------------------------------------------
I have put a message box to see if if (lHnd) was closed but it always pops-up the message box meaning the (lHnd) is not completely closed... I Guess
Thanx again
irfi
-
Dec 14th, 2008, 10:39 PM
#13
Re: VB6: Unable to open file. Check file name and permissions
And you said it works but still errors, what exactly did you mean by that?
-
Dec 15th, 2008, 12:22 AM
#14
Re: VB6: Unable to open file. Check file name and permissions
I have modified your code to determine if the CloseHandle was successful or not, CloseHandle returns zero if it was not successful, non-zero if it was successful.
Code:
Private Sub Command1_Click()
Dim lPid As Long
Dim lRet As Long
lPid = Shell("C:\Program Files\PDFMerger\PDFMerge.exe /d c:\PDFdirectory\ /o " & "c:\" & TKNo & ".pdf")
If lPid <> 0 Then
lHnd = OpenProcess(Synchronize, 0, lPid) 'Get a handle to the shelled process.
If lHnd <> 0 Then 'If successful, wait for the
lRet = WaitForSingleObject(lHnd, INFINITE) ' application to end.
lRet = CloseHandle(lHnd) 'Close the handle.
If lRet <> 0 Then 'CloseHandle was successful
Shell "c:\Program Files\Adobe\Acrobat 5.0\Acrobat\Acrobat.exe " & "c:\" & TKNo & ".pdf"
End If
End If
End If
End Sub
-
Dec 15th, 2008, 06:14 AM
#15
Fanatic Member
Re: VB6: Unable to open file. Check file name and permissions
 Originally Posted by brucevde
Most likely cause is that the PDFMerger exe is still using the file. It may need to be closed/shutdown before another app can access the new file.
Another possible cause is because the Shell command executes programs asyncronously and control returns to VB almost immediately, the Imaging program cannot access the file because the PDFMerger program has yet to finish creating the file. Adding a delay between the two Shell commands migth solve the problem.
As for the dynamic folder problem, try this
Code:
Shell "C:\Program Files\PDFMerger\PDFMerge.exe /d c:\" & TKNo & "\ /o c:\" & TKNo & ".pdf"
What brucevde has mentioned is absolutely correct. You cannot open the file untill PDFMerger is done with creating your file. And btw, why are you using APIs when you have easy-to-use System.Diagnostics.Process with WaitForExit method. Try this:
Code:
Dim p As Process
p = System.Diagnostics.Process.Start("C:\Program Files\PDFMerger\PDFMerge.exe /d c:\PDFdirectory\ /o " & "c:\" & TKNo & ".pdf")
p.WaitForExit() 'this will assure that the application waits till PDFMerger is through with creating the file
-
Dec 15th, 2008, 06:23 AM
#16
Re: VB6: Unable to open file. Check file name and permissions
 Originally Posted by pvbangera
What brucevde has mentioned is absolutely correct. You cannot open the file untill PDFMerger is done with creating your file. And btw, why are you using APIs when you have easy-to-use System.Diagnostics.Process with WaitForExit method. Try this:
Code:
Dim p As Process
p = System.Diagnostics.Process.Start("C:\Program Files\PDFMerger\PDFMerge.exe /d c:\PDFdirectory\ /o " & "c:\" & TKNo & ".pdf")
p.WaitForExit() 'this will assure that the application waits till PDFMerger is through with creating the file
Hey pvbangera, I believe this is VB6.0, your recommendation is for .Net.
-
Dec 15th, 2008, 06:28 AM
#17
Fanatic Member
Re: VB6: Unable to open file. Check file name and permissions
Oops...sorry... my bad. A VB6 question in DB section to which a .Net answer is posted..hilarious
-
Dec 15th, 2008, 12:20 PM
#18
Thread Starter
Addicted Member
Re: VB6: Unable to open file. Check file name and permissions
Thank you dee-u - U ROCK
After a little modification it worked for me and all credits to you coz u wrote the code. I had searched extensively on the net about this issue and finally this is the best solution to w start the second process only after the first one is completed.
Here is the code:
---------------
Option Explicit
Const SYNCHRONIZE = &H100000
Const INFINITE = &HFFFF 'Wait forever
Const WAIT_OBJECT_0 = 0 'The state of the specified object is signaled.
Const WAIT_TIMEOUT = &H102 'The time-out interval elapsed, and the
'object’s state is nonsignaled.
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
-------------------------------
Private Sub Command1_Click()
Dim lPid As Long
Dim lHnd As Long
Dim lRet As Long
lPid = Shell("C:\Program Files\PDFMerger\PDFMerge.exe /d c:\PDFdirectory\ /o " & "c:\" & TKNo & ".pdf")
If lPid <> 0 Then
lHnd = OpenProcess(SYNCHRONIZE, 0, lPid) 'Get a handle to the shelled process.
If lHnd <> 0 Then 'If successful, wait for the
lRet = WaitForSingleObject(lHnd, INFINITE) ' application to end.
CloseHandle (lHnd) 'Close the handle.
End If
Shell "c:\Program Files\Adobe\Acrobat 5.0\Acrobat\Acrobat.exe " & "c:\" & TKNo & ".pdf"
End If
End Sub
-----------------------------------------
Just wondering if i can monitor the shell process with a progress bar or show a progress bar for the entire click_event of the Button
Thanx a zillion!!!!!!!!!
irfi
-
Dec 22nd, 2008, 01:01 AM
#19
Thread Starter
Addicted Member
Re: VB6: Unable to open file. Check file name and permissions
Hi guyz...........
what if the source and the output folders are on remote machine.
For local Machine
Shell "C:\Program Files\PDFMerger\PDFMerge.exe /d c:\" & TKNo & "\ /o c:\" & TKNo & ".pdf"
For Remote Machine
Shell "C:\Program Files\PDFMerger\PDFMerge.exe /d \\MyPc\source" & TKNo & "\ /o \\MyPc\output" & TKNo & ".pdf"
where source and output are shared folders on remote machine MyPc. But it is not working it says need atleast one file to merge which means that the path is not correct.
Much appreciated!!!
Thanx
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
|