Open/Save/Close .rtf in Wordpad Programatically


Is there a way to Open a Word Document (xxx.rtf) using WordPad and then resave within Wordpad programatically.
I can manually do this with out any problem.

I need this because we have some .rtf files which fail to open using word but with Wordpad they open fine.

Word fails to open and the error description i get is

"Word was unable to read this document.  It may be corrupt.
Try one or more of the following:
* Open and Repair the file.
* Open the file with the Text Recovery converter.

If i open this doc in Wordpad, resave and then open up the doc in MS-word mannualy, it works fne.

Any help will be greatly appreciated.

Who is Participating?
>>SendKeys "%(fs)" --- Alt F and S  ---Also can be used to save file

Sendkeys can send only   **one**   char. It must be

SendKeys "%f" '-send Alt F
SendKeys "s"    '-send S
Éric MoreauSenior .Net ConsultantCommented:
Hi dtruong98,

Wordpad does not support automation (like Word) so it cannot be done.

I wrote this sample for german wordpad. It uses
SendKeys "%f"
to open a file.

If you use the english wordpad you have
modify the SendKeys combination.

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Form_Load()
Visible = False
Shell "C:\Programs\WORDPAD.EXE", vbNormalFocus
mySleep 30 'wait three seconds

SendKeys "^o" 'open file = CTRL+o

Clipboard.SetText "c:\test.doc"     ' file name to open

SendKeys "^v"           'CTRL+v    inserting the file name from clipboard
SendKeys "%f"           'sends ALT+f      open file

mySleep 80                'wait 8 seconds
SendKeys "^s"            'CTRL+s      save file

mySleep 20 'wait 2 seconds

MsgBox "file saved"

End Sub

Sub mySleep(ByVal deciSeconds As Long)
    For r = 1 To deciSeconds: Sleep 100: DoEvents: Next
End Sub
dtruong98Author Commented:
Hi Elmar,

I am using the following code:

Shell "C:\Program Files\Windows NT\Accessories\WordPad.exe C:\temp\test.doc"
AppActivate "test.doc - WordPad"
SendKeys "^s"    ---Ctrl S ---Used to save file
SendKeys "%(fs)" --- Alt F and S  ---Also can be used to save file
SendKeys "%(fx)"  ---Alt F and X -  Close wordpad

I can succesfully open the "C:\temp\test.doc" file in wordpad, but unable to save and exit wordpad.

I tried putting the sleep commands but with no use.

Any ideas ??

dtruong98Author Commented:
The following code worked:

Shell "C:\Program Files\Windows NT\Accessories\WordPad.exe C:\temp\test.doc", vbMaximizedFocus
Sleep 5000 'Sleep 5 seconds
AppActivate "test.doc - WordPad"
SendKeys "^s" 'Save using Ctrl S
AppActivate "test.doc - WordPad"
AppActivate "test.doc - WordPad"
CloseWordPad (Done using an API call)

Thanks Elmar for your help.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.