Workbooks.OpenText Open File Dialog

breynolds0431

Active Member
Joined
Feb 15, 2013
Messages
303
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi. Trying to have files run the fixed-width automatically with Workbooks.opentext. The problem I'm running into is trying to get an open file dialog to open so the user can select the text file to convert. Below is what I have so far, but it stops running when the file opens to the import wizard.

Thanks for any help on this.

Code:
Sub CTConvert()

Dim myfile As String



Workbooks.OpenText filename:=myfile, _
        Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
        Array(0, 1), Array(1, 1), Array(14, 1), Array(24, 1), Array(33, 1), Array(41, 1), Array(55, _
        1), Array(65, 1), Array(66, 1), Array(74, 1), Array(82, 1), Array(102, 1), Array(112, 1), _
        Array(121, 1), Array(129, 1), Array(143, 1), Array(153, 1), Array(154, 1), Array(170, 1)) _
        , TrailingMinusNumbers:=True


myfile = GetFile(myfile)


Workbooks(myfile).Close
  
End Sub


Public Function GetFile(ByVal strPath As String) As String
    
    Application.Dialogs(xlDialogOpen).Show Arg1:="*.*"
    
End Function
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
This will prompt the user to select a text file. However, if the user clicks on Cancel, it exits the procedure.

Code:
Dim myfile As Variant

myfile = Application.GetOpenFilename( _
                                      FileFilter:="Text Files (*.txt), *.txt", _
                                      Title:="Select text file", _
                                      ButtonText:="Open")
                                      
If myfile = False Then Exit Sub

Note that you won't be able to close the workbook using Workbooks(myfile).Close since myfile contains the full path in addition to the filename. But if the workbook remains the active workbook after it has been opened, you can close it like this, assuming you don't want to save any changes that may have been made...

Code:
activeworkbook.Close SaveChanges:=False

Hope this helps!
 
Last edited:
Upvote 0
Hi Domenic. Thank you for the idea. However, after I added, I get a 1004 run-time for "Sorry, we couldn't find . Is it possible it was moved, renamed, or deleted?" Did I add your code right? See update below:

Code:
Sub CTConvert()

Dim myfile As Variant



Workbooks.OpenText filename:=myfile, _
        Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
        Array(0, 1), Array(1, 1), Array(14, 1), Array(24, 1), Array(33, 1), Array(41, 1), Array(55, _
        1), Array(65, 1), Array(66, 1), Array(74, 1), Array(82, 1), Array(102, 1), Array(112, 1), _
        Array(121, 1), Array(129, 1), Array(143, 1), Array(153, 1), Array(154, 1), Array(170, 1)) _
        , TrailingMinusNumbers:=True


myfile = Application.GetOpenFilename( _
                                      FileFilter:="Text Files (*.txt), *.txt", _
                                      Title:="Select text file", _
                                      ButtonText:="Open")
                                      
If myfile = False Then Exit Sub
  
End Sub
 
Upvote 0
It should be the other way around...

Code:
Sub CTConvert()

    Dim myfile As Variant
    
    myfile = Application.GetOpenFilename( _
                                          FileFilter:="Text Files (*.txt), *.txt", _
                                          Title:="Select text file", _
                                          ButtonText:="Open")
                                          
    If myfile = False Then Exit Sub
      
    
    Workbooks.OpenText Filename:=myfile, _
            Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
            Array(0, 1), Array(1, 1), Array(14, 1), Array(24, 1), Array(33, 1), Array(41, 1), Array(55, _
            1), Array(65, 1), Array(66, 1), Array(74, 1), Array(82, 1), Array(102, 1), Array(112, 1), _
            Array(121, 1), Array(129, 1), Array(143, 1), Array(153, 1), Array(154, 1), Array(170, 1)) _
            , TrailingMinusNumbers:=True




End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top