How to retain focus on Notepad when hidden

Dude

Board Regular
Joined
Jan 12, 2006
Messages
80
I have some code that opens notepad and adds some text to some html files then saves and closes them.
But, I would like for this to run in the background/hidden and can't get it to address the notepad instance unles it is forground/activewindow.
I have this problem everytime I use "SendKeys" is there a way to SendKeys to hidden/minimized apps ??

Code:
Sub AddHTML()

    Dim MyText As String
    Dim Filename As String
    Dim i As Integer
    Dim NtPad As Variant
    
For i = 6 To 18
    '------------------------------------------
    MyText = "<meta http-equiv=refresh content=""15"">"
    Filename = "M:\Hpublic\WorkStatus\WorkcenterStatus" & i & ".htm"
    '-------------------------------------------
    NtPad = Shell("C:\Windows\NOTEPAD.EXE " & Filename, 1)
With NtPad
    SendKeys MyText, True
    SendKeys "{ENTER}", True
    SendKeys "%FS", True   ' File-Save
    Application.Wait Now + TimeValue("00:00:01")
    SendKeys "%{F4}", True
End With
Next i

End Sub
I am intending to use this as a small part of a larger task and schedule it to run hourly and wouldn't want it to pop-up unexpectedly. Also, I find things run much slower if they have to run while displayed, this would hopefuly speed things up as well.
I have been searching high and low for good information on Notepad automation but am finding little if any that pertains.

Thanks
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Dude

Why are you using Notepad and SendKeys for this?

Can't you just use VBA file input/output methods?

SendKeys is notoriously flaky and Notepad is just one of many applications that you can use to display/open a text file.

By the way With NtPad doesn't really mean/do anything.

All the Shell function does is return the task ID of the application, not an object reference to it.
 
Upvote 0
This will save to a TXT file or the other to a WORD DOC in the background, if you comment out the MsgBox's.


Sub myTSave()
'Standard Module code, like: Module1!
Dim myFolder As String
'Save Range as Text File.

ActiveSheet.Activate

'Ask user to select range for text file.
Set myRange = Application.InputBox(prompt:="Please select a range!", _
Title:="Text File Range!", Type:=8)

'Ask user for folder to save text file to.
myFolder = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")

'Save selected data as text file in users selected folder.
ActiveWorkbook.SaveAs Filename:=myFolder, FileFormat:=xlText, CreateBackup:=False

'Indicate save action.
MsgBox "Text File: " & myFolder & "Saved!"

'Go to top of sheet.
Range("A1").Select
End Sub


Sub myToWordFile()
'Standard Module code, like: Module1!
Dim myFolder As String, myRange As Range
Dim dataRng As Range
'Save Range as Text File.

ActiveSheet.Activate

'Ask user to select range for text file.
Set myRange = Application.InputBox(prompt:="Please select a range!", _
Title:="Text File Range!", Type:=8)

Set dataRng = ActiveSheet.Range(myRange.Address)

'Ask user for folder to save text file to.
myFolder = Application.GetSaveAsFilename(fileFilter:="Word Document (*.doc), *.doc")

'Save selected data as text file in users selected folder.
dataRng.SaveAs Filename:=myFolder, FileFormat:=xlCurrentPlatformText, CreateBackup:=False

'Indicate save action.
MsgBox "Text File: " & myFolder & "Saved!"

'Go to top of sheet.
Range("A1").Select
End Sub
 
Upvote 0
From what I can find Output writes a new file and append only adds to the end. Either way I just want to add one line and I have tested it and it works even if I add it to the top/as first line in the file. How can I append to the beginning of a file.
 
Upvote 0
Joe
Thanks for the reply but I am just trying to add one line of text to the top of an existing text file. Not trying to write a new file.
The text I eant to write doesn't exist in a range, it is just a small html tag.
 
Upvote 0
Dude

Yes it creates a new file, but I think that's what you actually need to do.

You can easily read in from an existing file and output to a new file with any required changes.

Then delete the existing file and rename the new file.
 
Upvote 0
Sub OpenTextFile()
'Standard Module Code, Like: Module1.
'Will append file if needed!

'ForReading 1 Open a file for reading only. You can't write to this file.
'ForAppending 8 Open a file and write to the end of the file.

'TristateUseDefault –2 Opens the file using the system default.
'TristateTrue –1 Opens the file as Unicode.
'TristateFalse 0 Opens the file as ASCII.


Const ForReading = 1, ForWriting = 2, ForAppending = 3
'Dim fs, f
Dim myFile As String
Dim NPOPFile As String
Dim myText As String
Dim MessageT, TitleT, DefaultT
Dim Msg, Style, Title, Help, Ctxt, Response

Msg = "You will append your new text to any in the file you select if you continue!" _
& vbCr & vbCr & "Do you want to continue?" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "Caution!" ' Define title.
Help = "DEMO.HLP" ' Define Help file.
Ctxt = 1000 ' Define topic context.
' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
GoTo myContinue
Else ' User chose No.
GoTo myEnd
End If

'Get text to write to file!
myContinue:
MessageT = "Enter your text here:" ' Set prompt.
TitleT = "Append Text File with Data!" ' Set title.
DefaultT = "Add Data Here!" ' Set default.
' Display message, title, and default value.
myText = InputBox(MessageT, TitleT, DefaultT)

On Error GoTo Err_OpenTextFile
'Use this Text file!
fileToOpen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
End If

myFile = fileToOpen
NPOFile = "NotePad.exe " & myFile

'Work with file [Append Text to File].
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(myFile, 8, TristateUseDefault)
f.Write Chr(9) & myText
f.Close

'Open NotePad with a data file!
ActiveSheet.Select
Call Shell(NPOFile, 1)

Exit_OpenTextFile:
Exit Sub

Err_OpenTextFile:
MsgBox Err.Description
Resume Exit_OpenTextFile
myEnd:
End Sub
 
Upvote 0
Norie,
Can the Input text be held in a variable or does it have to be written to a range? If variable How do you incorporate the new text into the variable?

If I Open file for Output as #1
how do I add text to it without harming the existing text? And how do I define where in the file the new text should go?

Sorry but I am just not seeing it
 
Upvote 0
Norie,

Here is what I have so far.
Code:
Sub InputTry()

Dim MyText As String
Dim AllText As String
Dim FileName As String
Dim r As Integer
Dim Top As Integer
Dim Btm As Integer

FileName = "M:\Hpublic\WorkStatus\WrkStatus.htm"
MyText = "<meta http-equiv=refresh content=""15"">"
Open "M:\Hpublic\WorkStatus\WorkcenterStatus6.htm" For Input As #1
    r = 0
Do Until EOF(1)
    Line Input #1, AllText
    Range("A4").Offset(r, 0) = AllText
    r = r + 1
Loop
Close #1
Top = ActiveSheet.Range("A3").Row
Btm = ActiveSheet.Range("A65536").End(xlUp).Row
Range("A" & Top).Formula = MyText

Open FileName For Output As #1
    For r = Top To Btm
        AllText = Range("A" & r)
        Write #1, AllText
    Next r
Close #1

End Sub

It seems close, but it is putting quotation marks around each line of text.
Needless to say this doesn't work well when viewed with a browser.

Any thoughts?
 
Upvote 0
Norie,

I changed the line from "Write #1" to "Print #1" and what do you know ?

It works :LOL: :p :LOL: :p :biggrin: :devilish:

Thanks for being persistent and making me just do it (Nike).

Thanks Joe
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,573
Members
449,089
Latest member
Motoracer88

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