View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
David Evans[_3_] David Evans[_3_] is offline
external usenet poster
 
Posts: 3
Default Cutting/pasting from Excel.Application object - missing the obvious?

Hi,

as part of a VBA macro in an Excel workbook, I am trying to open a CSV that
has date fields in. Workbooks.open doesn't give me consistent date or text
fields so I can't order them, so instead I am using OpenText.

The problem is that, while I can now open the CSV correctly, I don't seem to
be able to control the new Excel.Application - everything I do still works
on the calling/original Excel.Application.

Edited code consists of:

importFile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
'End macro if no csv selected
If importFile = "False" Then GoTo Exits
Set reportFile = ThisWorkbook
'Copy header from the current workbook
Sheets("Title").Select
Rows("1:1").Select
Selection.Copy
'Create new Workbook with the data I want to copy into the current workbook
Set csvImport = CreateObject("Excel.Application")
csvImport.Workbooks.OpenText Filename:=importFile, _
DataType:=xlDelimited, _
TextQualifier:=xlTextQualifierNone, _
Comma:=True, _
FieldInfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2))
'How do I paste the header row I copied above into the newly created
csvWorkbook...???
Set csvWorkbook = csvImport.ActiveWorkbook
csvImport.Visible = True
csvWorkbook.Activate
csvImport.ActiveWindow.Activate
MsgBox csvWorkbook.Name
'csvWorkbook IS the right thing, but how do I activate it in order to be
able to interact with it?
Rows("1:1").Select
ActiveSheet.Paste
'This pastes into the original, calling "Title" sheet, not the new
csvWorkbook in csvImport!
Cells.Select
Selection.Copy
reportFile.Activate
Sheets("Base").Select
Range("A1").Select
ActiveSheet.Paste
'This ends up copying the whole "Title" sheet and pasting it into the "Base"
sheet, not copying the new csvWorkbook and pasting that


There would appear to be two avenues:

1. Activate csvWorkbook and manipulate it, the commands for which I'm
missing

or

2. run ThisApplication.Workbooks.OpenText ... - but I don't know how to
reference the current application, hence trying to go via creating a new one
and giving it a handle.

Any ideas?