473,657 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Microsoft.Offic e.Interop.Excel Help

KodeKrazy
32 New Member
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 4987
kadghar
1,295 Recognized Expert Top Contributor
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
KodeKrazy
32 New Member
You totally ROCK! I knew there had to be a relatively easy method due to the complexity of the excel.applicati on object. Just wasn't sure how to access it. Your sample code worked like a charm! Thanks a Million!

KK
Apr 15 '08 #3
KodeKrazy
32 New Member
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 Recognized Expert Top Contributor
Ok, here is what I have now:
Glad to be of help.

Seems good, is it allright?

Kad
Apr 15 '08 #5
KodeKrazy
32 New Member
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
KodeKrazy
32 New Member
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
KodeKrazy
32 New Member
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
KodeKrazy
32 New Member
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.Qui t 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 Recognized Expert Top Contributor
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.Qui t 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

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

Similar topics

2
27182
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? Thanks Jules
3
10784
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") wksExcel = wkbExcel.Worksheets(i)
5
27241
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 any of the methods because I cannot pull in the Microsoft.Office.Interop.Excel class. I am using Visual c# 2005 Express as my IDE, and here what I have done so far. I open a new project and select Project->Add Reference Then I go to the...
0
2887
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, ConsecutiveDelimiter:=False, Tab:=False, _ Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _ :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1),
0
1631
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
1303
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 Microsoft.Office.Core and not Microsoft.Office.Interop Does anybody know any solution for this ? Thanks,
0
1240
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 would highly be appreciated. Saradhamba
0
3726
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 the value. When I try to get a collection of named cells in the loop below, I come up with only one entry: 'Portfolio Company Information'!Print_Area The part before the ! is obviously my sheet name, but I have a whole slew of custom named cells in...
1
14031
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 explicitly?Hope you can help me. Thanks.
1
8503
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
8603
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
7320
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...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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
4151
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...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1944
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.