2 Replies - 682 Views - Last Post: 04 February 2012 - 02:59 PM Rate Topic: -----

Topic Sponsor:

#1 m4_prashanth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-February 12

VB.NET Excel SaveAs option Error.........

Posted 04 February 2012 - 08:16 AM

Hi there,

I was trying to edit a excel document and save under different name and my coding for this is as follows.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Open
        Dim MyExcel As New Excel.Application
        MyExcel.Workbooks.Open("C:\Test\WalkAir.xlsx")

        'Extract
        MyExcel.Sheets("sheet1").Activate()
        MyExcel.Range("A99").Activate()

        MyExcel.ActiveCell.Value = TextBox1.Text


        MyExcel.SaveAs("C:\Test\alkAir.xlsx")
        MyExcel.Workbooks.Close()
        MyExcel = Nothing

    End Sub


While debugging the code it resulting with me an error message as Public member 'SaveAs' on type 'ApplicationClass' not found.

I would be much obliged if you can help me in this issue.

Best Regards

Is This A Good Question/Topic? 0
  • +

Replies To: VB.NET Excel SaveAs option Error.........

#2 Beach_Coder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 10-November 11

Re: VB.NET Excel SaveAs option Error.........

Posted 04 February 2012 - 09:36 AM

You've got to add a bit after the file name. The .xlsx bit you have is fine, but it is a part of the filename string only. Try:

MyExcel.SaveAs("Whatever.xlsx", FileFormat:=xlWorkbookNormal)



You may have to do this:
MyExcel.SaveAs("Whatever.xlsx", Excel.xlFileFormat.FileFormat:=xlWorkbookNormal)



If you type Excel.xlFileFormat all of the possible fileformats should pop up. Then make sure there isn't any inconsistency among the extension you have on your filename string and the file format you have chosen.
Was This Post Helpful? 0
  • +
  • -

#3 Ionut  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 327
  • View blog
  • Posts: 914
  • Joined: 17-July 10

Re: VB.NET Excel SaveAs option Error.........

Posted 04 February 2012 - 02:59 PM

SaveAs is member of Workbook class, not ExcelApplication class. MSDN link
MyExcel.Workbooks[1].SaveAs('Name.xlsx')



Also, try defining an object of type Workbook.
Dim wb as Excel.Workbook

wb = MyExcel.Workbooks.Open("C:\Test\WalkAir.xlsx")
'some code
wb.SaveAs('name.xlsx')
wb.Close()
wb = Nothing
MyExcel.Quit()
MyExcel = Nothing



Using Excel interop, you work with unmanaged memory. .NET handles quite badly the whole concept and for each .(dot) you apply, you will end up with memory leak. So, one undocumented rule when working with Excel from .NET is to use maximum 2 dots(best it is with one)
MyExcel.Workbooks.Worksheets[1].Cells[2, 1].Value ' NO

Dim ws As Worksheet
ws = MyExcel.Workbooks.Worksheets[1]
ws.Cells[2, 1].Value 'YES - you can reduce the number, but this is an example


This post has been edited by Ionut: 04 February 2012 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1