0

refering to this link Creating an Excel Application from VB.Net

I want to save my excel file to this path:

C:\Users\asdfme\Documents

with the filename format [PROJ_DATE].xls example:PROJ_20140703.xls here is my code:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call temporaryValue()
        Dim appXL As Excel.Application
        Dim wbXl As Excel.Workbook
        Dim shXL As Excel.Worksheet
        Dim raXL As Excel.Range
        Dim strPathExcel As String

        ' Start Excel and get Application object.
        appXL = CreateObject("Excel.Application")
        appXL.Visible = True
        ' Add a new workbook.
        wbXl = appXL.Workbooks.Add
        shXL = wbXl.ActiveSheet
        ' Add table headers going cell by cell.
        Dim heads() As String = {"", "MONo", "PostingDate", "DocDate", "SBU", "Operation", "Resource", "TimeType", "StartDate", "StartTime", "EndDate", "EndTime", "NoofResources", "Remarks", "Quantity", "Rejects"}
        For head1 As Integer = 1 To 15
            shXL.Cells(1, head1).Value = heads(head1)
        Next
        Dim body() As String = {"", txtmo.Text, PostingDate, DocDate, SBU, Operation, Session("user"), (ddltype.SelectedValue).ToUpper, lblsDate.Text, lblsTime.Text, lbleDate.Text, lbleTime.Text, no_ofres, lblrem.Text, txtgoods.Text, txtrejects.Text}
        For body1 As Integer = 1 To 15
            shXL.Cells(2, body1).Value = body(body1)
        Next
        appXL.Visible = True
        appXL.UserControl = True
        ' Release object references.
        wbXl.SaveAs(Filename:="C:\Users\asdfme\Documents\PROJ_20140703.xls")
        raXL = Nothing
        shXL = Nothing
        wbXl = Nothing
        appXL.Quit()
        appXL = Nothing
        Exit Sub
Err_Handler:
        MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
    End Sub

when i run this code,

  • it only create an excel file and ask me if i want to save the file.
  • what i want is whenever i run this, the file will save with the file name format (written above) in the path i declare.

Please help. thank you

13
  • Fine, and the question is? Commented Jul 3, 2014 at 9:13
  • I edit my question. :) I want to save the file directly to my path: C:\Users\asdfme\Documents without asking me whether to save or dont save the file. thank you Commented Jul 3, 2014 at 9:23
  • wbXl.SaveAs(Filename:="C:\Users\asdfme\Documents\PROJ_20140703.xls") Commented Jul 3, 2014 at 9:42
  • 2
    i have tried your code its working correctly on my environment, try to close all excel sheets in your computer and change the file location, i used to save the excel file to wbXl.SaveAs(Filename:="C:\Documents and Settings\All Users\Desktop\PROJ_20140703.xls") Commented Jul 3, 2014 at 9:59
  • 1
    Let me get this straight. Your code runs on an ASP.NET page on the SERVER SIDE and you want to save this file on the CLIENT machine? If this is the case there are no chance you could do that with that code on a live hosted server. The path your express is relative to the SERVER machine, not the client one Commented Jul 3, 2014 at 10:06

1 Answer 1

1
Dim myXlsFileName As String = "PROJ_" & " " & Format(Now.Date, "yyyyddMM") & ""
wbXl.SaveAs(Filename:="C:\Documents and Settings\All Users\Desktop\" & myXlsFileName & ".xls")
Sign up to request clarification or add additional context in comments.

4 Comments

thanks hector, but how will i know if the file is already exist?
If Not File.Exists("C:\Documents and Settings\All Users\Desktop\" & myXlsFileName & ".xls") Then 'create file End If
hi hector, you help me a lot but i am having a problem in saving the file again
thank you hector ^_^ i solve it with the file name ^_^ thank you again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.