ONLY DO WHAT ONLY YOU CAN DO

こけたら立ちなはれ 立ったら歩きなはれ

さまざまな言語で Access オートメーション

VBScript

Option Explicit
 
Const acOutputReport = 3
Const acViewPreview  = 2
Const acFormatXLS    = "Microsoft Excel (*.xls)"
Const acFormatRTF    = "Rich Text Format (*.rtf)"
Const acFormatSNP    = "Snapshot Format (*.snp)"
Const acFormatHTML   = "HTML (*.html)"
 
Dim acApp: Set acApp = CreateObject("Access.Application")
acApp.OpenCurrentDatabase WScript.Arguments(0)
acApp.Visible = True
 
Dim strReportName: strReportName = WScript.Arguments(1)
With acApp
    With .DoCmd
        Select Case UCase(WScript.Arguments(3))
        Case "XLS"
            .OutputTo acOutputReport, strReportName, acFormatXLS,  WScript.Arguments(2) & "autoxls.xls",  True
        Case "RTF"
            .OutputTo acOutputReport, strReportName, acFormatRTF,  WScript.Arguments(2) & "autortf.rtf",  True
        Case "SNAP"
            .OutputTo acOutputReport, strReportName, acFormatSNP,  WScript.Arguments(2) & "autosnap.snp", True
        Case "HTML"
            .OutputTo acOutputReport, strReportName, acFormatHTML, WScript.Arguments(2) & "autohtml.htm", True
        Case Else
            .OpenReport strReportName, acViewPreview
        End Select
    End With
End With
 
Select Case UCase(WScript.Arguments(3))
Case "XLS", "RTF", "SNAP", "HTML"
    acApp.Quit
End Select
Set acApp = Nothing
 
'実行形式
'cscript //nologo OpenReport.vbs "s:\test.mdb" "test_report" "s:\" "XLS"
'cscript //nologo OpenReport.vbs "s:\test.mdb" "test_report" "s:\" "RTF"
'cscript //nologo OpenReport.vbs "s:\test.mdb" "test_report" "s:\" "SNAP"
'cscript //nologo OpenReport.vbs "s:\test.mdb" "test_report" "s:\" "HTML"
'cscript //nologo OpenReport.vbs "s:\test.mdb" "test_report" ""    ""

Perl

use Win32::OLE;
 
use constant AcOutputReport => 3;
use constant AcViewPreview  => 2;
use constant AcFormatXLS    => "Microsoft Excel (*.xls)";
use constant AcFormatRTF    => "Rich Text Format (*.rtf)";
use constant AcFormatSNP    => "Snapshot Format (*.snp)";
use constant AcFormatHTML   => "HTML (*.html)";
 
Win32::OLE::CreateObject("Access.Application", $acApp);
$acApp->OpenCurrentDatabase($ARGV[0]);
$acApp->{Visible}       = 1;
 
$strReportName = $ARGV[1];
 
if (uc($ARGV[3]) eq "XLS")
{
    $acApp->DoCmd->OutputTo(AcOutputReport, $strReportName, AcFormatXLS,  $ARGV[2] . "autoxls.xls",  1);
}
elsif (uc($ARGV[3]) eq "RTF")
{
    $acApp->DoCmd->OutputTo(AcOutputReport, $strReportName, AcFormatRTF,  $ARGV[2] . "autortf.rtf",  1);
}
elsif (uc($ARGV[3]) eq "SNAP")
{
    $acApp->DoCmd->OutputTo(AcOutputReport, $strReportName, AcFormatSNP,  $ARGV[2] . "autosnap.snp", 1);
}
elsif (uc($ARGV[3]) eq "HTML")
{
    $acApp->DoCmd->OutputTo(AcOutputReport, $strReportName, AcFormatHTML, $ARGV[2] . "autohtml.htm", 1);
}
else
{
    $acApp->DoCmd->OpenReport($strReportName, AcViewPreview);
}
 
if (uc($ARGV[3]) eq "XLS" || uc($ARGV[3]) eq "RTF" || uc($ARGV[3]) eq "SNAP" || uc($ARGV[3]) eq "HTML")
{
    $acApp->Quit();
}
 
#実行形式
#perl OpenReport.pl "s:\test.mdb" "test_report" "s:\" "XLS"
#perl OpenReport.pl "s:\test.mdb" "test_report" "s:\" "RTF"
#perl OpenReport.pl "s:\test.mdb" "test_report" "s:\" "SNAP"
#perl OpenReport.pl "s:\test.mdb" "test_report" "s:\" "HTML"
#perl OpenReport.pl "s:\test.mdb" "test_report" ""    ""

PHP

<?php
define("AcOutputReport",  3);
define("AcViewPreview",   2);
define("AcFormatXLS",    "Microsoft Excel (*.xls)");
define("AcFormatRTF",    "Rich Text Format (*.rtf)");
define("AcFormatSNP",    "Snapshot Format (*.snp)");
define("AcFormatHTML",   "HTML (*.html)");
 
$acApp = new COM("Access.Application");
$acApp->OpenCurrentDatabase($argv[1]);
$acApp->Visible = true;
 
$strReportName = $argv[2];
 
if (strtoupper($argv[4]) == "XLS")
{
    $acApp->DoCmd->OutputTo(AcOutputReport, $strReportName, AcFormatXLS,  $argv[3] . "autoxls.xls",  1);
}
elseif (strtoupper($argv[4]) == "RTF")
{
    $acApp->DoCmd->OutputTo(AcOutputReport, $strReportName, AcFormatRTF,  $argv[3] . "autortf.rtf",  1);
}
elseif (strtoupper($argv[4]) == "SNAP")
{
    $acApp->DoCmd->OutputTo(AcOutputReport, $strReportName, AcFormatSNP,  $argv[3] . "autosnap.snp", 1);
}
elseif (strtoupper($argv[4]) == "HTML")
{
    $acApp->DoCmd->OutputTo(AcOutputReport, $strReportName, AcFormatHTML, $argv[3] . "autohtml.htm", 1);
}
else
{
    $acApp->DoCmd->OpenReport($strReportName, AcViewPreview);
}
 
if (strtoupper($argv[4]) == "XLS" || strtoupper($argv[4]) == "RTF" || strtoupper($argv[4]) == "SNAP" || strtoupper($argv[4]) == "HTML")
{
    $acApp->Quit();
}
 
#実行形式
#php OpenReport.php "s:\test.mdb" "test_report" "s:\" "XLS"
#php OpenReport.php "s:\test.mdb" "test_report" "s:\" "RTF"
#php OpenReport.php "s:\test.mdb" "test_report" "s:\" "SNAP"
#php OpenReport.php "s:\test.mdb" "test_report" "s:\" "HTML"
#php OpenReport.php "s:\test.mdb" "test_report" ""    ""

Python

# coding: Shift_JIS
import win32api, win32con, win32com, win32com.client, os, time, sys
 
acOutputReport = 3
acViewPreview  = 2
acFormatXLS    = "Microsoft Excel (*.xls)"
acFormatRTF    = "Rich Text Format (*.rtf)"
acFormatSNP    = "Snapshot Format (*.snp)"
acFormatHTML   = "HTML (*.html)"
 
acApp              = win32com.client.Dispatch("Access.Application")
acApp.OpenCurrentDatabase(sys.argv[1])
acApp.Visible      = True
 
strReportName = sys.argv[2]
 
if (sys.argv[4].upper() == "XLS"):
    acApp.DoCmd.OutputTo(acOutputReport, strReportName, acFormatXLS,  sys.argv[3] + "autoxls.xls",  true)
elif (sys.argv[4].upper() == "RTF"):
    acApp.DoCmd.OutputTo(acOutputReport, strReportName, acFormatRTF,  sys.argv[3] + "autortf.rtf",  true)
elif (sys.argv[4].upper() == "SNAP"):
    acApp.DoCmd.OutputTo(acOutputReport, strReportName, acFormatSNP,  sys.argv[3] + "autosnap.snp", true)
elif (sys.argv[4].upper() == "HTML"):
    acApp.DoCmd.OutputTo(acOutputReport, strReportName, acFormatHTML, sys.argv[3] + "autohtml.htm", true)
else:
    acApp.DoCmd.OpenReport(strReportName, acViewPreview)
 
if (sys.argv[4].upper() == "XLS") or (sys.argv[4].upper() == "RTF") or (sys.argv[4].upper() == "SNAP") or (sys.argv[4].upper() == "HTML"):
    acApp.Quit
 
#実行形式
#puthon OpenReport.py "s:\test.mdb" "test_report" "s:\" "XLS"
#puthon OpenReport.py "s:\test.mdb" "test_report" "s:\" "RTF"
#puthon OpenReport.py "s:\test.mdb" "test_report" "s:\" "SNAP"
#puthon OpenReport.py "s:\test.mdb" "test_report" "s:\" "HTML"
#puthon OpenReport.py "s:\test.mdb" "test_report" ""    ""

Ruby

require 'win32ole'
 
AcOutputReport = 3
AcViewPreview  = 2
AcFormatXLS    = "Microsoft Excel (*.xls)"
AcFormatRTF    = "Rich Text Format (*.rtf)"
AcFormatSNP    = "Snapshot Format (*.snp)"
AcFormatHTML   = "HTML (*.html)"
 
acApp = WIN32OLE.new('Access.Application')
acApp.OpenCurrentDatabase(ARGV[0])
acApp.Visible = true
 
strReportName = ARGV[1]
 
case ARGV[3].upcase
when "XLS"
    acApp.DoCmd.OutputTo(AcOutputReport, strReportName, AcFormatXLS,  ARGV[2] + "autoxls.xls",  true)
when "RTF"
    acApp.DoCmd.OutputTo(AcOutputReport, strReportName, AcFormatRTF,  ARGV[2] + "autortf.rtf",  true)
when "SNAP"
    acApp.DoCmd.OutputTo(AcOutputReport, strReportName, AcFormatSNP,  ARGV[2] + "autosnap.snp", true)
when "HTML"
    acApp.DoCmd.OutputTo(AcOutputReport, strReportName, AcFormatHTML, ARGV[2] + "autohtml.htm", true)
else
    acApp.DoCmd.OpenReport(strReportName, AcViewPreview)
end
 
case ARGV[3].upcase
when "XLS", "RTF", "SNAP", "HTML"
    acApp.Quit
end
 
#実行形式
#ruby OpenReport.rb "s:\test.mdb" "test_report" "s:\" "XLS"
#ruby OpenReport.rb "s:\test.mdb" "test_report" "s:\" "RTF"
#ruby OpenReport.rb "s:\test.mdb" "test_report" "s:\" "SNAP"
#ruby OpenReport.rb "s:\test.mdb" "test_report" "s:\" "HTML"
#ruby OpenReport.rb "s:\test.mdb" "test_report" ""    ""