Exportar dados em CSV

Olá, estou precisando exportar para csv alguns dados do meu projeto, os dados são de um driver, a cada x segundos eu tinha que criar um novo csv que substitui o antigo com a ultima informação passada pelo driver, é algo bem “seco” são 40 entradas de informação que tem que ir em um csv sem titulo nem colunas com nomes, apenas as informações. Utilizei este código abaixo como base (estou utilizando ele no projeto) mas não consegui fazer ele mais simples e nem trocar a tabela pelas variáveis do driver

Nome = Export_Button.Fonte.E3Query.table

Opcao = Application.ShowFilePicker(false, Nome, “csv”, 1, “Planilhas Excel (.csv)|.csv”)

if Opcao = true then

if Nome <> “” then

Set cs = Export_Button.Fonte.E3Query.GetADORecordset()

tabela = cs.GetString(2, ,";", vbNewLine)
'As linhas abaixo montam o cabeçalho
cabecalho=""
n=cs.Fields.Count
for i=1 to n
cabecalho = cabecalho& cs.Fields(i-1).Name & “;”
next

Set aux2a = CreateObject(“Scripting.FileSystemObject”)
Set aux1a = aux2a.CreateTextFile(Nome, True)
aux1a.Close

Set aux = CreateObject(“Scripting.FileSystemObject”)
Set aux1 = aux.OpenTextFile(Nome,8)
aux1.WriteLine "Tabela: "& Export_Button.Fonte.E3Query.table
aux1.WriteLine “”
aux1.WriteLine cabecalho
aux1.WriteLine tabela
aux1.Close

resp = MsgBox (“Deseja abrir o arquivo?”, vbYesNo + vbQuestion, “Abrir”)

if resp = 6 then 'YES
Application.ExecuteExternalApp Nome, “”, “”, 1

end if

end if

end if

Sub ExportToCsv_OnPreset()
    Const ForWriting = 2
    Dim dIOTags
    Dim dProperties
    Dim oFile
    Dim oFso
    Dim oTag
    Dim sFileName
    Dim sProperty

    On Error Resume Next
    
    sFileName = ".\MyFile.csv"
    
    ListIOTags dIOTags
    
    ListProperties dProperties
    
    Set oFso = CreateObject("Scripting.FileSystemObject")
    
    If oFso.FileExists( sFileName ) Then
        Set oFile = oFso.OpenTextFile( sFileName, ForWriting )
    Else 
        Set oFile = oFso.CreateTextFile( sFileName )
    End If 
    
    
    For Each sProperty In dProperties
        oFile.Write sProperty & ";"
    Next
    
    oFile.WriteLine
    
    For Each oTag In dIOTags
        For Each sProperty In dProperties
            oFile.Write Eval("oTag." & sProperty) & ";"
        Next
        oFile.WriteLine
    Next
    
    If ( Err ) Then
        ' TODO
    End If
    
End Sub

Sub ListIOTags( ByRef d )
    Set d = CreateObject("Scripting.Dictionary")
    
    ' Adicione aqui tags de interesse
    d.Add Application.GetObject("Modbus.Tag1"), Nothing
    d.Add Application.GetObject("Modbus.Tag2"), Nothing
    d.Add Application.GetObject("Modbus.Tag3"), Nothing
End Sub

Sub ListProperties( ByRef d )
    Set d = CreateObject("Scripting.Dictionary")
    
    ' Adicione aqui as propriedades de interesse
    d.Add "TimeStamp", Nothing
    d.Add "Name", Nothing
    d.Add "DocString", Nothing
    d.Add "Value", Nothing
    d.Add "Quality", Nothing
End Sub
1 Like