General information
Category:
Coding, syntax & commands
Title:
Open a WorkBook - from Word
Hi All,
I'm trying to open an Excel workbook and import a .txt file. I get an error on the "WorkBooks.Open" command - 4th statement in the sub below. What am I doing wrong? TIA.
Regards,
Paige
Option Explicit
Sub ImportTextUsingOpenText()
'\ Use this approach to pre-set all of the text import values
'\ and validate file selection before performing import
Dim sFile
Application.ScreenUpdating = False
Application.DisplayAlerts = False
WorkBooks.Open
Worksheets.Add
Range("A1") = "Open Text Method"
Range("A2").Select
'\ get file name from user
sFile = Application.GetOpenFilename( _
FileFilter:="Text Files (*.txt), *.txt", FilterIndex:=1, _
Title:="Copy Text File To Worksheet")
If sFile <> False Then
'\ supply other OpenText parameters here
WorkBooks.OpenText FileName:=sFile '\import text to temporary worksheet
ActiveSheet.UsedRange.Select '\ select imported text in temporary workbook
Selection.Copy '\ copy to clipboard
ActiveWorkbook.Close '\ close temporary workbook
ActiveSheet.Paste '\ paste text into your workbook
End If
End Sub
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only