;============================================================ ; iron_office.hsp — Microsoft Office COM 自動化 (.NET COM Interop) ; ; .NET Framework の COM Interop (Type.GetTypeFromProgID + ; Activator.CreateInstance) を使って Excel / Word / PowerPoint の ; COM オートメーション (IDispatch late binding) を駆動する。 ; ; VBScript ブリッジ方式は Windows 11 24H2 で VBScript が既定無効化 ; されたため廃止し、hsp3net の mcall 経由に移行した。 ; ; hsp3net (GUI / CL) 専用。Office がインストールされていない環境 ; では全関数が stat != 0 を返す。 ; ; API: ; office_excel_get_cell "file.xlsx", sheet, row, col, var_str ; office_excel_set_cell "file.xlsx", sheet, row, col, "value" ; office_excel_to_pdf "file.xlsx", "out.pdf" ; office_word_to_pdf "file.docx", "out.pdf" ; office_ppt_to_pdf "file.pptx", "out.pdf" ; ; 依存: hsp3net ランタイム (mcall / newnet / delnet) ; 大量セル操作は iron_xlsx.hsp (OpenXML 直読み) または ; iron_excel.hsp (.NET COM 直接) を推奨。 ;============================================================ #ifndef __iron_office_hsp__ #define __iron_office_hsp__ #module iron_office ;------------------------------------------------------------ ; 内部: フルパスに変換 (COM は相対パスを受け付けない) ;------------------------------------------------------------ #defcfunc _off_fullpath str _rp, local _fp _fp = _rp ; 先頭が \ or X: ならフルパスとみなす if peek(_fp, 0) = '\\' : return _fp if strlen(_fp) >= 2 { if peek(_fp, 1) = ':' : return _fp } _fp = dir_cur + "\\" + _rp return _fp ;------------------------------------------------------------ ; 内部: C# ソースを動的コンパイル (初回のみ) ;------------------------------------------------------------ #deffunc _off_ensure_loaded local _cs if _off_loaded@iron_office : return _cs = {" using System; using System.Reflection; using System.Runtime.InteropServices; public static class OfficeHelper { // Excel public static string ExcelGetCell(string file, int sheet, int row, int col) { var t = Type.GetTypeFromProgID("Excel.Application"); if (t == null) return ""; dynamic app = Activator.CreateInstance(t); try { app.Visible = false; app.DisplayAlerts = false; dynamic wb = app.Workbooks.Open(file); dynamic v = wb.Sheets[sheet].Cells[row, col].Value; string r = v != null ? v.ToString() : ""; wb.Close(false); return r; } finally { app.Quit(); Marshal.ReleaseComObject(app); } } public static void ExcelSetCell(string file, int sheet, int row, int col, string val) { var t = Type.GetTypeFromProgID("Excel.Application"); if (t == null) return; dynamic app = Activator.CreateInstance(t); try { app.Visible = false; app.DisplayAlerts = false; dynamic wb = app.Workbooks.Open(file); wb.Sheets[sheet].Cells[row, col].Value = val; wb.Save(); wb.Close(false); } finally { app.Quit(); Marshal.ReleaseComObject(app); } } public static void ExcelToPdf(string src, string dst) { var t = Type.GetTypeFromProgID("Excel.Application"); if (t == null) return; dynamic app = Activator.CreateInstance(t); try { app.Visible = false; app.DisplayAlerts = false; dynamic wb = app.Workbooks.Open(src); wb.ExportAsFixedFormat(0, dst); // xlTypePDF = 0 wb.Close(false); } finally { app.Quit(); Marshal.ReleaseComObject(app); } } // Word public static void WordToPdf(string src, string dst) { var t = Type.GetTypeFromProgID("Word.Application"); if (t == null) return; dynamic app = Activator.CreateInstance(t); try { app.Visible = false; dynamic doc = app.Documents.Open(src); doc.ExportAsFixedFormat(dst, 17); // wdExportFormatPDF = 17 doc.Close(0); } finally { app.Quit(); Marshal.ReleaseComObject(app); } } // PowerPoint public static void PptToPdf(string src, string dst) { var t = Type.GetTypeFromProgID("PowerPoint.Application"); if (t == null) return; dynamic app = Activator.CreateInstance(t); try { dynamic pres = app.Presentations.Open(src, -1, -1, 0); pres.SaveAs(dst, 32); // ppSaveAsPDF = 32 pres.Close(); } finally { app.Quit(); Marshal.ReleaseComObject(app); } } } "} loadnet _cs, 3, "Microsoft.CSharp.dll" _off_loaded@iron_office = 1 return ;============================================================ ; public: Excel セル読み取り ;============================================================ #deffunc office_excel_get_cell str file, int sheet, int row, int col, var out_str, \ local _fp, local _dummy _off_ensure_loaded _fp = _off_fullpath(file) newnet _dummy, "OfficeHelper" mcall _dummy, "ExcelGetCell", out_str, _fp, sheet, row, col return stat ;============================================================ ; public: Excel セル書き込み ;============================================================ #deffunc office_excel_set_cell str file, int sheet, int row, int col, str val, \ local _fp, local _dummy _off_ensure_loaded _fp = _off_fullpath(file) newnet _dummy, "OfficeHelper" mcall _dummy, "ExcelSetCell", _fp, sheet, row, col, val return stat ;============================================================ ; public: Excel → PDF ;============================================================ #deffunc office_excel_to_pdf str src, str dst, \ local _s, local _d, local _dummy _off_ensure_loaded _s = _off_fullpath(src) : _d = _off_fullpath(dst) newnet _dummy, "OfficeHelper" mcall _dummy, "ExcelToPdf", _s, _d return stat ;============================================================ ; public: Word → PDF ;============================================================ #deffunc office_word_to_pdf str src, str dst, \ local _s, local _d, local _dummy _off_ensure_loaded _s = _off_fullpath(src) : _d = _off_fullpath(dst) newnet _dummy, "OfficeHelper" mcall _dummy, "WordToPdf", _s, _d return stat ;============================================================ ; public: PowerPoint → PDF ;============================================================ #deffunc office_ppt_to_pdf str src, str dst, \ local _s, local _d, local _dummy _off_ensure_loaded _s = _off_fullpath(src) : _d = _off_fullpath(dst) newnet _dummy, "OfficeHelper" mcall _dummy, "PptToPdf", _s, _d return stat #global #endif