hereのサンプルコードから始めて、基本的なVB.NET Excelファイル作成アプリを機能させようとすると、次のコードでエラーメッセージが表示されます。
Private Sub ButtonCreateExcelFile_Click( sender As Object, e As EventArgs) Handles ButtonCreateExcelFile.Click
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkBook.Application.Visible = True
xlWorkBook.Application.ScreenUpdating = False
xlWorkSheet = xlWorkBook.Sheets("sheet1")
'xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
xlWorkSheet.SaveAs("C:\\misc\\vbexcel.xls")
. . .
私は最初にこれを試しました:
xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
(バックワックを回避できませんでした)、そしてこれ(「vbexcel.xlsx」)は、次のように「52587100」と誤解されていました:
System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2146827284
HelpLink=C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM
HResult=-2146827284
Message=Microsoft Office Excel cannot access the file 'C:\52587100'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
Source=Microsoft Office Excel. . .
次のように、サブフォルダーをパスに追加し、エスケープ文字を追加します。
xlWorkSheet.SaveAs("C:\\misc\\vbexcel.xlsx")
...同様のエラーが発生しましたが、今回はファイルにアクセスできないというだけです(ファイル名が「52587100」であるとは主張していません):
System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2146827284
HelpLink=C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM
HResult=-2146827284
Message=The file could not be accessed. Try one of the following:
このコードの何が問題で、ファイルにアクセスできないのでしょうか?