473,699 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Workbook.SaveA s" Failing

Greetings,

I have the code below where I am instantiating Excel and then attempting
to the save the new workbook (as "Test.xls") . However, on the line of
code that attempts to save the workbook, I receive the following error
message:

"Object reference not set to an instance of an object."

Any ideas as to what I am doing wrong? [Note: oWB.Save() works fine,
just not oWB.SaveAs()].

Thanks in advance!

*************** *************** *************** *************

public static void CreateExcelWork book(string spName)
{
Excel.Applicati on oXL;
Excel._Workbook oWB;
Excel._Workshee t oSheet;
Excel.Range oRng;
try
{
GC.Collect(); // clean up any other excel guys hangin'
around...
oXL = new Excel.Applicati on();
oXL.Visible = true;
//Get a new workbook.
oWB = (Excel._Workboo k)(oXL.Workbook s.Add(
Missing.Value ));
oSheet = (Excel._Workshe et)oWB.ActiveSh eet;
//get our Data
string myConnString =
System.Configur ation.Configura tionSettings.Ap pSettings
["Connection "];
SqlConnection myConnection = new SqlConnection
(myConnString);
SqlCommand myCommand = new SqlCommand
(spName,myConne ction);
myConnection.Op en();
SqlDataReader myReader;
myReader = myCommand.Execu teReader();
// Create Header and sheet...
int iRow =2;
for(int j=0;j<myreader. fieldcount;j++)
{
oSheet.Cells[1, j+1] = myReader.GetNam e(j).ToString
();
}
// build the sheet contents
while (myReader.Read( ))
{
// Loop through each field.
for(int k=0;k < myReader.FieldC ount;k++)
{
oSheet.Cells[iRow,k+1]= myReader.GetVal ue
(k).ToString();
}
iRow++;
}// end while
myReader.Close( );
myReader = null;
oSheet.get_Rang e("A1", "Z1").Font. Bold = true;
oSheet.get_Rang e("A1", "Z1").VerticalA lignment =
Excel.XlVAlign. xlVAlignCenter;
//AutoFit columns A:Z.
oRng = oSheet.get_Rang e("A1", "Z1");
oRng.EntireColu mn.AutoFit();
oXL.Visible = true;
oXL.UserControl = true;
string strFile ="C:\report" +
System.DateTime .Now.Ticks.ToSt ring() +".xls";
Debug.WriteLine ("strFile : " + strFile);
// Code fails on next line.
oWB.SaveAs
("C:\\Test.xls" ,Excel.XlFileFo rmat.xlWorkbook Normal,null,nul l,false,fals
e,Excel.XlSaveA sAccessMode.xlS hared,false,fal se,null,null,nu ll);
...

*** Sent via Developersdex http://www.developersdex.com ***
May 3 '06 #1
0 5435

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7922
by: lir | last post by:
Hi, I am trying to save a fram in a page using the execCommand("saveas") the html is save currectly but I can not save the gif on that page. How can I save the fram with the gif via execCommand("saveas"). I would like the user to choose where to put the file on his/her loca hard drive. Thanks, Liro - li
4
3172
by: mescaline | last post by:
Hi, I am looking for a C++ programming "workbook" I have learned some basic C++ OOP such as classes, inheritance, polymorphism, virtual functions, encapsulation and all those la-la terms... bascially from Bruce Eckel's and the Sams 21 days books. I have done some programming for classes (like inheriting classes, using protected, a little bit of templates) but I want to get sharpen
2
10891
by: psanjay | last post by:
Hi, Am unable to open javascript SaveAs dialog box in Firefox browser. Following is the script, <a href="javascript:void(0);" onclick="document.execCommand('SaveAs',false,'*.xyz');">Save this page</a> The above script works fine in IE. Can anyone please let me know how do I make the SaveAs dialog box open in Firefox browser. Thanks, Sanjay
3
1605
Coldfire
by: Coldfire | last post by:
Hello, The issue is: I have a webpage that displays image in img tag while taking its src from a remote url ( dynamically). Now, my client want this functionality to ask for "Save at any location" routine when someone clicks on the image so that user can avoid right-click saveas. Platform: xp/asp.net2.0/web-application PLz help asap
4
4214
by: GavReynolds | last post by:
Hi All! I am using Javascript to export some content in a popup (in this case, just 5 simple words) to a text file. This works fine and the user can save the text file. Upon opening the file, it appears to be just a basic text file, however, i encounterd a problem importing this into flash (thats another story!). The text file needs to consist of just a line of text for the import (ie. word1=hello&word2=world), this is how it appears in...
6
22964
by: mrobinson86 | last post by:
Hey guys, wondered if anybody could help me out with a pretty simple piece of code that has been driving me crazy for the past few days? I am trying to create a client side self sustaining message board system using basic DHTML to keep all of my colleges up to date. I have successfully built functions to control the posting of messages, but as I try to create a method which completes an overwrite/save as command over the same html file I...
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.