Excel VBA - Using Shell to Open a TXT file in Notepad that Begins with the First Part of the Filename?

MEUserII

Board Regular
Joined
Oct 27, 2017
Messages
91
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
Platform
  1. Windows
I have a macro which uses Shell to open a TXT file in Notepad. I am trying to get this macro to work with opening a TXT file in Notepad beginning with the first part of the filename; I have listed below my two attempts to have this work (MacroAttempt1 and MacroAttempt2):

Sub MacroAttempt1()
Dim filename1 As String
filename1 = "C:\Users\FLast\Desktop\Folder\1234 (ABCD-EFGH) (IJ) (03-21-2018__0000-00) (TXT).TXT"
Shell ("C:\Windows\system32\notepad.exe" & " " & filename1), vbNormalFocus
End Sub

Sub MacroAttempt2()
Dim filename2 As String
filename2 = "C:\Users\FLast\Desktop\Folder" & "1234" & "*(TXT).TXT"
Shell ("C:\Windows\system32\notepad.exe" & " " & filename2), vbNormalFocus
End Sub

The first macro, MacroAttempt1, works successfully. However, the second macro, MacroAttempt2, which is supposed to open the TXT file beginning with the first part of the filename "1234" and an asterisk ("*") and ends with "(TXT).TXT" does not work.

How would I fix this macro, so that it opens the TXT file beginning with the first part of the filename?
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
1) Try to use code tags when posting code

Code:
[COLOR=#333333]Sub MacroAttempt1()[/COLOR]
[COLOR=#333333]Dim filename1 As String[/COLOR]
[COLOR=#333333]filename1 = "C:\Users\FLast\Desktop\Folder\1234 (ABCD-EFGH) (IJ) (03-21-2018__0000-00) (TXT).TXT"[/COLOR]
[COLOR=#333333]Shell ("C:\Windows\system32\notepad.exe" & " " & filename1), vbNormalFocus[/COLOR]
[COLOR=#333333]End Sub[/COLOR]

[COLOR=#333333]Sub MacroAttempt2()[/COLOR]
[COLOR=#333333]Dim filename2 As String[/COLOR]
[COLOR=#333333]filename2 = "C:\Users\FLast\Desktop\Folder" & "1234" & "*(TXT).TXT"[/COLOR]
[COLOR=#333333]Shell ("C:\Windows\system32\notepad.exe" & " " & filename2), vbNormalFocus[/COLOR]
[COLOR=#333333]End Sub[/COLOR]

It makes it easier for everyone to read.


2) You cannot use shell to make notepad do what it otherwise won't do from the command line. If I open a CMD shell window and manually type in C:\Windows\system32\notepad.exe *.txt, I'll get an error message. If I instead type in "EditPad *.txt", Editpad (a notepad.exe alternative) will load all .txt files in the folder. This is because editpad supports wildcard file names, while notepad does not. You would be better off using the file open dialog to pick the file name, then use shell to open it in notepad

Code:
Sub MacroAttempt3()
    Dim filename3 As String
    filename3 = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    If filename3 <> "False" Then
        Shell ("C:\Windows\system32\notepad.exe" & " " & filename3), vbNormalFocus
    Else
        MsgBox "File selection cancelled by user"
    End If
End Sub
 
Upvote 0
How would I fix this macro, so that it opens the TXT file beginning with the first part of the filename?
Use the Dir function, which accepts a wildcard file name and returns the first matching file name, and specify that file with the folder path prepended as the Notepad parameter.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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