How to disable screen refresh for another application (notepad)

I wrote a macro in vba that opens a text file using notepad, selects all txt and copies it to Excel. I have to process about 100 files daily this way, and I want to get rid of the blinking images that I'm watching. The code works, but the problem is that the command

Application.Screenupdating = False

Does not work with notebook. I can use only normal focus, otherwise the code does not work. How can I execute the code below without noticing that the notepad file is open and processed?

My code is:

Sub GetTextFile()
Application.ScreenUpdating = False
Dim MyPath As String
Dim MyFile As String

MyPath = "C:\Users\bgyona02\Desktop\OLAttachments\"

MyFile = Dir(MyPath & "*.txt", vbNormal)    

Do While Len(MyFile) > 0
  MyFile = Dir
Loop

Debug.Print GetTextFileContent(" C:\Users\bgyona02\Desktop\OLAttachments\" & MyFile)
    'MyFile = Shell("C:\WINDOWS\notepad.exe` C:\Users\bgyona02\Desktop\OLAttachments\" & MyFile, vbNormalFocus)
    'SendKeys "^a", True  '^A selects everything already in the pdf file.
    'SendKeys "^c", True
    'SendKeys "%fx", True
End Sub

I could not find any working solution.

+4
source share
2 answers

, Notepad, vbHide vbNormalFocus Shell:

Dim strCmd = "C:\WINDOWS\notepad.exe C:\Users\bgyona02\Desktop\OLAttachments\" & LatestFile
MyFile = Shell(strCmd, vbHide)

, SendKeys ....

, , , FileSystemObject Notepad.exe?

Option Explicit

Const FOR_READING = 1

Sub LoadTextFile()
    Dim varTxtContent As Variant
    Dim intLine As Integer

    'Debug.Print GetTextFileContent("D:\temp.txt")

    varTxtContent = Split(GetTextFileContent("D:\temp.txt"), vbCr, -1, vbBinaryCompare)
    For intLine = 0 To UBound(varTxtContent) - 1
        ThisWorkbook.Worksheets("Sheet1").Range("B" & intLine + 1).Value = varTxtContent(intLine)
    Next intLine


End Sub

Function GetTextFileContent(strPath As String) As String
    Dim strContent As String
    Dim objFso As Object
    Dim objFile As Object
    Dim objStream As Object

    strContent = ""
    On Error GoTo CleanUp:

    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFso.GetFile(strPath)
    Set objStream = objFile.OpenAsTextStream(FOR_READING, 0)

    With objStream
        strContent = .ReadAll
        .Close
    End With

CleanUp:
    Set objStream = Nothing
    Set objFile = Nothing
    Set objFso = Nothing
    GetTextFileContent = strContent

End Function

, . :

â, î or ô
foo
bar foo
baz bar foo

, Application.ScreenUpdating Excel, , Windows. , - , , - Windows API. FileSystemObject , .

.

+3

- , . , , . 1 , . , :

Option Explicit

Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

Public Const GW_HWNDNEXT As Long = 2
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwprocessid As Long) As Long

Function ProcIDFromWnd(ByVal hwnd As Long) As Long
   Dim idProc As Long

   ' Get PID for this HWnd
   GetWindowThreadProcessId hwnd, idProc
   ProcIDFromWnd = idProc
End Function

Function GetWinHandle(hInstance As Long) As Long
   Dim tempHwnd As Long

   ' Grab the first window handle that Windows finds:
   tempHwnd = FindWindow(vbNullString, vbNullString)

   ' Loop until you find a match or there are no more window handles:
   Do Until tempHwnd = 0
      ' Check if no parent for this window
      If GetParent(tempHwnd) = 0 Then
         ' Check for PID match
         If hInstance = ProcIDFromWnd(tempHwnd) Then
            ' Return found handle
            GetWinHandle = tempHwnd
            ' Exit search loop
            Exit Do
         End If
      End If

      ' Get the next window handle
      tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
   Loop
End Function

Sub MinimizeNotepad()
    Dim retval As Long, np_retval As Long
    np_retval = Shell("C:\notepad.exe", vbNormalFocus)
    retval = MoveWindow(GetWinHandle(np_retval), 1, 1, 1, 1, 1) ' Application.hwnd
End Sub
0

Source: https://habr.com/ru/post/1663310/


All Articles