AccessVBAメモ

AccessVBAで作ったサンプルコード集

AccessVBA 指定したWord Excel ppt ファイルを開いて印刷する

スポンサーリンク

 

予め印刷したいファイルをフルパスをテーブル保存しておきます。(テーブル名/フルパス)

印刷実行時にそのテーブルを読み込み、指定したフルパスを開いて印刷実行します。

印刷実行したら、そのファイルを閉じる一連の動作です。

 

'-----------------------------------------------------------------------------------------------------------

Private Sub selectListName()

 

Dim db As Database

Dim rst As Recordset

 

Dim strSQL As String

Dim filename As String

Dim filetype As String

 

 

Set db = CurrentDb

 

strSQL = "SELECT テーブル名.* FROM テーブル名;"

 

Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)

 

 

If rst.RecordCount <> 0 Then

 

 

    If IsNull(rst!フルパス) Then

   

    MsgBox "印刷ファイルが、指定されていません。"

   

    Exit Sub

   

    Else

   

    filename = rst!フルパス

   

        If rst!フルパス Like "*.doc*" Then

   

        filetype = "doc"

   

        Else

            If rst!フルパス Like "*.xls*" Then

           

            filetype = "xls"

           

            Else

           

            filetype = "ppt"

           

            End If

        End If

   

   

’予め登録してあるフルパスからファイル形式を推定し、印刷実行する

'printManual プロシージャにファイルタイプとフルパスを渡します

 

    Call printManual(filetype, filename)

   

   

    End If

   

End If

 

End If

 

db.Close

Set db = Nothing

 

End Sub

 

'印刷実行のプロシージャ

 

Private Sub printManual(filetype As String, filename As String)

 

Dim fileobject As Object

Dim xlsbook As Object

Dim pptfile As Object

 

Select Case filetype

 

Case Is = "doc"

 

Set fileobject = CreateObject("Word.Application")

 

fileobject.Documents.Open (filename)

 

fileobject.ActiveDocument.PrintOut

fileobject.ActiveDocument.Close

 

fileobject.Quit

 

Set fileobject = Nothing

 

 

Case Is = "xls"

 

Set fileobject = CreateObject("Excel.Application")

 

Set xlsbook = fileobject.Workbooks.Open(filename)

 

xlsbook.PrintOut

 

xlsbook.Close

fileobject.Quit

 

Set xlsbook = Nothing

 

Set fileobject = Nothing

 

Case Is = "ppt"

 

Set fileobject = CreateObject("Powerpoint.Application")

 

Set pptfile = fileobject.Presentations.Open(filename)

 

pptfile.PrintOut

pptfile.Close

fileobject.Quit

 

Set pptfile = Nothing

Set fileobject = Nothing

 

End Select

 

 

 

End Sub