Results 1 to 7 of 7

Thread: Using Excel

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    33

    Using Excel

    I have designed a small software that, after user input, exports all the data into a template Excel File that has all the report format pre edited. (i dont want to use the report feature as i want this data in Excel). I have already written the code for exporting and it works fine.
    In a day around 5 such reports will be printed. But the problem is that once this data is exported i want it to automatically use the "Save As" function instead of save and it shud save in pre-set location and i shud have access to the name property of the file so that an input box pops up asking the name, whatever i type in that shud be the name of the file and it shud go to the pre-set location and it shud be a new instance of the template so that the original template is still there for further use.
    Ive never worked in VBA before so im a lil weak in this, someone please guide me.
    Thank You

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Using Excel

    it wouldn't be in vba. you'd write it in vb after your exporting.
    we need to see your export code...

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    33

    Re: Using Excel

    This is the code that i have used to create and export.. Now i need to work on saving it..


    Code:
     
            Dim MatchExcel As New Excel.Application
            MatchExcel.Workbooks.Add()
            MatchExcel.Visible = True
            With MatchExcel
                .Range("A1").Select()
                Dim numrows As Integer = 0
                Do Until numrows = P1RowCount
                    .ActiveCell.Offset(numrows, 0).Value = DataGridView1.Item(0, numrows).Value
                    .ActiveCell.Offset(numrows, 1).Value = DataGridView1.Item(1, numrows).Value
                    .ActiveCell.Offset(numrows, 2).Value = DataGridView1.Item(2, numrows).Value
                    .ActiveCell.Offset(numrows, 3).Value = DataGridView1.Item(3, numrows).Value
                    .ActiveCell.Offset(numrows, 4).Value = DataGridView1.Item(4, numrows).Value
                    .ActiveCell.Offset(numrows, 5).Value = DataGridView1.Item(5, numrows).Value
                    .ActiveCell.Offset(numrows, 6).Value = DataGridView1.Item(6, numrows).Value
                    .ActiveCell.Offset(numrows, 7).Value = DataGridView1.Item(7, numrows).Value
                    numrows += 1
                Loop
            End With

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Using Excel

    something like this:

    Code:
    Dim MatchExcel As New Excel.Application
    MatchExcel.Workbooks.Add()
    MatchExcel.Visible = True
    With MatchExcel
        .Range("A1").Select()
        Dim numrows As Integer = 0
        Do Until numrows = P1RowCount
            .ActiveCell.Offset(numrows, 0).Value = DataGridView1.Item(0, numrows).Value
            .ActiveCell.Offset(numrows, 1).Value = DataGridView1.Item(1, numrows).Value
            .ActiveCell.Offset(numrows, 2).Value = DataGridView1.Item(2, numrows).Value
            .ActiveCell.Offset(numrows, 3).Value = DataGridView1.Item(3, numrows).Value
            .ActiveCell.Offset(numrows, 4).Value = DataGridView1.Item(4, numrows).Value
            .ActiveCell.Offset(numrows, 5).Value = DataGridView1.Item(5, numrows).Value
            .ActiveCell.Offset(numrows, 6).Value = DataGridView1.Item(6, numrows).Value
            .ActiveCell.Offset(numrows, 7).Value = DataGridView1.Item(7, numrows).Value
            numrows += 1
        Loop
    End With
    
    Dim sfd As New SaveFileDialog
    If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
        MatchExcel.Workbooks(0).SaveAs(sfd.FileName)
    End If

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    33

    Re: Using Excel

    this works great but the problem is now the user needs to type the file name and select the path. The file name is already saved in the form of a string and the path is always same, so cant i integrate these two into the code so that i can eliminate the user making errors while saving, if they choose the wrong path or wrong file name it will be a problem.
    Thank you

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Using Excel

    instead of:

    Code:
    Dim sfd As New SaveFileDialog
    If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
        MatchExcel.Workbooks(0).SaveAs(sfd.FileName)
    End If
    Code:
    MatchExcel.Workbooks(0).SaveAs(path)

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2013
    Posts
    33

    Re: Using Excel

    Thank You very Much .Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width