How to write VBA to an XLSX file?

Questions and answers on how to use XLSReadWriteII 5.
darnocian
Posts: 10
Joined: Thu Oct 03, 2019 10:11 am

How to write VBA to an XLSX file?

Post by darnocian »

Hi,

I'm having issues writing VBA to a new file , or read writing VBA in an existing file. I'm not sure if I'm doing something wrong or some sequence issue regarding settings.

Here is a sample application with 3 methods:
TestReadVBA - works fine
TestReadWriteVBA - fails
TestWriteVBA - fails

Code: Select all

program sample;

{$APPTYPE CONSOLE}
{$R *.res}

uses
    XLSReadWriteII5,
    BIFF_VBA5,
    System.SysUtils;

procedure TestReadVBA(const Filename: string);
var
    XLS: TXLSReadWriteII5;
    ICount, IMacroLines: Integer;
begin
    XLS := TXLSReadWriteII5.Create(nil);
    try
        Xls.ReadVBA := True;
        XLS.LoadFromFile(Filename);
        for ICount := 0 to (XLS.VBA.Count - 1) do
        begin
            Writeln('module: ' + XLS.VBA.Modules[ICount].Name);
            for IMacroLines := 0 to (XLS.VBA.Modules[ICount].Source.Count - 1) do
            begin
                Writeln(XLS.VBA.Modules[ICount].Source.Strings[IMacroLines]);
            end;
        end;
    finally
        Xls.Free;
    end;
end;

procedure TestReadWriteVBA(const Filename: string; const Newfilename: string);
var
    XLS, XLS2: TXLSReadWriteII5;
begin
    XLS := TXLSReadWriteII5.Create(nil);
    try
        XLS.ReadVBA := True;
        XLS.LoadFromFile(Filename);

        Xls.VBA.EditVBA := True;

        with Xls.VBA.AddModule('mymodule2', VmtDocument).Source do
        begin
            Add('Public Function add(a As Integer, b As Integer) As Integer');
            Add('add = a + b');
            Add('End Function');
        end;

        Xls.SaveToFile(Newfilename);

    finally
        Xls.Free;
    end;
end;

procedure TestWriteVBA(const Filename: string);
var
    XLS: TXLSReadWriteII5;
begin
    XLS := TXLSReadWriteII5.Create(nil);
    try
        Xls.VBA.EditVBA := True; // blows up because XlS.VBA is not initialised

        with Xls.VBA.AddModule('mymodule2', VmtDocument).Source do
        begin
            Add('Public Function add(a As Integer, b As Integer) As Integer');
            Add('add = a + b');
            Add('End Function');
        end;

        XLS.SaveToFile(filename);
    finally
        Xls.Free;
    end;
end;

begin
    try
        // works fine
        TestReadVBA('sample.xls');

        // problem appending
        TestReadWriteVBA('sample.xls', 'sample_with_update.xls');

        // problem appending vba to a new file
        TestWriteVBA('new_sample.xls');
    except
        on E: Exception do
            Writeln(E.Message);
    end;

end.
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: How to write VBA to an XLSX file?

Post by larsa »

Hello

There is a new update now fixing this. Please download it and test it.
Lars Arvidsson, Axolot Data
darnocian
Posts: 10
Joined: Thu Oct 03, 2019 10:11 am

Re: How to write VBA to an XLSX file?

Post by darnocian »

Hi,

I redownloaded xlsSpreadSheet_ddx103_reg.exe but I don't see any change.

Regards
Conrad
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: How to write VBA to an XLSX file?

Post by larsa »

Hello

Here is an example on how to create a VBA:

Code: Select all

  XLS.Version := xvExcel97;

  XLS.ReadVBA := True;
  XLS.BIFF.ReadMacros := True;
  XLS.VBA.EditVBA := True;

  with XLS.VBA.AddModule('mymodule2', VmtDocument).Source do begin
    Add('Public Function __SUGGA__(a As Integer, b As Integer) As Integer');
    Add('add = a + b');
    Add('End Function');
  end;

  XLS.Filename := 'test.xls';
  XLS.Write;
Lars Arvidsson, Axolot Data
darnocian
Posts: 10
Joined: Thu Oct 03, 2019 10:11 am

Re: How to write VBA to an XLSX file?

Post by darnocian »

Do I need an update - could you please refer to what I must download? Nothing changed from the reinstallation I mentioned above (and have downloaded twice)

The example above does not work for me.
darnocian
Posts: 10
Joined: Thu Oct 03, 2019 10:11 am

Re: How to write VBA to an XLSX file?

Post by darnocian »

The example you sent doesn't work for me.

with the library, I can read the VBA code that was written by the library, but it does not seem to be available in Excel when I open it that way.

I still don't see any change to the installer - I did a binary difference. Is the update you mentioned somewhere else?

Regards,
Conrad
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: How to write VBA to an XLSX file?

Post by larsa »

Hello

I sent you a mail this Friday and today asking you to send me the XLS file you created.
Please do so. If the code you used to create it not is identical to the
sample I provided, please also include your code.
Lars Arvidsson, Axolot Data
darnocian
Posts: 10
Joined: Thu Oct 03, 2019 10:11 am

Re: How to write VBA to an XLSX file?

Post by darnocian »

Hi Lars,
I don't seem to be getting your mails. I've checked spam folder as well.
Regards,
Conrad
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: How to write VBA to an XLSX file?

Post by larsa »

Hello

I sent you this by mail as well.

I found the error. The module type shall be VmtDocument, not VmtMacro.

Correct:
XLS.VBA.AddModule('mymodule2', VmtDocument).Source
Lars Arvidsson, Axolot Data
darnocian
Posts: 10
Joined: Thu Oct 03, 2019 10:11 am

Re: How to write VBA to an XLSX file?

Post by darnocian »

Hi Lars,

I tried that. I emailed you the example and output.

Unfortunately no luck for me.

Regards,
Conrad
darnocian
Posts: 10
Joined: Thu Oct 03, 2019 10:11 am

Re: How to write VBA to an XLSX file?

Post by darnocian »

Just confirming - I've tried switching between VmtMacro and VmtDocument.

I've also tried writing for xvExcel2007 which fails as writing xlsm is also required.

BTW: I'm not sure I'm getting your emails.
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: How to write VBA to an XLSX file?

Post by larsa »

Hello

I'm receiving your mails.

Please send me an excel file you created with your TestWriteVBA procedure but with ModuleType set to VmtDocument,
Lars Arvidsson, Axolot Data
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: How to write VBA to an XLSX file?

Post by larsa »

Hello

Your sample works fine here. I have uploaded a new version of the component. Please download it and make sure the version number is 6.00.59
Lars Arvidsson, Axolot Data
darnocian
Posts: 10
Joined: Thu Oct 03, 2019 10:11 am

Re: How to write VBA to an XLSX file?

Post by darnocian »

Thanks Lars,

xls
I think this update helped considerably.

Image

I got it to do what I needed. with vmtDocument, the code is added in the 'Microsoft Excel Objects' section. with vmtMacro, the code is added in the 'Modules' section as I required.

xlsm
Also, can you advise how to read/add to xlsm?

I tried changing the version to 2007 and the filename, but it did not work.

Regards,
Conrad
darnocian
Posts: 10
Joined: Thu Oct 03, 2019 10:11 am

Re: How to write VBA to an XLSX file?

Post by darnocian »

Hi Lars,

So it doesn't seem to work on XLSM? I get an exception reading and writing.

Any tips on settings to fix this as this is quite key for me as well as I have a mix of xls and xlsm to deal with.

regards,
Conrad
Post Reply