LuaCOMによるMicrosoft WordおよびMicrosoft Excelの制御

Luaスクリプトを使用して、Microsoft WordもMicrosoft Excelも開くことなく、指定した内容でそれらの文書を生成します。

Windowsオペレーティングシステムを使用していること、およびMicrosoft WordまたはMicrosoft Excel、あるいはその両方がインストールされていることを確認します。
  1. スクリプトエディターを開きます。
  2. 新しい空のスクリプトを作成します。
  3. 例として、次のスクリプトのいずれかをスクリプトエディターに読み込みます。
  4. そのスクリプトを実行します。
    -- MS WORD
    require "luacom"
    
    -- Open Word
    local msword = luacom.CreateObject("Word.Application")
    assert(msword, "Could not open MS Word")
    
    -- Initialise the document
    msword.Visible = true
    doc = msword.Documents:Add()
    
    -- Add content
    insertionPoint = doc.ActiveWindow.Selection
    insertionPoint.Style = "Heading 1"
    insertionPoint:TypeText( "Feko Says..." )
    insertionPoint:TypeParagraph()
    insertionPoint:TypeText( "Hello world!" )
    -- MS EXCEL
    require "luacom"
    
    -- Open Excel
    local excel = luacom.CreateObject("Excel.Application")
    assert(excel, "Could not open MS Excel")
    
    -- Initialise the worksheet
    excel.Visible = true
    workbook = excel.Workbooks:Add()
    worksheet = workbook.Worksheets:Add()
    
    -- Populate the data and display the contents of cell A3
    worksheet.Range( "A1", "A1" ).Value2 = [[hello]]
    worksheet.Range( "A2", "A2" ).Value2 = [[world]]
    worksheet.Range( "A3", "A3" ).Value2 = [[=CONCAT(A1," ",A2,"!!")]]
    feko.Form.Info( "Excel says...", worksheet.Range( "A3", "A3" ).Value2 )
    
    -- Change an input value and display A3 once again
    worksheet.Range( "A2", "A2" ).Value2 = [[everybody]]
    feko.Form.Info( "Excel says...", worksheet.Range( "A3", "A3" ).Value2 )