Open a file with notepad through VBA

前端 未结 5 599
一向
一向 2020-12-19 23:39

I am trying to open a file with notepad.

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

set OFS = myOFSO.OpenTextFile(strfilename) 

I

相关标签:
5条回答
  • 2020-12-20 00:08

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

    returnvalue = Shell("notepad.exe " & strfilename, vbNormalFocus)
    
    0 讨论(0)
  • 2020-12-20 00:09

    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
    
    0 讨论(0)
  • 2020-12-20 00:16

    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
    
    0 讨论(0)
  • 2020-12-20 00:27

    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)
    0 讨论(0)
  • 2020-12-20 00:34

    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)
    
    0 讨论(0)
提交回复
热议问题