Close Open PDF or JPG using VBA

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Hi
I have a function which detects if a particular file is open, this works correctly. What I can't figure out is how to close the file using VBA, the files are images (usually jpgs) and pdfs.
Any ideas?
Dannielle
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
If you opened the file with FSO (File Scripting Object) then you would have a file number that you can use to close it? VBA close method doesn't close files, so maybe post your code for clarity. Please use code tags (vba button on posting toolbar) to maintain indentation and readability.
 
Upvote 0
If you opened the file with FSO (File Scripting Object) then you would have a file number that you can use to close it? VBA close method doesn't close files, so maybe post your code for clarity. Please use code tags (vba button on posting toolbar) to maintain indentation and readability.

This is the beginning of the procedure code followed by the function code.
The function works but the Open OrgNamePath For Input As #1 Close #1 and Open NewNamePath For Input As #2 section does not, I found this somewhere on the net and obviously I have it wrong.
There are 2 related documents that might have been opened their path names are held here OrgNamePath and NewNamePath
please let me know if I have missed anything.
Dannielle
VBA Code:
Sub SaveFileProcedure(OrgNamePath As String, NewNamePath As String)
Dim Cnt_ID As Integer, PassData As String
          
    If IsFileOpen(OrgNamePath) = True Then
        Open OrgNamePath For Input As #1
        Close #1
    End If
    If IsFileOpen(NewNamePath) = True Then
        Open NewNamePath For Input As #2
        Close #2
    End If
..........

VBA Code:
Function IsFileOpen(PathName As String)
    Dim iFilenumber As Long
    Dim iErr As Long
    
    On Error Resume Next
    iFilenumber = FreeFile()    ' Assign a free file number.
    ' Attempt to open the file and lock it.
    Open PathName For Input Lock Read As #iFilenumber
    Close iFilenumber
    iErr = Err              ' Assign the Error Number which occured
    On Error GoTo 0
    
    Select Case iErr
        Case 0:    IsFileOpen = False
        Case 70:   IsFileOpen = True
        Case Else:
           
            Error iErr
    End Select
    
End Function
 
Upvote 0
Did a bit of testing with your code. Why would you try to edit the file (input) if it is open when all you want to do (I think) is close it if it's open?
If IsFileOpen(OrgNamePath) = True Then
... Open OrgNamePath For Input As #1 <<< ?
 
Upvote 0
Did a bit of testing with your code. Why would you try to edit the file (input) if it is open when all you want to do (I think) is close it if it's open?
If IsFileOpen(OrgNamePath) = True Then
... Open OrgNamePath For Input As #1 <<< ?
I need an emoji for um I don't know 🥴. Honestly I found this code somewhere on the net months ago. I presumed it was to do with numbering the open files. I never sorted the issue and went of working on other sections of my code knowing this was an issue but not my biggest issue. Now I can't find where I got this code from (note to self..... make a note in code) Please educate me or at least point me in the right direction. I'm self taught starting with excel and now access, I have found there is so much on excel vba compared to access unfortunately.
Dannielle
 
Upvote 0
So now Access is involved? FWIW, I'm better at that but have no idea how that came into the picture.
Perhaps if you explained in broad terms what it is you want to do/have happen. To say you want to close a file is too narrow because I have no idea what application you're doing this in now, nor how the file is opened (assuming that is part of the process) or if it's just to check that if a file is open (and what type of file it is), or if it's just to check if it exists, or ....
If it's opened and it's visible, it's visible in its native application, so why not just click on the application close button??
Lotsa questions.

Yes, when you copy code, it's a good idea to include a comment (link to the site)
 
Upvote 0
So now Access is involved? FWIW, I'm better at that but have no idea how that came into the picture.
Perhaps if you explained in broad terms what it is you want to do/have happen. To say you want to close a file is too narrow because I have no idea what application you're doing this in now, nor how the file is opened (assuming that is part of the process) or if it's just to check that if a file is open (and what type of file it is), or if it's just to check if it exists, or ....
If it's opened and it's visible, it's visible in its native application, so why not just click on the application close button??
Lotsa questions.

Yes, when you copy code, it's a good idea to include a comment (link to the site)
Sorry I presumed that it was known as it is the Access Notice Board, sorry for the confusion.
The Database is basically a filing system. The form opens the browser, the user selects a file, it is renamed and moved to the relevant folder. A second form is used to add file relevant information, description, people linked to file, etc and finally if the file is an image the exif data is updated. The code I'm asking for help with is to close the file, I thought, possibly incorrectly, that it would be simple to close a file as I used code to open it. I can use a message box to remind the user to close the associate file (pdf or image, predominantly jpg) if need be.
The particular code in issue...
The code tests if the path exists, then if the file is open, then I would like to close the file. This is so it can be renamed and have the exif (images only) information changed. If the user forgets that the file is open an error is generated so I am trying prevent this issue.
Dannielle
 
Upvote 0
Not really sure you can close a file if you have not opened it?
 
Upvote 0
Sorry I presumed that it was known as it is the Access Notice Board, sorry for the confusion.
Confusion is mine as I missed that because of having multiple tabs open on this site. Unfortunately I have no knowledge about how to test for open pdf's or image files. Unless you can edit the pdf because you have full Adobe, I don't see the point in testing for Input. Same goes for image files, because I'm under the impression that opening for Input is for text files and not images (and maybe not pdf's either). So maybe take a step back and describe the want rather than how to do something that may not even be necessary.
The only other avenue I'm aware of that might be of use regarding image files is Windows Image Aquisition (WIA) code - but certainly not for pdf's.
 
Upvote 0
Confusion is mine as I missed that because of having multiple tabs open on this site. Unfortunately I have no knowledge about how to test for open pdf's or image files. Unless you can edit the pdf because you have full Adobe, I don't see the point in testing for Input. Same goes for image files, because I'm under the impression that opening for Input is for text files and not images (and maybe not pdf's either). So maybe take a step back and describe the want rather than how to do something that may not even be necessary.
The only other avenue I'm aware of that might be of use regarding image files is Windows Image Aquisition (WIA) code - but certainly not for pdf's.
Thanks for your time. I think a message box maybe the simplest option :)
Dannielle
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,991
Members
452,541
Latest member
haasro02

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top