ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   CreateObject("Notepad") ? (https://www.excelbanter.com/excel-programming/345074-createobject-notepad.html)

achidsey

CreateObject("Notepad") ?
 

As part of my code, I want to change the encoding of some .txt files from
Unicode to ANSI. Is there a direct way to do this in VBA.

If not, I want add code to create a Notepad session and then use the
SendKeys method to do this. How do I create a Notepad session in VBA.

I know I can start a program called Monarch through VBA using:

Set MonObj = CreateObject("Monarch32")

What's the equivalent for Notepad?

Thanks in advance,

Alan

--
achidsey

K Dales[_2_]

CreateObject("Notepad") ?
 
I am not sure that Notepad supports automation. But a better way to do what
you want would be to directly open a text file and use the Input# and Print#
statements to read and write directly to the file. By bypassing notepad you
avoid some of the possible problems using SendKeys, as when some system event
(or user event) interferes with the keystrokes and starts typing your
Sendkeys statements somewhere where they aren't supposed to go.

Basic structure would be as follows:

Dim InputFile as String, OutputFile as String, TextChar as String
InputFile = "C:\TEST.txt" ' replace with actual path to input file
OutputFile = "C:\TEST OUTPUT.txt" ' replace with output path
Open InputFile For Input As #1 ' Open file.
Open OutputFile for Output as #2 ' Open the output file: will either create
it or overwrite existing file
Do While Not EOF(1) ' Loop until end of file.
TextChar = Input(1, #1) ' Get one character.
' Perform any desired conversion here
Print #2, TextChar; ' Wirte to output file
Loop
Close #1 ' Close files
Close #2


--
- K Dales


"achidsey" wrote:


As part of my code, I want to change the encoding of some .txt files from
Unicode to ANSI. Is there a direct way to do this in VBA.

If not, I want add code to create a Notepad session and then use the
SendKeys method to do this. How do I create a Notepad session in VBA.

I know I can start a program called Monarch through VBA using:

Set MonObj = CreateObject("Monarch32")

What's the equivalent for Notepad?

Thanks in advance,

Alan

--
achidsey


achidsey

CreateObject("Notepad") ?
 
K Dales

Thanks for the information.

Alan
--
achidsey


"K Dales" wrote:

I am not sure that Notepad supports automation. But a better way to do what
you want would be to directly open a text file and use the Input# and Print#
statements to read and write directly to the file. By bypassing notepad you
avoid some of the possible problems using SendKeys, as when some system event
(or user event) interferes with the keystrokes and starts typing your
Sendkeys statements somewhere where they aren't supposed to go.

Basic structure would be as follows:

Dim InputFile as String, OutputFile as String, TextChar as String
InputFile = "C:\TEST.txt" ' replace with actual path to input file
OutputFile = "C:\TEST OUTPUT.txt" ' replace with output path
Open InputFile For Input As #1 ' Open file.
Open OutputFile for Output as #2 ' Open the output file: will either create
it or overwrite existing file
Do While Not EOF(1) ' Loop until end of file.
TextChar = Input(1, #1) ' Get one character.
' Perform any desired conversion here
Print #2, TextChar; ' Wirte to output file
Loop
Close #1 ' Close files
Close #2


--
- K Dales


"achidsey" wrote:


As part of my code, I want to change the encoding of some .txt files from
Unicode to ANSI. Is there a direct way to do this in VBA.

If not, I want add code to create a Notepad session and then use the
SendKeys method to do this. How do I create a Notepad session in VBA.

I know I can start a program called Monarch through VBA using:

Set MonObj = CreateObject("Monarch32")

What's the equivalent for Notepad?

Thanks in advance,

Alan

--
achidsey



All times are GMT +1. The time now is 05:47 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com