Insert VB script to exported xls

98 views Asked by At

My application exports an .xls with some data.

How can i manage to put some VB script to an exported xls from Delphi.

Explanation:

I'm using CreateOleObject('Excel.Application') for creating it.

That .xls will be changed by the user and imported in application again.

I want that any row that gets changed by the user, be marked as so.

I was able to achieve that in Excel with the script below, but I'm not able to insert that script at the document creation.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row > 1 Then Cells(Target.Row, "I") = Now()
End Sub

Any suggestion to solve that problem in a different way will be welcome.

1

There are 1 answers

0
JGBMattos On

I was able to solve that question with the code bellow:

ExcApp := CreateOleObject('Excel.Application');
ExcApp.Visible := False;
ExcApp.WorkBooks.Add;
xVBComponente := ExcApp.WorkBooks[1].VBProject.VBComponents.Item('Planilha1');
xVBComponente.CodeModule.AddFromString(xMacro);
ExcApp.WorkBooks[1].SaveAs(pFileName, 52);

The 52 in "...SaveAs.." is the constant "xlOpenXMLWorkbookMacroEnabled".

Besides the code i needed it to change some excel configs. link for details: Excel Config