Click button to open notepad

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
919
Office Version
  1. 365
Platform
  1. Windows
So, I have a button that says "Open Notepad". I have a textbox txtIni.Text that might have filepath, like "c:\ini.txt". If "Open Notepad" button is pressed, program should check that does that file exists, if it does, then it opens the notepad and loads that file. If it doesn't exists, then it should just load the notepad.

I tried this

Code:
Private Sub btnNotePad_Click()
    Dim retval As Variant
    
    If FileOrDirExists(txtIni.Text) Then
        If Not txtIni.Text = "" Then
            retval = Shell("notepad.exe " & txtIni.Text & ", 1")
        Else
            retval = Shell("notepad.exe", 1)
        End If
    Else
        retval = Shell("notepad.exe", 1)
    End If
End Sub
I think there's something wrong with that "Shell ("notepad.exe " & txtIni.Text & ", 1")" line.

That FileOrDirExists() function checks whether the file exists or not.
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
It should read:
Code:
      retval = Shell("notepad.exe " & txtIni.Text, 1)
 
Upvote 0
Does it run notepad if you simply use:
Code:
retval = shell("notepad.exe")
 
Upvote 0
Does it run notepad if you simply use:
Code:
retval = shell("notepad.exe")

No, but if I open Run from the Start menu, notepad.exe works well.

By the way, what is supposed to be the datatype of retval? String? Some kind of object?
 
Upvote 0
No, but if I open Run from the Start menu, notepad.exe works well.

By the way, what is supposed to be the datatype of retval? String? Some kind of object?

It returns a variable, numerical value - a Windows Task ID value I think. It can safely be dimmed as a "Double" in your procedure.

As for your problem try specifying the fullpath to Notepad as GettingBetter suggests. You shouldn't need to but something odd is occurring.
 
Upvote 0
It seems to work now. Here's the code in case someone else has some use for it too:

Code:
Private Sub btnNotePad_Click()
    Dim retval As Double
    If FileOrDirExists(txtIni.Text) Then
        If Not txtIni.Text = "" Then
            retval = Shell("C:\WINDOWS.0\notepad.exe " & txtIni.Text, 1)
        Else
            retval = Shell("C:\WINDOWS.0\notepad.exe", 1)
        End If
    Else
        retval = Shell("notepad.exe", 1)
    End If
End Sub

Like said, that FileOrDirExists(txtIni.Text) check if the file in that text box exists.


Thank you for all of you for your help! Again I'm a step closer to completing my thesis and thus, graduation.
 
Upvote 0
Oh, one more question: How can I easily determine what is the Windows directory? For me it's c:\Windows.0, but usually it's just c:\Windows.
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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