Avatar billede pbudk Nybegynder
17. juli 2002 - 20:29 Der er 17 kommentarer og
1 løsning

Åbne ekstern fil fra Access

Hej

Jeg har et tekstfelt i min db, hvor stien til et ekstern dokument ligger.

Spørgsmålet er nu:
Hvordan åbner jeg dokumentet ved at klikke på feltet i dataarkvisning ?

Håber der er nogen der kan hjælpe

/pbudk
Avatar billede terry Ekspert
17. juli 2002 - 20:39 #1
'Place this in a module
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As
Long) As Long


Public Const SW_HIDE = 0
Public Const SW_SHOW = 5


'This is behind a button on the form. When you select a record you can press th ebutton to open the document

Private Sub Command1_Click()
Dim stAppPath As String
Dim stAppName As String
Dim result As Integer

    On Error GoTo Err_Command1_Click

    stAppName = Me.SomeFieldOnTheForm
   
   
    result = ShellExecute(0, "Open", stAppName, "", "", SW_SHOW)

    If result < 32 Then
        MsgBox "Couldn't open file ..."
    End If
   
Exit_Command1_Click:
    Exit Sub

Err_Command1_Click:
    MsgBox Err.Description
    Resume Exit_Command1_Click
   
End Sub
Avatar billede terry Ekspert
17. juli 2002 - 20:40 #2
SomeFieldOnTheForm needs to be changed to the correct field name
Avatar billede pbudk Nybegynder
17. juli 2002 - 20:55 #3
This is my first serious project in access so please have patience if I ask stupid questions.
But how do I execute the module ?

I have named the module: openFile and created an onclick event for the field.
The event looks like this:

Private Sub fFil_Click()
openFile()
End Sub

But I get a syntax error - what have I done wrong ?

By the way - thanks for helping so quickly :o)

/pbudk
Avatar billede mugs Novice
17. juli 2002 - 20:56 #4
Idfet jeg ikke mener der er nogen grund til at åbne et Word-dokument uden samtidig at overføre data fra Access, kan flg kode benyttes. Den åbner et berstemt Word-dokument, og overfører samtidig formularens data til word:

Dim objword As New Word.Application
Dim WordDoc As New Word.Document
Set WordDoc = objword.Documents.Add("D:\VBA funktioner\VBA\Opskrifter\Opskrift.doc")
Call InsertAtBookmark(WordDoc, "Opskrift", Me!Opskrift)
objword.Visible = True
DoCmd.Hourglass False

'Ny funktion
Function InsertAtBookmark(objWordDoc As Word.Document, strBookmark As String, strText As String) As Boolean
With objWordDoc.Bookmarks
If .Exists(strBookmark) Then
.Item(strBookmark).Range.Text = strText
InsertAtBookmark = True
End If
End With
End Function

Læg din e-mail hvis du ønsker et eksempel.
Avatar billede pbudk Nybegynder
17. juli 2002 - 21:00 #5
øh det kan godt være, at jeg ikke lige fik formuleret mig ordentligt.
Men
Avatar billede pbudk Nybegynder
17. juli 2002 - 21:02 #6
Ups - jeg fik lige trykket forkert :o(
prøver igen.

øh det kan godt være, at jeg ikke lige fik formuleret mig ordentligt.

Mit eneste ønske er, at lukke nogle allerede eksisterende filer op (både pdf, excel og word).
Avatar billede terry Ekspert
17. juli 2002 - 21:24 #7
Mugs, I dont quite agree with you! Just becuae your opening a word document OR an Excel Sheet doesnt mean that it is to be altered by Access!

pbudk the solution I gave opens any document type with the Program it is associated with! .XLS = Excel .DOC Word for example.

You should TRY it to see what happens!
Avatar billede terry Ekspert
17. juli 2002 - 21:28 #8
The line Public Declare Function ShellExecute .... is telling Access that it can find a Function named ShellExecute in a file which is VERY likely installed on your PC (shell32.dll)

This line actually calls the fnction and the stAppName should now contain "Jeg har et tekstfelt i min db, hvor stien til et ekstern dokument ligger." In other words the name of the dcoument you want to open.

result = ShellExecute(0, "Open", stAppName, "", "", SW_SHOW)
Avatar billede mugs Novice
17. juli 2002 - 21:31 #9
terry > I just don't see the reason to open a Word-document or excel-sheet without transfer data from access.
Avatar billede terry Ekspert
17. juli 2002 - 21:33 #10
So really ALL that you need to do is place button on your form and name it Command1

then alter this line so that SomeFieldOnTheForm gets changed to the name of the field which contains your path+file name of teh file you want to open.

Then you MUST make sure that the on click event for the button is set to EVENT PROCEDURE (Private Sub Command1_Click() gets performed)


stAppName = Me.SomeFieldOnTheForm
Avatar billede terry Ekspert
17. juli 2002 - 21:36 #11
mugs>Example: I recently made a program which registered ALL buildings owned by LEGO. They also wanted to archive ALL documents concerning each building, and it should be possible to open thes edocuments and edit them. Nothing to do with copying data from Access to the document. It could be  CAD CAM drawing of the building for example.
Avatar billede pbudk Nybegynder
17. juli 2002 - 21:44 #12
My fields are in "dataarkvisning" on a subform - does this have any impact on the code ?
Avatar billede pbudk Nybegynder
17. juli 2002 - 21:47 #13
I keep getting a "compile error: syntax error" I guess I am not correctly referring to the module.

Private Sub Command1_Click()
openFile()
End Sub
Avatar billede mugs Novice
17. juli 2002 - 21:48 #14
terry > In your example the best must bee to store the information in an Access-db. Search after the building in your db, and when you have founded it, then click on a command-button and access will find the correct document in word. LEGO must own a lot off buildings, so the task for store data off them is a task for Access.

the word-documents are stored with a building no, that matches the same field in access, and when you have found the building no in access, then you click on a command button, and acess will open the word-document with the same building no.
Avatar billede terry Ekspert
17. juli 2002 - 21:59 #15
I have a table which has a reference to th ebuilding and a path+filename to the document. There can be many documents so they get displayed in a list with a comment they can choose the document form the list and it gets opended for viewing.
They have around 300 I think (In DK alone)
Avatar billede terry Ekspert
17. juli 2002 - 22:02 #16
you MUST NOT use openFile()

Use the code as it IS
If your having problems the send it to terry@santhell.dk and I will take a look, either this evening or first thing tomorrow.

Please ZIP the DB or rename it to .txt or Outlook (XP) will block it!
Avatar billede pbudk Nybegynder
17. juli 2002 - 22:14 #17
AH - I didn't read your original instructions thoroughly.

Now it's working

Thank you very much
Avatar billede terry Ekspert
17. juli 2002 - 22:31 #18
OK, and thanks :o)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester