0

Trying to get a basic VB.NET Excel file creation app working, starting from some sample code from here, I'm getting err msgs with this code:

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")
    . . .

I first had tried this:

xlWorkSheet.SaveAs("C:\vbexcel.xlsx")

(failing to escape the backwhack), and this ("vbexcel.xlsx") was somehow mistaken for "52587100" as you can see here:

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. . .

By adding a subfolder to the path, and the escape characters, like so:

xlWorkSheet.SaveAs("C:\\misc\\vbexcel.xlsx")

...I got a similar error, but this time it just says it cannot access the file (without claiming the file name is named "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:

What is wrong with this code, that is preventing the file from being accessible?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    Some possible solutions: https://www.add-in-express.com/forum/read.php?FID=5&TID=12539 – Eric J. Oct 27 '15 at 22:21
  • 2
    The root of C is locked out from even Administrators (aka *Power Users*) now-a-days. You require **elevated permissions** to access it. –  Oct 27 '15 at 22:24

0 Answers0