45.4 Examples

The following example launches Microsoft Excel, adds a new worksheet, fill in some fields and finally clears the worksheet and quits Excel

     :- use_module(library(comclient)).
     :- use_module(library(lists)).
     
     test :-
        test('Excel.Application').
     
     test(ProgID) :-
        comclient_create_instance(ProgID, App),
        %% Visuall Basic: app.visible = 1
        comclient_invoke_put(App, visible, 1),
        %% VB: app.workbooks.add
        comclient_invoke_method_proc(App, [workbooks, add]),
        %% VB: with app.activesheet
        comclient_invoke_method_fun(App, activesheet, ActiveSheet),
     
        Rows = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],
        Cols = Rows,
        %% VB: .cells i,j . value = i+j/100
        (
          member(I, Rows),
          member(J, Cols),
          ValIJ is I+J/100,
     
          comclient_invoke_put(ActiveSheet, [cells(I,J),value], ValIJ),
          fail
        ; true
        ),
        (
          member(I, Rows),
          member(J, Cols),
          %% retrieve cell values
          comclient_invoke_method_fun(ActiveSheet, [cells(I,J), value],CellValue),
          format(user_error, '~nCell(~w,~w) = ~w', [I,J,CellValue]),
          fail
        ; true
        ),
     
        Range = 'A1:O15',
        format(user_error, '~Npress return to clear range (~w)', [Range]),
        flush_output(user_error),
        get0(_),
     
        %% VB: .range A1:O15 .Clear
        comclient_invoke_method_proc(ActiveSheet, [range(Range),clear]),
     
        %% Avoid Excel query "do you want to save..."
        %%  VB: app.activeworkbook.saved = 1
        comclient_invoke_put(App, [activeworkbook,saved], 1),
     
        format(user_error, '~Npress return to quit \'~w\'', [ProgID]),
        flush_output(user_error),
        get0(_),
     
        %% VB: app.quit
        comclient_invoke_method_proc(App, quit),
     
        comclient_release(ActiveSheet),
        comclient_release(App).