Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
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?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Cutting/pasting from Excel.Application object - missing the obvious?

David,
Do you really a 2nd Excel instance ? It is easier to stick in the same
instance.
Also declare all your variables

dim csvImport as workbook

Set csvImport = Workbooks.OpenText Filename:=importFile, _
DataType:=xlDelimited, _
TextQualifier:=xlTextQualifierNone, _
Comma:=True, _
FieldInfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2))


NickHK

"David Evans" wrote in message
...
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?




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Cutting and Pasting from SQL Table to Excel Sheet Chaplain Doug Excel Discussion (Misc queries) 0 April 25th 08 04:55 PM
Cutting and Pasting in Excel Rev. Munchkindad Excel Discussion (Misc queries) 19 September 5th 07 08:40 PM
can excel auto correct when cutting and pasting bsmile29 Excel Discussion (Misc queries) 5 June 21st 06 09:06 PM
Must Be Missing Something Obvious About ListBoxes Johnny Meredith Excel Programming 2 June 4th 05 08:49 AM
Application-defined or object-defined error - missing the basics [email protected] Excel Programming 1 December 28th 04 10:23 PM


All times are GMT +1. The time now is 05:12 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"