how to programming in C#

W

Weimin Zhang

Hi, following is the VBA code generated by Macro in Excel
XP (2002). What I would like to do is to incorporate the
same thing into C#, but I don't know how to pass the Array
() to C# in the Workbooks.OpenText() method. Can anyone
please help me on how to pass these parameters for this
method in C#? Thanks a lot!

Sub WMTest()
'
' WMTest Macro
' Macro recorded 10/6/2003 by WEIMIN ZHANG
'

'
Workbooks.OpenText
Filename:="C:\MMS\BatchPrint\dave.txt", Origin:=437, _
StartRow:=1, 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), Array(5, 1), Array(6,
1), Array(7, 1), Array(8, 1), Array(9, 1)), _
TrailingMinusNumbers:=True
ChDir "C:\MMS\BatchPrint"
ActiveWorkbook.SaveAs
Filename:="C:\MMS\BatchPrint\dave.xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:=False _
, CreateBackup:=False
End Sub
 
R

Robert Jacobson

I don't really have time to dig into your code, but here's an article that
shows you how to translate VBA macros to C#. It has a code snippet showing
the SaveAs method. (The OpenText method should be similar.)

Excel, Exchange, and C#
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp05152003.asp

Also, here is the documentation for the Excel object model, which should
give you details on the parameters that you need for your OpenText and and
SaveAs methods:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/off2000/html/xlmthOpenText.asp

Also, for what it's worth, I don't think you need the ChDir line at all.
It's an old VB function for changing the active directory. Since you're
using a fully-qualified DOS path in the SaveAs method, though, I think it's
unnecesary.
 
W

Weimin Zhang

Thanks a lot for your valuable time spent on my issues!

I got it work anyway. Just wonder why there are so little
help docs on MSDN for programming Office in C#?
 
R

Robert Jacobson

Glad to hear it. Probably because the Office object model was written in
VBA (Visual Basic), and most Office developers have probably migrated to
VB.Net. However, I think Microsoft is making an effort to support both
languages for Office development.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top