473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Microsoft.Office.Interop.Excel Help

KodeKrazy
I'm trying to read an Excel worksheet and do a find/replace for all of the commas "," in any of the cells in the sheet and replace them with a dash "-" I can get as far as getting the workbook opened but I'm not sure where to go from here. Is there a simple method to accomplish this?

Below is the code I have so far:

Expand|Select|Wrap|Line Numbers
  1. Dim exl As Microsoft.Office.Interop.Excel.Application
  2.  
  3.         Dim sht As Microsoft.Office.Interop.Excel.Worksheet
  4.  
  5.         exl = New Microsoft.Office.Interop.Excel.Application
  6.  
  7.         exl.Workbooks.Open("C:\...\WHOLESALEpl.xls")    ' Actual path omitted
Apr 15 '08 #1
10 4963
kadghar
1,295 Expert 1GB
I'm trying to read an Excel worksheet and do a find/replace for all of the commas "," in any of the cells in the sheet and replace them with a dash "-" I can get as far as getting the workbook opened but I'm not sure where to go from here. Is there a simple method to accomplish this?

Below is the code I have so far:

Expand|Select|Wrap|Line Numbers
  1. Dim exl As Microsoft.Office.Interop.Excel.Application
  2.  
  3.         Dim sht As Microsoft.Office.Interop.Excel.Worksheet
  4.  
  5.         exl = New Microsoft.Office.Interop.Excel.Application
  6.  
  7.         exl.Workbooks.Open("C:\...\WHOLESALEpl.xls")    ' Actual path omitted
So far so good. Now, if you have doubts working with your excel's vba, just record a macro and copy-paste the code. For what you want, something like this might be of help:

Expand|Select|Wrap|Line Numbers
  1. Dim Obj1 as object
  2. set obj1 = createobject("excel.application")
  3. ob1.workbooks.open("c:\...\WHOLESALEpl.xls")
  4. obj1.Cells.Replace What:=",", Replacement:="-", LookAt:=2, SearchOrder _
  5.         :=1, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
  6. obj1.visible=true
Now, just make sure to replace the xlconstants with its numeric value, since other VB than Excel's VBA wont recognize them. That's why, even when the macro recorder wrote LookAt:=xlPart, you should use LookAt:=2

HTH
Apr 15 '08 #2
You totally ROCK! I knew there had to be a relatively easy method due to the complexity of the excel.application object. Just wasn't sure how to access it. Your sample code worked like a charm! Thanks a Million!

KK
Apr 15 '08 #3
Ok, here is what I have now:
Expand|Select|Wrap|Line Numbers
  1. Dim exl As Microsoft.Office.Interop.Excel.Application
  2.  
  3.         Dim sht As Microsoft.Office.Interop.Excel.Worksheet
  4.  
  5.         exl = New Microsoft.Office.Interop.Excel.Application
  6.  
  7.         exl.Workbooks.Open("C:\...\WHOLESALEpl.xls")
  8.  
  9.         sht = exl.Workbooks(1).ActiveSheet
  10.  
  11.         exl.Cells.Replace(",", "-", 2, 1, False, False, False)
  12.  
  13.         sht.SaveAs("C:\...\WHOLESALEplfixed.csv", _
  14.                    FileFormat:="xlCSV")
  15.  
  16.         exl.Application.Quit()
Apr 15 '08 #4
kadghar
1,295 Expert 1GB
Ok, here is what I have now:
Glad to be of help.

Seems good, is it allright?

Kad
Apr 15 '08 #5
It works great (does the replace in all cells) until I try to do the SaveAs method. It then returns an unhandled COMException error, specifically: "Exception from HRESULT: 0x800A03EC" I have tried most of the other supposed "fixes" that I was able to find online (e.g.: Setting "Trust VB Project") in the actual Excel application. Alas, none of them were effective in alleviating the error. Any help is greatly appreciated.

KK
Apr 15 '08 #6
Actually, I did change a couple of lines of code in other foiled attempts at removing the error. The code currently looks like this:

Expand|Select|Wrap|Line Numbers
  1.                       Try
  2.  
  3.         Dim exl As New Microsoft.Office.Interop.Excel.Application
  4.  
  5.         Dim xlwb As Microsoft.Office.Interop.Excel.Workbook
  6.  
  7.         'Dim sht As Microsoft.Office.Interop.Excel.Worksheet
  8.  
  9.         xlwb = exl.Workbooks.Open("C:\...\WHOLESALEpl.xls")
  10.  
  11.          exl.Cells.Replace(",", "-", 2, 1, False, False, False)
  12.         exl.DefaultSaveFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV
  13.         exl.ActiveWorkbook.SaveAs("C:\...\WHOLESALEplfixed.csv", _
  14.         FileFormat:="xlCSV")
  15.  
  16.         exl.Application.Quit()
  17.         Catch ex As Exception
  18.         MsgBox(ex.Message)
  19.  
  20.         End Try
Apr 15 '08 #7
I finally got it to work!!! Here is the VB code:

Expand|Select|Wrap|Line Numbers
  1.        Try
  2.             Dim exl As New Microsoft.Office.Interop.Excel.Application
  3.  
  4.             Dim xlwb As Microsoft.Office.Interop.Excel.Workbook
  5.  
  6.             xlwb = exl.Workbooks.Open("C:\...\WHOLESALEpl.xls")
  7.  
  8.             exl.Cells.Replace(",", "-", 2, 1, False, False, False)
  9.             exl.DefaultSaveFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV
  10.             exl.ActiveWorkbook.SaveAs("C:\...\WHOLESALEplfixed.csv", _
  11.             6)
  12.  
  13.             exl.Application.Quit()
  14.         Catch ex As Exception
  15.             MsgBox(ex.Message)
  16.  
  17.         End Try
Note that I changed the File Format:= "xlCSV" to 6 (the value for xlCSV) did a "build" (since I am working in VS2008) instead of just a "debug". Ran it and it works like a charm!

Thanks again for your help & I hope this post can help another developer with a similar situation.

Cheers!

KK
Apr 15 '08 #8
One thing I noticed while debugging was that the EXCEL application was still running (according to Task Manager) until I stopped debugging. Is this normal behavior? Is there a way to make excel exit, to save system overhead, before the program exits completely? I thought that was handled by the Application.Quit method? Do I need to set it to Null or Nothing?

BTW & FTR - TSDN or Bytes or whatever we are calling it is the best forum on the web!!! I have posted on other forums and get the quickest, and most useful information from THIS site.

Thanks,

KK
Apr 15 '08 #9
kadghar
1,295 Expert 1GB
One thing I noticed while debugging was that the EXCEL application was still running (according to Task Manager) until I stopped debugging. Is this normal behavior? Is there a way to make excel exit, to save system overhead, before the program exits completely? I thought that was handled by the Application.Quit method? Do I need to set it to Null or Nothing?
That issue has been long discussed here. Search it in the site, you might find something. I'll check it out later too.

One quick way will be setting the application visible to true, and close it normaly

BTW & FTR - TSDN or Bytes or whatever we are calling it is the best forum on the web!!! I have posted on other forums and get the quickest, and most useful information from THIS site.

Thanks,

KK
^.^

Wow, those are nice words. Thank you.
Apr 15 '08 #10
As far as killing the instance of Excel in your processes, I found this kicking around another forum:

Expand|Select|Wrap|Line Numbers
  1. System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oExcel)
Explanation given was this: a COM assembly has a life of its own - freestanding from your code. This line is what instructs that object to die. I tested this fix, and it seemed to work for me.

Happy coding!

- MeanDude
Feb 8 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Julia | last post by:
Hi, I am trying to use the Microsoft.Office.Interop.Excel.dll but I dont know how. First, how do I install it? I have tried to run the register.bat but it doesnt work. How should I do this? ...
3
by: ©pEIO | last post by:
Example: Dim xlsExcel As Excel.Application Dim wkbExcel As Excel.Workbook Dim wksExcel As Excel.Worksheet xlsExcel = New Excel.Application wkbExcel = xlsExcel.Workbooks.Open("C:\Test.xls")...
5
by: C# newbie with big problems | last post by:
Ok, I've tried everything I could come up with but nothing is working. I am creating an application which needs to post information into an excel document. I have not yet had a chance to play with...
0
by: Cheryl | last post by:
I am new to C# and I am trying to convert this vba code to C#: Columns("A:A").Select Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote,...
0
by: pradeepkumarappagari | last post by:
by using 'Microsoft.Office.Interop.Excel.dll' can we set print settings of an excel file? if possible tell me what and all we can do by using 'Microsoft.Office.Interop.Excel.dll'.
0
by: insanomania911 | last post by:
I tried everything: - the refrerence to MS OFFICE 11.0 - Installed the PIA to the Office 2003 and made sure that the files are in the assembly folder When I add the reference I only get...
0
by: Saradhamba | last post by:
Hi, I have made use of the excel objects to export my data to excel. When i try debugging it throws error in the excel object being created. It throws "System.FileNotFound.Exception". Any help...
0
by: bfrank1972 | last post by:
I want to be able to get a list of all custom named fields in an Excel worksheet, but I am having trouble with this. In the code below, access to a field that I named "DEALCODE" works fine - I get...
1
by: romcab | last post by:
Hi guys, I'm trying to manipulate excel using a C#.net and my problem is that I cannot add the namespace Microsoft.Office.Interop.Excel. Does anyone know why? Do I need to download it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.