Open a file with notepad through VBA

2020-02-13 03:01发布

I am trying to open a file with notepad.

const strfilename = "C:\Users\Desktop\abc.txt"

set OFS = myOFSO.OpenTextFile(strfilename) 

It is displaying

error code 424

标签: excel vba
5条回答
唯我独甜
2楼-- · 2020-02-13 03:12

If you want to create a new text-file and want to write into it, you could use this approach:

Sub writeToTextFile()
    Dim i As Long
    Open ThisWorkbook.Path & "\newFile.txt" For Output As #1

    Print #1, "This could be the first line"
    Print #1, "this the second"

    For i = 3 To 10
        Print #1, "this is the " & i & ". line"
    Next

    Close #1
End Sub
查看更多
啃猪蹄的小仙女
3楼-- · 2020-02-13 03:17

Below code will help you to open notepad from excel.

Dim fso As Object
Dim sfile As String
Set fso = CreateObject("shell.application")
sfile = "C:\Users\Universal\Desktop\test.txt"
fso.Open (sfile)
查看更多
Explosion°爆炸
4楼-- · 2020-02-13 03:20

Could just call a shell command to execute notepad.exe with the file path.

returnvalue = Shell("notepad.exe " & strfilename, vbNormalFocus)
查看更多
The star\"
5楼-- · 2020-02-13 03:23

Try this approach

Sub Test()
Dim strCont     As String

strCont = LoadFileStr$(ThisWorkbook.Path & "\Sample.txt")
Range("A1").Value = strCont
End Sub

Function LoadFileStr$(FN$)
With CreateObject("Scripting.FileSystemObject")
    LoadFileStr = .OpenTextFile(FN, 1).readall
End With
End Function
查看更多
Juvenile、少年°
6楼-- · 2020-02-13 03:31

Open any file with the default program for the filetype: (with a single line of code)

CreateObject("Shell.Application").Open("c:\Users\Desktop\abc.txt")

If you want to use this method with a filetype that isn't yet associated with an application:

  • Hit the Windows Key(Windows Key)
  • Start typing default
  • Click "Default Apps" (Windows 10) or "Default Programs" (Windows 7)
查看更多
登录 后发表回答