创建新的Excel工作簿文件将返回无效/损坏的错误

时间:2017-09-13 11:01:56

标签: excel vba excel-vba file

我正在尝试使用VBA创建一个新的excel工作簿。不幸的是,它一直给我这个错误信息:

enter image description here

关于如何创建新工作簿的任何想法。以下代码适用于CSV文件。

我认为该行:Open PathName & "\Test12345.xlsm" For Output Lock Write As #OutputFileNum导致问题。

Sub WriteFile()

  Dim ColNum As Integer
  Dim Line As String
  Dim LineValues() As Variant
  Dim OutputFileNum As Integer
  Dim PathName As String
  Dim RowNum As Integer
  Dim SheetValues() As Variant

  PathName = "C:\Wa Guide\9 Projects\1 Bet365\Betting System Output\Z"
  OutputFileNum = FreeFile

  Open PathName & "\Test123.xls" For Output Lock Write As #OutputFileNum

  Print #OutputFileNum, "Field1" & "," & "Field2"

  SheetValues = Sheets("Sheet1").Range("A1:H9").Value
  ReDim LineValues(1 To 8)

  For RowNum = 1 To 9
    For ColNum = 1 To 8
      LineValues(ColNum) = SheetValues(RowNum, ColNum)
    Next
    Line = Join(LineValues, ",")
    Print #OutputFileNum, Line
  Next

  Close OutputFileNum

End Sub

0 个答案:

没有答案