Search code examples
freepascallazarusole

I have a problem with porting VBA code to Lazarus


Sub ListaDokumentow()
On Error GoTo ErrHandler
    
    Dim oSubGT As InsERT.Subiekt
    Dim oListaDok As InsERT.SuDokumentyLista
    Dim oDok As InsERT.SuDokument
    Dim sNapis As String
        
    Set oSubGT = UruchomSubiekta()
     
 
    Set oListaDok = oSubGT.Dokumenty.Wybierz()
    oListaDok.FiltrTypOpcje = gtaFiltrSuDokumentOpcjeZam
    oListaDok.FiltrTyp = gtaFiltrSuDokumentZam
    oListaDok.FiltrOkres = gtaFiltrOkresNieokreslony
    oListaDok.MultiSelekcja = True
    
    oListaDok.Wyswietl
    
    sNapis = "Zaznaczono nastęujące dokumenty: " & vbCrLf
    
    For Each oDok In oListaDok.ZaznaczoneDokumenty
        sNapis = sNapis & oDok.NumerPelny & "ID:" & oDok.Identyfikator & vbCrLf
        
    Next
    
    MsgBox sNapis
    
    
    Exit Sub
ErrHandler:
    MsgBox "Wystąpił błąd: " & Err.Number & " - " & Err.Description
    
End Sub

I wrote such a code in VBA but I can not transfer it to Lazarus, it is about returning the invoice id and the invoice number

unit sm_testy;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, wps_u, LCLType,Comobj,variants,ActiveX;

type
  TOleCollectionEnumClass = class(TObject);
  TEkSmTesty = class(TForm)
  private

  public

  end;

var
  EkSmTesty: TEkSmTesty;

implementation

{$R *.lfm}


var
aSubGT,oSubGT:OleVariant;
oListGT,gtaFiltrOkresNieokreslony,gtaFiltrSuDokumentOpcjeZam,gtaFiltrSuDokumentZam:OleVariant;
oDok: OleVariant;
Reply, BoxStyle: Integer;

begin

BoxStyle := MB_ICONQUESTION + MB_YESNO;
//Reply := Application.MessageBox(PChar(IntToStr(PROG_VER_NUM_RC)),'Test', BoxStyle);

wpsConnect(oSubGT,true);

oListGT:=oSubGT.Dokumenty.Wybierz;
oListGT.FiltrTypOpcje:=15;
//oListGT.FiltrTyp:=gtaFiltrSuDokumentZam;
oListGT.FiltrOkres:=gtaFiltrOkresNieokreslony;
oListGT.MultiSelekcja:= True;
oListGT.Wyswietl;

end. // eof
                          

I stopped at the display of window of "subiekt" with the list, and I do not know how to enumerate the oleVariant object

Maybe someone would at least lead me on the right path

I can't enumerate the oDok object in Lazarus, which in my opinion should look something like this

oDok: = CreateOleObject ('InsERT.SuDokument');


Solution

  • I use this code in a separate utility unit:

    Type
      TEatenType = {$ifdef fpc} {$ifdef ver3_0}pulong{$else}Ulong{$endif}{$else}Integer{$endif}; // type for eaten parameter MkParseDisplayName
      oEnumIterator = record
                          mainobj: OleVariant;
                          oEnum  : IEnumVariant;
                          IterItem : OleVariant;
                          IterVal  : LongWord;
                          function Enumerate(v:olevariant):oEnumIterator;
                          function GetEnumerator :oEnumIterator;
                          function MoveNext:Boolean;
                          property Current:OleVariant read iteritem;
                       end;
    
    
    Implementation
    
    { oEnumIterator}
    
    function oEnumIterator.getenumerator :oEnumIterator;
    begin
     result:=self;
    end;
    
    Function oEnumIterator.Enumerate(v :olevariant):oEnumIterator;
    begin
      mainobj:=v;
      oEnum  := IUnknown(mainobj._NewEnum) as IEnumVariant;
      result:=self;
    end;
    
    Function  oEnumIterator.MoveNext:boolean;
    begin
      result:=(oEnum.Next(1, iteritem, iterval) = s_ok);
    end;
    

    and then enumerate using

    var  oEnum            : oEnumIterator;
         colItem,
         colItems : Olevariant
     ..
     colitems:=objWMIService.ExecQuery('Select * from Win32_OperatingSystem');
     for colItem in oEnum.Enumerate(colItems) do  
             memo1.lines.add('Version: ',colitem.Version);