Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8135563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:27:44+00:00 2026-06-06T10:27:44+00:00

I’m having some trouble with looping and creating MS Excel docs, code snippet below

  • 0

I’m having some trouble with looping and creating MS Excel docs, code snippet below

Private Sub selectedRowsButton_Click( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles selectedRowsButton.Click

    Dim selectedRowCount As Integer = _
        DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)

    If selectedRowCount > 0 Then

        Dim sb As New System.Text.StringBuilder()
        Dim objexcel As New Excel.Application
        Dim i As Integer
        Dim FACode As Integer
        Dim Sitename As Integer
        Dim Sitecode As Integer
        Dim Address As Integer
        Dim City As Integer
        Dim State As Integer
        Dim ZIP As Integer


        FACode = 1
        Sitename = 5
        Sitecode = 2
        Address = 6
        City = 7
        State = 9
        ZIP = 10
        Dim xlWorkbook As Excel.Workbook
        xlWorkbook = objexcel.Workbooks.Open("template path")
        For i = 0 To selectedRowCount - 1

            objexcel.Visible = True
            objexcel.Range("B2").Value = DataGridView1.SelectedCells(Sitename).Value.ToString()

            objexcel.Range("B3").Value = DataGridView1.SelectedCells(Sitecode).Value.ToString()

            objexcel.Range("B5").Value = DataGridView1.SelectedCells(FACode).Value.ToString()

            Dim thisfile As Object


            thisfile = objexcel.Range("B5").Value & "." & _
            objexcel.Range("B3").Value & "." & "otherstring" & "." & "otherstring2" & "." & ".xls"

            With objexcel

                xlWorkbook.SaveAs(Filename:="c:\test\" & thisfile)
                '~~> Close the Excel file without saving
                xlWorkbook.Close(False)
            End With          

        Next i

    End If

I’m getting the error Exception from HRESULT: 0x800A03EC for the statement

  objexcel.Range("B2").Value = DataGridView1.SelectedCells(Sitename).Value.ToString()

IF I select only one row of my DataGrid before creating the program works fine, it is when I select multiple rows that this error occurs. Since I’m creating the program specifically for multiple row selections I’m stumped as to where I’ve gone wrong. Any help or pointers appreciated, Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:27:46+00:00Added an answer on June 6, 2026 at 10:27 am

    Two things

    1. You have declared objexcel As Excel.Application so you shouldn’t use objexcel.Range("B2").Value. Use xlWorkbook.Range("B2").Value. Change it everywhere in your code.

    2. You cannot use SaveAs like that. See the snapshot below. If you want to save as xls file then you have to use FileFormat:=56

    See this code example

    '~~> Save As file
    xlWorkbook.SaveAs(Filename:="c:\test\" & thisfile, FileFormat:=56)
    

    If you do not specify the file format then you will get an error message when you open the file after opening.

    You might want to look at this link on how to automate Excel from VB.Net

    Topic: VB.NET and Excel

    Link: http://www.siddharthrout.com/vb-dot-net-and-excel/

    enter image description here

    I am not too sure what you exactly are trying to do with the DGV. Like Sean mentioned you are not incrementing the values. If you can post a snapshot of how your DGV looks and how your Excel file should look after the export then we can help you in a much better way 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.