none
Open and save Excel File in Visual Basic Express 2008

    Question

  • Hi guys,

    I'm using VB Express 2008 and I would like to open an excel file and save it with different name file.

    For example,  I would like to open a file in C:\test.xls and then I want to save them with different file name such as C:\try.xls

    What's the code for doing that?

    I know this is a stupid question.....but I'm a newbie

    Thanks

    Monday, April 19, 2010 3:20 PM

Answers

  • Hi eefendy, here is the basics of how to open and save an Excel file. You need to first ensure that you import the name space. Also make a reference to Microsoft Excel 12.0 or 11.0 (whatever version your computer has) object library.

    Imports.Microsoft.Office.Interop

    Next you will need to declare your variables

    Private xls As New Excel.Application

     Private xlwb As Excel.Workbook

    Then you will use this code to open and save your file with a new file name.

    xlwb = xls.Workbooks.Open(WorkbookPath)

     xlwb.SaveAs(FilePath)
    I hope this works for you, let me know.


    Jeremy Western MCPD Windows – OmniDevSolutions jwestern at omnidevsolutions dot com Please make sure to mark answers as helpful when used and answered when completed.
    Monday, April 19, 2010 4:26 PM
  • Hi,

    yes. I'll post you in addition the MSDN Library Page http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel%28office.11%29.aspx

    There you can find a description of Excel Interop and you'll find examples how to work with the ExcelInterop.

    Regards

    martinwy

    • Marked as answer by eefendy Sunday, May 02, 2010 4:01 AM
    Monday, April 19, 2010 4:43 PM

All replies

  • Hi eefendy, here is the basics of how to open and save an Excel file. You need to first ensure that you import the name space. Also make a reference to Microsoft Excel 12.0 or 11.0 (whatever version your computer has) object library.

    Imports.Microsoft.Office.Interop

    Next you will need to declare your variables

    Private xls As New Excel.Application

     Private xlwb As Excel.Workbook

    Then you will use this code to open and save your file with a new file name.

    xlwb = xls.Workbooks.Open(WorkbookPath)

     xlwb.SaveAs(FilePath)
    I hope this works for you, let me know.


    Jeremy Western MCPD Windows – OmniDevSolutions jwestern at omnidevsolutions dot com Please make sure to mark answers as helpful when used and answered when completed.
    Monday, April 19, 2010 4:26 PM
  • Hi,

    yes. I'll post you in addition the MSDN Library Page http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel%28office.11%29.aspx

    There you can find a description of Excel Interop and you'll find examples how to work with the ExcelInterop.

    Regards

    martinwy

    • Marked as answer by eefendy Sunday, May 02, 2010 4:01 AM
    Monday, April 19, 2010 4:43 PM
  •  

    From my example, is this what you mean.....

    xlwb = xls.Workbooks.Open("C:\test.xls")

     xlwb.SaveAs("C:\try.xls")

     

    Sincerely,

    Eddy

     

    Monday, April 19, 2010 5:01 PM
  • Martinwy,

    Thanks martin. I'll definately use this.

     

    Sincerely,

    Eddy

    Monday, April 19, 2010 5:02 PM
  • Hi eefendy, yes, that is what I mean.
    Jeremy Western MCPD Windows – OmniDevSolutions jwestern at omnidevsolutions dot com Please make sure to mark answers as helpful when used and answered when completed.
    Monday, April 19, 2010 5:09 PM
  • Thanks Jeremy.

    I'll try the code then.....

    Sincerely,

    Eddy

    Monday, April 19, 2010 7:42 PM
  • Hi!

    Try this

    Save all information on Textbox in Current Directory - Recommended to portable application.

    Insert a simple Button1 or in Form1.load

            Dim appendMode As Boolean = True '<--- Overwrite all information
            Dim sw As New StreamWriter("YourFile\example.xls ", appendMode, System.Text.Encoding.Unicode) '<--- Create a new file on current directory
            sw.Write(textbox1.Text) '<--- Write all information
            sw.Close() '<--- Close all information 

            Dim appendMode2 As Boolean = True '<--- Overwrite all information
            Dim sw2 As New StreamWriter("YourFile\example.xlsx ", appendMode, System.Text.Encoding.Unicode) '<--- Create a new file on current directory
            sw2.Write(textbox1.Text) '<--- Write all information
            sw2.Close() '<--- Close all information

            Dim appendMode3 As Boolean = True '<--- Overwrite all information
            Dim sw3 As New StreamWriter("YourFile\example.doc ", appendMode, System.Text.Encoding.Unicode) '<--- Create a new file on current directory
            sw3.Write(textbox1.Text) '<--- Write all information
            sw3.Close() '<--- Close all information     

            Dim appendMode4 As Boolean = True '<--- Overwrite all information
            Dim sw4 As New StreamWriter("YourFile\example.docx ", appendMode, System.Text.Encoding.Unicode) '<--- Create a new file on current directory
            sw4.Write(textbox1.Text) '<--- Write all information
            sw4.Close() '<--- Close all information

            Dim appendMode5 As Boolean = True '<--- Overwrite all information
            Dim sw5 As New StreamWriter("YourFile\example.txt ", appendMode, System.Text.Encoding.Unicode) '<--- Create a new file on current directory
            sw5.Write(textbox1.Text) '<--- Write all information
            sw5.Close() '<--- Close all information


            Dim appendMode6 As Boolean = True '<--- Overwrite all information
            Dim sw6 As New StreamWriter("YourFile\example.rtf ", appendMode, System.Text.Encoding.Unicode) '<--- Create a new file on current directory
            sw6.Write(textbox1.Text) '<--- Write all information
            sw6.Close() '<--- Close all information

     

    With SaveFileDialog : Insert "Button1" in the "Form1 "

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

            On Error Resume Next

            Dim file As String
            With Me.SaveFileDialog1
                .DefaultExt = "xls"
                .FileName = file
                .Filter = "Excel 2003(*.xls)|*.xls|Excel 2007(*.xlsx)|*.xlsx|All Files(*.*)|*.*"
                .OverwritePrompt = True
                .Title = "Save information by Naldo"
                '
                If Me.SaveFileDialog1.ShowDialog = DialogResult.OK Then
                    file = Me.SaveFileDialog1.FileName
                    Dim objwriter As StreamWriter = New StreamWriter(file)
                    objwriter.Write(Me.TextBox1.Text)
                    objwriter.Close()
                    objwriter = Nothing

                End If

            End With

        End Sub

     

    If you need more help, contact me.

    Arnaldo Rivera

    admsystems@live.com

    Monday, April 19, 2010 9:34 PM
  • Hello eefendy,

    Thanks for your post.

    Is your problem solved? If there is no problem, please mark the helpful replies as anwers. Thanks.

    If you still have any problems, please feel free to contact me.

    wish you a nice day. :)

    Best regards,

    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    Friday, April 23, 2010 8:27 AM
  • Hi guys,

    I'm terribly sorry for not replying to all your kind message!!  .....SORRY!!......

    I've been very busy with school works....

    AND YES, all of them those codes works!!

    I do have a small problem though......

    You know how you save it as the same file, it will have a MessageBox that said "the file is there....overwrite them?" (something like that)

    How can I use VB express code to automatically click "Yes" button?!?!

    Anybody having this problem??

    Again, I'm really sorry and I DO REALLY APPRICIATE ALL OF YOUR HELP!!!!......I'll try to check my post more often.....

    THANKS FOR EVERYTHING GUYS!!  :)

     

    Sincerely,

    Eddy

     

    Ps: Did anybody have the same problem with the MessageBox? and what is the code to automatically click the "Yes" button on VB Express 2008?

    Sunday, May 02, 2010 3:47 AM
  • JWolf,

    Thanks for the reply and sorry for taking forever to reply back!

    My next question is, how do you automatically click the "Yes" button when there is a MessageBox that said something like "You already have the file name....overwrite it?" when I'm trying to save the same file.

     

    Sincerely,

    Eddy

    Sunday, May 02, 2010 3:51 AM