943,965 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 10191
  • C# RSS
Apr 18th, 2006
0

Using Microsoft Excel 10.0 Libary Object -Dang .dlls

Expand Post »
Hi All,
Well where I work they are having a problem and I had a solution. Well I was reading on this http://www.c-sharpcorner.com/UploadF...4-031b641aae3c

Well what it does is pull a named range, sticks the data in an array, my software does it's thing, then puts the changed data back into excel. I used the Office PIA and installed them and everything.

Several long nights, and much snaking I was done. I tested it and fixed some changes and it works beautiflully. I was so proud!!

Well I created a little setup packaged and started trying to install it on other machines. Well it installs prefectly but when I try to run the app a message box comes up and says in the title "Missing Reference", the message says "Please Reinstall Software'.

What?!? How can this be???? Well I did everything I know. Reinstalled it again and again, made sure the Office PIA were "registered" and installed. Made sure the .net framework was installed. Tried the install on several computers. Even installed VS.Net 2005 on another computer and when I open up the solution won't even build sucessfully.

So what it all boils down to is the app can only build and run on the original deveolper machines.

Here is the code, but the loops that I worked really hard on I deleted.

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Diagnostics;
  9. //great for using missing.value;
  10. using System.Reflection;
  11. using Microsoft.Office.Core;
  12. using System.Threading;
  13. using System.IO;
  14.  
  15.  
  16.  
  17. namespace DeannaProj2
  18. {
  19. public partial class Form1 : Form
  20. {
  21.  
  22. //declaring and object Called AppExcel
  23. private Excel.Application AppExcel = null;
  24.  
  25.  
  26. ArrayManipulator AM = new ArrayManipulator();
  27.  
  28. string strSource = null;
  29. string strRowCount = null;
  30. public string [,] arrySingle = null;
  31.  
  32.  
  33.  
  34. public Form1()
  35. {
  36. InitializeComponent();
  37. }
  38.  
  39.  
  40. //to take care of opening and picking the excel file
  41. private void btnExcel_Click(object sender, EventArgs e)
  42. {
  43.  
  44. exceldata.ShowDialog();
  45. strSource = exceldata.FileName;
  46. lblSource.Text = strSource;
  47.  
  48.  
  49. }
  50.  
  51. private void btnRow_Click(object sender, EventArgs e)
  52. {
  53.  
  54. //to check to see if user has selected an excel file
  55. if (strSource == "")
  56. {
  57. MessageBox.Show("Please Pick An Excel File", "Error");
  58. }
  59.  
  60. strRowCount = txtbxRowCount.Text;
  61. btnStart.Enabled = true;
  62. btnLabels.Enabled = true;
  63.  
  64. }
  65.  
  66.  
  67. private void btnStart_Click(object sender, EventArgs e)
  68. {
  69.  
  70.  
  71. //to check to see if user has selected an excel file
  72. if ((strSource == "") || (strSource == null))
  73. {
  74. MessageBox.Show("Please Pick An Excel File", "Error");
  75. }
  76.  
  77. //then creating and starting the application
  78.  
  79. InitializeComponent();
  80. AppExcel = new Excel.Application();
  81.  
  82. if (AppExcel == null)
  83. {
  84. MessageBox.Show("Could not start Excel");
  85. exceldata.ShowDialog();
  86. }
  87.  
  88.  
  89. //now declaring to start a new workbook readonly
  90. Excel.Workbook WB1 = AppExcel.Workbooks.Open(strSource, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
  91. 0, true, 0, 0);
  92.  
  93.  
  94.  
  95. //to get the compile a list of worksheets
  96. Excel.Sheets WBsheets = WB1.Worksheets;
  97.  
  98. //get the first and only worksheet the 1 if for the first sheet in the workbook
  99. Excel.Worksheet sheet = (Excel.Worksheet)WBsheets.get_Item(1);
  100.  
  101. //prep the row count by subtracting one cause of 0 based counting
  102. int intRowCount = System.Convert.ToInt32(strRowCount);
  103.  
  104. strRowCount = System.Convert.ToString(intRowCount);
  105.  
  106. //here to prep the rows by adding a Colum 5 ("E")
  107. strRowCount = "E" + strRowCount;
  108.  
  109.  
  110. //here you define your range
  111. Excel.Range range = sheet.get_Range("A1", strRowCount);
  112. //here you pull the data out
  113. System.Array arryReturn = (System.Array)range.Cells.Value2;
  114.  
  115.  
  116.  
  117.  
  118. //good old cleanup
  119. arryReturn = null;
  120. arrySource = null;
  121.  
  122. //end of dangerous code
  123. #endregion
  124.  
  125.  
  126. //to show excel
  127. // AppExcel.Visible = true;
  128.  
  129.  
  130. //going to kill excel so it removes the deadlock on the file
  131. KillExcel();
  132.  
  133.  
  134. }
  135.  
  136. private void btnKillExcel_Click(object sender, EventArgs e)
  137. {
  138. //will call KillExcel procedure
  139. KillExcel();
  140. }
  141.  
  142. private void btnLabels_Click(object sender, EventArgs e)
  143. { //will build the schema for the array
  144. AM.arryTemp = new string[arrySingle.Length / 5, 5];
  145. //this will then copy the contents of the array ____MAKE NOTE_____
  146. Array.Copy(arrySingle, AM.arryTemp, arrySingle.Length);
  147. //Will start the multiplying process
  148. AM.Multiplexer();
  149. //Start the Write To Excel Process
  150. WriteToExcel();
  151.  
  152. }
  153.  
  154. public void WriteToExcel()
  155. {
  156. //then creating and starting the application
  157.  
  158. InitializeComponent();
  159. AppExcel = new Excel.Application();
  160.  
  161. if (AppExcel == null)
  162. {
  163. MessageBox.Show("Could not start Excel");
  164. exceldata.ShowDialog();
  165. }
  166.  
  167.  
  168. //now declaring to start a new workbook
  169. Excel.Workbook WB1 = AppExcel.Workbooks.Open(strSource, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
  170. 0, true, 0, 0);
  171.  
  172. //to get the compile a list of worksheets
  173. Excel.Sheets WBsheets = WB1.Worksheets;
  174.  
  175. //get the first and only worksheet the 2 if for the first sheet in the workbook
  176. Excel.Worksheet sheet = (Excel.Worksheet)WBsheets.get_Item(2);
  177.  
  178. //will take the length of arryFinal and divide by five cause multi deimension array
  179. int intLenght = AM.arryFinal.Length / 5;
  180.  
  181.  
  182.  
  183.  
  184. //now that the array has then been fed and written to excel, the WorkBook must be saved to the hard drive
  185. sheet.SaveAs(strSource, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
  186. Missing.Value, Missing.Value, Missing.Value, Missing.Value);
  187. try
  188. {
  189.  
  190. //messagebox to show the process is done
  191. MessageBox.Show("The Multiplexing is complete.", "Completed");
  192. }
  193. catch
  194. {
  195. MessageBox.Show("Operation Cancled By User (Did Not Save Over)", "Error");
  196. }
  197. }
  198.  
  199. public void KillExcel()
  200. {
  201.  
  202. //this will fish out all the process that have excel
  203. //the secret is "process name w/o .exe"
  204. Process[] process = Process.GetProcessesByName("Excel");
  205. for (int i = 0; i < process.GetLength(0); i++)
  206. {
  207. //kills excel process
  208. process[i].Kill();
  209.  
  210.  
  211. }
  212. }
  213.  
  214. private void Form1_Load(object sender, EventArgs e)
  215. {
  216.  
  217. #region Licensing
  218. //pulls the hostname (LBMISTECH1) all caps too
  219. string strHostName = System.Environment.MachineName;
  220. // add the "\r\n" so it will match the line we will pull out of the text file
  221. strHostName = strHostName + "\r\n";
  222. //will assign where the file needs to go
  223. string strPath = @"c:\windows\system32\work.mul";
  224.  
  225. //see if file work.mul exists in sytem32 folder
  226. if (File.Exists(strPath))
  227. {
  228. MessageBox.Show("Welcome To Multiplexer.", " Written By Lee Hicks");
  229. }
  230. else
  231. {
  232. //this will execute if file is not present
  233. MessageBox.Show("Please reinstall application", "Missing reference");
  234. //will close the form
  235. this.Close();
  236.  
  237. }
  238.  
  239. //this will try to open the text file to see if anything exists
  240. try
  241. {
  242. //create a streamreader to read the text file
  243. StreamReader srWorkMul = File.OpenText(strPath);
  244. //dumb the contents from file to the string
  245. string strContents = srWorkMul.ReadToEnd();
  246. // test to see if contents of text file match the hostname
  247. if (strContents == strHostName)
  248. {
  249. //do nothing
  250. }
  251. else
  252. {
  253. MessageBox.Show("Please reinstall application", "Missing reference");
  254. //once again kill sthe app
  255. this.Close();
  256. }
  257.  
  258. }
  259. catch
  260. {
  261. MessageBox.Show("Please reinstall application", "Missing reference");
  262. //if the applications can't find the file to open will close it again
  263. this.Close();
  264.  
  265. }
  266. #endregion
  267.  
  268.  
  269. }
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. }// end class
  280. }// end namespace

Also I did add my own little version of licensing so the company I work for dosen't take my program and sell it without me knowing it. If anyone can help me with the Office Primary Interop Assemblies I would be thankful. I need this asap and is currently rewriting my app using OLE DB and a SQL server. But I would like to atleast get this first version working so people and actually see that it works and my second version is comming along slowly.

Help plz!!!!
Lee
Last edited by alc6379; Apr 19th, 2006 at 1:39 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
blacklocist is offline Offline
87 posts
since Apr 2006
Apr 19th, 2006
0

Re: Using Microsoft Excel 10.0 Libary Object -Dang .dlls

i didnt really read your code so im not sure what your doing with the data. but anyways im sure all you need is ms office installed on the machine for it to work.

if all you are doing is reading and writing to a excel file you could try using oledb with jet 4.0 and the excel driver in the conn string. then that will let you read the excel file like a database.
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Apr 19th, 2006
0

Re: Using Microsoft Excel 10.0 Libary Object -Dang .dlls

I'd have to agree with plazmo on this one.

You will probably have to have Office installed on all of the machines that you're planning on depolying to. I think that the Excel stuff you're doing relies upon COM, which in itself relies upon the regeistered presense of Excel/Office in order to do all of its work.

I hate to say your work was in vain, but if you had used oleDb with Jet4.0, as plazmo described, the only thing your clients would need is the correct version of .NET CLR installed on their machines.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Apr 19th, 2006
0

Re: Using Microsoft Excel 10.0 Libary Object -Dang .dlls

Well I already checked that, all the computers where I work have Office XP. The users would hunt me if I didn't cause they all like Excel.

Well I will just have to accept that my work is in vain. Well thanks for all the feed back and I am working oledb. Just real slow cause I have never used it before till now.

Also just something to chew on. For Microsoft being big into programming and make VS you would think since they pride their product Office that they would make it much much easier to write apps, especially for their developing software. Ohhh well
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
blacklocist is offline Offline
87 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: getting details from the loop
Next Thread in C# Forum Timeline: Help me How to write basic prorgram for Bluetooth Dongle





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC