Solved

How do I get an asp.net (vb.net) TextBox value into a cell on a pre-existing spreadsheet in Excel?

Posted on 2006-11-05
8
539 Views
Last Modified: 2008-02-26

In VBscript this button-click code works fine:

  Sub OpenExcel_OnClick

      Set objExcel = CreateObject("Excel.Application")
      Set objWorkbook = objExcel.Workbooks.Open("C:\default.xls")

      objExcel.Visible = True
      objExcel.Cells(2, 2).Value = "Scooby Doo"
       
  End Sub

However, if I want the value of "Scooby Doo" to come from an entry typed into a textbox on the interface, it doesn't work:

  Sub OpenExcel_OnClick

      Set objExcel = CreateObject("Excel.Application")
      Set objWorkbook = objExcel.Workbooks.Open("C:\default.xls")

      objExcel.Visible = True
      objExcel.Cells(2, 2).Value = TextBox1.Text
       
  End Sub

If I use an html 'TextField' control or a 'TextBox' control it doen't matter.... the cell remains empty but the spreadsheet launches fine.

How do I get this textbox value into the spreadsheet cell?  There has to be a way because txtfields on asp.net applications seem to be very common on the web for collecting user information. Please help if you can....I'm dying on this one and my head hurts from bangin' it..... many thanks.








0
Comment
Question by:jazjef
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17877099
Is this client-side VBScript running in an ASP.NET page? If so, then the chances are that your text box hasn't been rendered with an ID of "TextBox1". View the page source and find the text box. If you want, post the full page source and I'll have a look.
0
 
LVL 4

Author Comment

by:jazjef
ID: 17877212
Thanks Muzzy2003.... here's my full page source:


<%@ Import Namespace="System.IO" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication22.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
            <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
            <SCRIPT language="VBScript">
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<!-- Instruct non-IE browsers to skip over VBScript modules.

  Sub OpenExcel_OnClick

      Set objExcel = CreateObject("Excel.Application")
      Set objWorkbook = objExcel.Workbooks.Open("C:\default.xls")

      objExcel.Visible = True
      'objExcel.Workbooks.Add
      'objExcel.Cells(2, 2).Value = "Scooby Doo"
      objExcel.Cells(2, 3).Value = TextBox1.Text

  End Sub

-->
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            </SCRIPT>
      </HEAD>
      <body bgColor="#ffffff" MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  &nbsp;<INPUT id="cmdOpenExcel" type="button" value="OpenExcel" name="OpenExcel">
                  <asp:textbox id="TextBox1" style="Z-INDEX: 102; LEFT: 72px; POSITION: absolute; TOP: 72px" runat="server"
                        Width="88px">Scooby Doo</asp:textbox></form>
      </body>
</HTML>
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17877371
No, I meant the source as it's rendered to the browser. But never mind. Try replacing the relevant line of your script block with:

     objExcel.Cells(2, 3).Value = TextBox1.value

If that doesn't work, try this:

     objExcel.Cells(2, 3).Value = <%= TextBox1.ClientID %>.value
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 16

Expert Comment

by:muzzy2003
ID: 17877399
Basically, you are writing client-side script, where the property you want to access is the DHTML value property. The Text property is a server-side one that you would access in VB.NET (or C#) code behind. Additionally, giving an ASP.NET control a particular ID does not mean that it will be rendered to the browser with that DHTML id - that is what the second version will address.
0
 
LVL 4

Author Comment

by:jazjef
ID: 17877404
OK.... here's the source as rendered to the browser upon debugging.... the '.value'  and '<%= TextBox1.ClientID %>.value' suggestions don't work either:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
            <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
            <SCRIPT language="VBScript">
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<!-- Instruct non-IE browsers to skip over VBScript modules.

  Sub OpenExcel_OnClick

      Set objExcel = CreateObject("Excel.Application")
      Set objWorkbook = objExcel.Workbooks.Open("C:\default.xls")

      objExcel.Visible = True
      'objExcel.Workbooks.Add
      objExcel.Cells(2, 2).Value = "Scooby Doo"
      objExcel.Cells(2, 3).Value = TextBox1.Text
      
      
  End Sub

-->
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            </SCRIPT>
      </HEAD>
      <body bgColor="#ffffff" MS_POSITIONING="GridLayout">
            <form name="Form1" method="post" action="WebForm1.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwxMzkwODI1NzQ5Ozs+Z7pN57S0wq/0mCPwcWq04fLRxRA=" />

                  &nbsp;<INPUT id="cmdOpenExcel" type="button" value="OpenExcel" name="OpenExcel">
                  <input name="TextBox1" type="text" value="Scooby Doo" id="TextBox1" style="width:88px;Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 72px" /><INPUT style="Z-INDEX: 102; LEFT: 72px; WIDTH: 88px; POSITION: absolute; TOP: 120px; HEIGHT: 24px"
                        type="text" size="9" value="Scooby Doo" id="TextField1"></form>
      </body>
</HTML>
0
 
LVL 16

Accepted Solution

by:
muzzy2003 earned 500 total points
ID: 17877412
Try this:

     objExcel.Cells(2, 3).Value = document.getElementById('TextBox1').value
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17877431
Or failing that:

     objExcel.Cells(2, 3).Value = document.getElementById('<%= TextBox1.ClientID %>').value

which should be pretty certain to work if (possibly) a little unnecessary.
0
 
LVL 4

Author Comment

by:jazjef
ID: 17877493
muzzy2003:

Thanks guy!!!  You nailed it dude! Works like a dream.....

PS:  I did have to replace ('TextBox1') with ("TextBox1") in double quotes so it wouldn't 'comment out' .... but that did it just fine!


For others looking for this answer, here's the code that works using my original question and muzzy2003's solution:

Sub OpenExcel_OnClick

     Set objExcel = CreateObject("Excel.Application")
     Set objWorkbook = objExcel.Workbooks.Open("C:\default.xls")

     objExcel.Visible = True
     objExcel.Cells(2, 2).Value = "Scooby Doo"
     objExcel.Cells(2, 3).Value = document.getElementById("TextBox1").value
         
  End Sub

0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question