2017-05-18 14 views
0

私はワード文書とデータを格納するデータベースを構築しています。この表には、文書という単語のコピーが添付されています。アクセステーブルから添付ファイルを開く方法

VBAを使用してField1(デフォルト名)から添付ファイル(ワードドキュメント)のコピーを参照して開くにはどうすればよいですか?

私はDAOレコードセットを使用しようとしましたが、バグが続いています。アタッチメントのオブジェクト参照ドキュメントはほとんど役に立たなかった。

追加情報が必要な場合は教えてください。ありがとうございました!

Private Sub Field1_AfterUpdate() 
'Outline: 
'Macro is on Form_Entry. After user uploads an attachment into the DB via form, 
'I would like to open a copy of the attachment 
'and perform additional actions on the document 

'Declarations 
    Dim Active_DB As DAO.Database 
    Dim Active_Q As DAO.QueryDef 
    Dim Active_RS, iRS As DAO.Recordset 
    Dim Active_Field As DAO.Field 

'Refresh form such new entry is created 
    Forms!Entry.Refresh 

'Connect DB 
    Set Active_DB = CurrentDb 
    Set Active_RS = Forms!Entry.Recordset 

'Retrieve recordset of the current record entry. 
    Active_RS.FindFirst "ID =" & Forms!Entry.ID.Value 

'This is where I run into problems. I am not sure what the command is to open a document. 
    Set Active_Field = Active_RS!Field1.Open 

    Debug.Print Active_Field.FileName 

End Sub 
+2

コードのない質問は閉鎖される傾向があります。コードにエラーがある場合でも、それを含めるのが最善です。 –

+0

@TimWilliams。メモをありがとうございます。私は私の投稿を編集しました:) – DVCode

+1

レコードセットから直接ドキュメントを開くことはできません。ファイルに保存し、そこから開く –

答えて

1

皆様のご協力ありがとうございます。

コメントに基づいて、処理のために一時ファイルを保存して開くことで、この問題を回避することができました。

Private Sub Field1_AfterUpdate() 
'Declarations 
    'Access-Related 
    Dim Active_DB As DAO.Database 
    Dim Active_Q As DAO.QueryDef 
    Dim Active_RST, parent_RST, child_RST As DAO.Recordset 
    Dim Active_Field As DAO.Field 

    'Word-Related 
    Dim app_Word As Word.Application 
    Dim Active_DOC As Word.Document 

    'Varible Declaration 
    Dim str_Dir, str_FName As String 


    'Refresh to create record 
    Forms!entry.Refresh 


    'Initial Assignments 
    str_Dir = Environ("Temp") & "\" 

    Set Active_DB = CurrentDb 
    Set Active_RST = Forms!entry.Recordset 


    'Assign Record Set to Current Record 
    Active_RST.FindFirst "ID =" & Forms!entry.ID.Value 

    'Assign Field value 
    Set parent_RST = Active_RST.Fields("Field1").Value 
    Set Active_Field = parent_RST.FileData 

    'Create Word Application & document Objects 
    Set app_Word = CreateObject("Word.application") 

    If Dir(str_Dir & parent_RST.FileName) = "" Then 
     'If directory does not exist, create SaveToFiles 
     'Else open Document 
     Active_Field.SaveToFile str_Dir & parent_RST.FileName 
    End If 
    Set Active_DOC = Documents.Open(str_Dir & parent_RST.FileName) 


    Contract_Text.Value = str_Dir & parent_RST.FileName 

End Sub 
関連する問題