Close files from Access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a database to manage documents. The database record for each document
includes a hyperlink to the document. WIth the normal changes that occur
over time, some of the links are no longer correct. I would to create a
small application that loops through all the records, tests the hyperlinks
and records the invalid links. I know how to follow a hyperlink in code to
open the document. If the link is good then I will end up with an open
document. How do I close that document from code?

Thanks,

David
 
If you can strip the full path out of the hyperlink, you can use

If Len(Dir(FullPath)) = 0 Then
' file doesn't exist
Else
' file exists
End If

Far faster than opening each instance.
 
Thank you for your excellent suggestion. I do store a path as well as the
hyperlink in the database. At first I thougt your suggestion opened up a
new, and perhaps more important test than hyperlink validity, namely does the
document exist and agree with the name/path in the database.

Unfortunately, the documents are on a company ePortal rather than a hard
drive and I can't type the path into Windows Explorer and get anywhere.

To get to a document I have to open Internet Explorer, go to the ePortal and
then click through the folders to navigate to a document. The path that I
store in the database cooresponds to the folder heirarchy on the way to a
document. I can also click on the stored hyperlink and open the document
directly.

The hyperlinks start out: https://eportal.companyname... They don't seem
to have the path embedded in them.

In summary, I don't know how to make your idea work. Also, even if we can
use your test to verify the existance and location of the document, a
hyperlink validity test would still be useful.

Thank again,

David
 
Well, it'll be very slow, but you should be able to use the GetObject
function to retrieve a reference to the open document. For example, assuming
you're dealing with a Word document, you should be able to do something
like:

Dim objWord As Object
Dim objDocument As Object
Dim intLoop As Integer

Set objWord = GetObject(, "Word.Application")

For intLoop = (objWord.Documents.Count - 1) To 0 Step -1
Set objDocument = objWord.Documents(intLoop)
objDocument.Close SaveChanges:=0 ' don't save changes
Next intLoop

objWord.Application.Quit
Set objWord = Nothing
 
Thank you for staying with me. I am getting over my head (code wise) so I am
using your code pretty much as is. What I have right now before your code is:

With Me.cmdOpenFile
.HyperlinkAddress = txtHyperLink 'set the hyperlink address
for the doc
.Hyperlink.Follow 'follow the hyperlink
to the document
.HyperlinkAddress = "" 'reset the hyperlink
address
End With

This code opens the document by following the hyperlink. I put your code
behind this and get two errors.

If I click the command button to run the code I get error 429 'ActiveX
component can't create object'. in line:
Set objWord = GetObject(, "Word.Application")
I think it is trying to execute this code before the document is open.

If I put a stop in this line and wait until the document is open before
proceeding then it executes this line OK and but there is another error later
in line
objDocument.Close SaveChanges:=0 ' don't save changes

The error is Run-time error 4605. 'This method or property is not available
because the document is in another application.'

Is there a fix?

Thanks,

David
 
Hi Alex,

Thanks for jumping in.

Interesting stuff. I created a new module in my application and dropped in
the code you referenced. In my code I substitued the Usage Example (for URL)
they gave in the reference, namely:
'Open URL: ?fHandleFile("http://home.att.net/~dashish", WIN_NORMAL)

I substituted one of the hyperlinks in my document table.

It acted just like my follow hyperlink code. The document opened - and
unfortunately, remained open.

With a successful open the function return value is -1. I think that
indicates success and there seems to be other meaningful possibilities in
code.

So it all looks good with the exception of the original problem of closing
the open document.

Thanks,

David
 
Alex,

Thank you for your help. I have been working with the code you referenced
with some success. I messed around quite a bit with the "Get Class name of a
running app" code trying to figure out what the Class Name was so that I
could supply it to the Close routine. What I found was that all (most?) docs
open in Internet Explorer regardless of what they are and that the Class Name
for IE is "IEFrame". So now I skip the "Get Class Name" function. My code
is simply: fCloseAPP("IEFrame"). It works most of the time. Occasionally
Access freezes. I haven't figured out why. It may be an odd file type or a
very large doc or perhaps just overload and tilt. But I can usually get a
few dozen docs tested before it freezes and so I am working through the list.

Thanks again,

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top