简体   繁体   中英

Insert VB script to exported xls

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM