在 VBA 中从数组创建 CSV

发布于 2024-10-02 06:45:36 字数 835 浏览 2 评论 0原文

我是一名新手程序员,试图自动化一些重复的工作场所任务,这些任务应该由聪明的脚本而不是人类来完成。我已经完成了一些 VBA 和 Java,但都是非常基础的东西。

我们有一些由经过验证的在线表单生成的数据,这些数据通过电子邮件发送到邮箱,然后邮件客户端 (Outlook 2010) 中的过滤器将其放入特定文件夹中。我的目的是编写一些 VBA,当触发时将检查该文件夹的内容,然后对于每个邮件项目,以字符串形式获取邮件正文文本,创建一个数组,然后将数组内容写入 .csv 文件。然后,我将让另一个应用程序(适用于 Firefox 的 iMacros)读取 csv 并与内部企业 Web 应用程序交互来完成其余的工作。

我想我可以做所有这些,除了将数组内容写入 csv 之外。

我通过查看代码示例(我确实尝试理解 MS 对象模型文档)来工作得最好,但是我能找到的将 VBA 数组写入 CSV 的最好方法是:

  'save the file
  Open sFileName For Output As #7
  For n = 0 To UBound(MyArray, 1)
    Print #7, Format(MyArray(n, 0), "0.000000E+00")
  Next n
  Close #7

我认为这是 VB 而不是 VBA,但是这里的总体想法有效吗?或者,如果其他人有一个代码示例来帮助我入门或任何其他指示,我将非常感激。

PS还看到如何保存二维数组作为 csv 文件? 但无法理解它。

I'm a novice programmer trying to automate some repetitive workplace tasks that should be done by a clever script instead of humans. I've done some VBA and Java, but very basic stuff.

We have some data generated by a validated online form that gets emailed to a mailbox, and then a filter in the mail client (Outlook 2010) puts it into a particular folder. My intention is to write some VBA that when triggered will check the contents of that folder, and then for each mail item, get the message body text as a string, create an array, and then write the array contents into a .csv file. I'll then have another app (iMacros for Firefox) read the csv and interact with an in-house corporate webapp to do the rest.

I think I can do all of that except write the array contents to csv.

I work best from looking at code examples (I do try to understand the MS Object Model documentation), but the best I can find for writing a VBA array to CSV is something like:

  'save the file
  Open sFileName For Output As #7
  For n = 0 To UBound(MyArray, 1)
    Print #7, Format(MyArray(n, 0), "0.000000E+00")
  Next n
  Close #7

I think this is VB and not VBA, but is the general idea here valid? Or if anyone else has a code sample to get me started or any other pointers I'd be very grateful.

PS Have also seen How do I save a 2-dimensional array as a csv file? but can't understand it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难得心□动 2024-10-09 06:45:36

下面的代码适用于将二维数组转换为 CSV。我没有编写代码,但已经测试过它并且有效!

' SaveAsCSV saves an array as csv file. Choosing a delimiter different as a comma, is optional.
'
' Syntax:
' SaveAsCSV dMyArray, sMyFileName, [sMyDelimiter]
'
' Examples:
' SaveAsCSV dChrom, app.path & "\Demo.csv"       --> comma as delimiter
' SaveAsCSV dChrom, app.path & "\Demo.csv", ";"  --> semicolon as delimiter
'
' Rev. 1.00 [8 jan 2003]
' written by P. Wester
' [email protected]


Public Sub SaveAsCSV(MyArray() As Variant, sFilename As String, Optional sDelimiter As String = ",")

Dim n As Long 'counter
Dim M As Long 'counter
Dim sCSV As String 'csv string to print

On Error GoTo ErrHandler_SaveAsCSV


'check extension and correct if needed
If InStr(sFilename, ".csv") = 0 Then
  sFilename = sFilename & ".csv"
Else
  While (Len(sFilename) - InStr(sFilename, ".csv")) > 3
    sFilename = Left(sFilename, Len(sFilename) - 1)
  Wend
End If

'If MultiDimensional(MyArray()) = False Then '1 dimension

  'save the file
'  Open sFileName For Output As #7
'  For n = 0 To UBound(MyArray(), 1)
'    Print #7, Format(MyArray(n, 0), "0.000000E+00")
'  Next n
'  Close #7

'Else 'more dimensional

  'save the file
  Open sFilename For Output As #7
  For n = 1 To UBound(MyArray(), 1)
    sCSV = ""
    For M = 1 To UBound(MyArray(), 2)
      sCSV = sCSV & Format(MyArray(n, M)) & sDelimiter
    Next M
    sCSV = Left(sCSV, Len(sCSV) - 1) 'remove last Delimiter
    Print #7, sCSV
  Next n
  Close #7

'End If

Exit Sub


ErrHandler_SaveAsCSV:
  Close #7

The code below works for converting 2D arrays in CSV. I have not written the code, but have tested it and it works!

' SaveAsCSV saves an array as csv file. Choosing a delimiter different as a comma, is optional.
'
' Syntax:
' SaveAsCSV dMyArray, sMyFileName, [sMyDelimiter]
'
' Examples:
' SaveAsCSV dChrom, app.path & "\Demo.csv"       --> comma as delimiter
' SaveAsCSV dChrom, app.path & "\Demo.csv", ";"  --> semicolon as delimiter
'
' Rev. 1.00 [8 jan 2003]
' written by P. Wester
' [email protected]


Public Sub SaveAsCSV(MyArray() As Variant, sFilename As String, Optional sDelimiter As String = ",")

Dim n As Long 'counter
Dim M As Long 'counter
Dim sCSV As String 'csv string to print

On Error GoTo ErrHandler_SaveAsCSV


'check extension and correct if needed
If InStr(sFilename, ".csv") = 0 Then
  sFilename = sFilename & ".csv"
Else
  While (Len(sFilename) - InStr(sFilename, ".csv")) > 3
    sFilename = Left(sFilename, Len(sFilename) - 1)
  Wend
End If

'If MultiDimensional(MyArray()) = False Then '1 dimension

  'save the file
'  Open sFileName For Output As #7
'  For n = 0 To UBound(MyArray(), 1)
'    Print #7, Format(MyArray(n, 0), "0.000000E+00")
'  Next n
'  Close #7

'Else 'more dimensional

  'save the file
  Open sFilename For Output As #7
  For n = 1 To UBound(MyArray(), 1)
    sCSV = ""
    For M = 1 To UBound(MyArray(), 2)
      sCSV = sCSV & Format(MyArray(n, M)) & sDelimiter
    Next M
    sCSV = Left(sCSV, Len(sCSV) - 1) 'remove last Delimiter
    Print #7, sCSV
  Next n
  Close #7

'End If

Exit Sub


ErrHandler_SaveAsCSV:
  Close #7
○闲身 2024-10-09 06:45:36

我不使用 VB,所以我不确定输出 #7 的打开文件,但如果它有效(使用一些随机输出测试它),那么您的代码片段是正确的。唯一的问题是您应该进行另一个 Print 调用,其中添加一个“,”。

只要确保在最后一次迭代中没有添加“,”即可。您可以通过添加一个 If 语句来检查是否 n = UBound(MyArray) - 1 (这是最后一个元素的索引。)

希望它有帮助,

PM

I don't work with VB so I'm not sure about the open file for output #7 but if that works (test it with some random output), then your code snippet is right. The only thing is that you should have another Print call where you add a ",".

Just make sure that on your last iteration you don't add the ",". You can do this by adding an If statement that will check if n = UBound(MyArray) - 1 (Which is the index of the last element.)

Hope it helps,

PM

旧城烟雨 2024-10-09 06:45:36

这是一个基于 Excel 的解决方案。如果您从 Excel 控制 Outlook,这可能是一个可行的解决方案。此外,如果您正在开发 Excel 项目,这可能会有用。

Private Sub ArrayToCsvWorkborkMethod(MyArray As Variant, FileName As String)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim WB As Workbook
Set WB = Application.Workbooks.Add
Dim WS As Worksheet
Set WS = WB.Sheets(1)

Dim Target As Range
Set Target = Range("A1").Resize(UBound(MyArray, 1), UBound(MyArray, 2))
Target.Value = MyArray
WB.SaveAs FileName, 23
WB.Close
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

我是这样解决的。这速度较慢,但​​消除了先前解决方案可能出现的任何奇怪的格式。
这将创建一个新工作簿,将数组写入工作表,将该新工作簿另存为 CSV 文件,然后关闭新工作簿。

至少对我来说,这工作起来更加可靠,并且可以消除任何格式问题,从而变得更加出色。

This is an excel based solution. If you control outlook from excel this could be a viable solution. Additionally if you are working on an excel project this could be of use.

Private Sub ArrayToCsvWorkborkMethod(MyArray As Variant, FileName As String)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim WB As Workbook
Set WB = Application.Workbooks.Add
Dim WS As Worksheet
Set WS = WB.Sheets(1)

Dim Target As Range
Set Target = Range("A1").Resize(UBound(MyArray, 1), UBound(MyArray, 2))
Target.Value = MyArray
WB.SaveAs FileName, 23
WB.Close
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

I solved it this way instead. This is slower but eliminates any weird formatting the previous solution might do.
This creates a new workbook, Writes your array to the worksheet, saves that new workbook as a CSV file and than closes the new workbook.

At least for me this works much more reliably and offloads any formatting concerns to excel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文