Starting excel

Can anybody tell me how I can open the FileName1 xls so that I can manipulate it's contents.  The code below works fine if the excel application is open.  If it's not alreday open the code will not work.
 
Set xApp = New Excel.Application
  Filename1 = App.Path & "\SSB" & x & ".xls"
  FileName2 = App.Path & "\SSB" & x & ".hld"
  Set xWork = GetObject(Filename1)
   xWork.Application.Columns("F:L").Select
   xWork.Application.Range("F2").Activate
   xWork.Application.ActiveCell.Activate
   xWork.Application.Selection.NumberFormat = "@"
   xWork.SaveAs FileName2, xlCSV

Thanks!

Regards
vgruAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wesleystewartCommented:

Filename1 = App.Path & "\SSB" & x & ".xls"
Set xApp = Getobject(Filename1)
set xSheet = xApp.worksheets(1)
This should start Excel if it isn't running, or use an existing instance of Excel if it is running.

You then work with xSheet, like:

xSheet.Columns("F:L").Select

Hope this helps.

Wes
0
adityauCommented:
Use the following code. Hope, it works.

Set xApp = New Excel.Application
Filename1 = App.Path & "\SSB" & x & ".xls"
FileName2 = App.Path & "\SSB" & x & ".hld"
Set xApp = New Excel.Application
xApp.Workbooks.Open filename1
xApp.Visible = True
Set xsheet = xApp.worksheets(1)
xWork.Application.Columns("F:L").Select
xWork.Application.Range("F2").Activate
xWork.Application.ActiveCell.Activate
xWork.Application.Selection.NumberFormat = "@"
xApp.SaveAs FileName2, xlCSV
xApp.Quit
Set xWork=Nothing
Set xWork = Nothing
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wesleystewartCommented:
You both might want to know that you don't need to use the "select" and "activate" methods when using Excel as an automation server.

xsheet.range("F2").numberformat = "@"

will do the trick without all those "select" and "activate" methods.

adityau:

Correct me if I'm wrong, but I don't think your code will work because xWork isn't set to an object variable.  Rather than referencing xWork.Application (which has to be causing an error) you should be referencing xSheet

You might want to make sure your code is bulletproof before posting it as an answer and locking up the question.

Wes
0
adityauCommented:
I am sorry.

Set xsheet = xApp.worksheets(1)

should be

Set xWork = xApp.worksheets(1)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.