Search code examples
vbawindowsms-officenotepad

How to write txt file at same file path but preventing window to update notepad display


I like to show string in notepad for easier debug purpose. I have been used the following VBA codes to display string for years. I use office 2021.

Public Sub writeTxt(ByVal sOri As String)
    Const sFFPath As String = "D:\Temp.txt"
    Open sFFPath For Output As #1
    Print #1, sOri
    Close #1
    Dim lOpen As Long
    lOpen = Shell("C:\WINDOWS\System32\notepad.exe " & sFFPath, vbNormalFocus)
End Sub

Previously, no matter how many times I ran the procedures, the notepad windows show different text string with the same file location. For example, I ran writeTxt "AA" and writeTxt "BB". There would be two notepad windows showing "AA" and "BB".

However, maybe Window or Office have some automatic update. Now when I ran writeTxt "AA" and writeTxt "BB". Two notepad windows will both display the later string "BB".

I would like to know how can I prevent earlier notepad window to update.

Thanks for your help and sorry for my poor english. Hope I explain myself well enough.


Solution

  • You can force windows to use "old" Notepad. Full instructions are here Open and Use Old Notepad - Shawn Brink

    In short - turn off App Execution Aliases for Notepad in Settings. Then run Notepad.exe - I believe your code should just work but I haven't personally tried this in code. Works fine for me "natively"