Solved

Open/Save/Close .rtf in Wordpad Programatically

Posted on 2006-11-03
5
2,101 Views
Last Modified: 2013-12-25
Hi,

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.
 (S:\Triumph\T0222\...\T0222-E00000346.doc)"

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.

Thanks,
DT
0
Comment
Question by:dtruong98
  • 2
  • 2
5 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 17869434
Hi dtruong98,

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

Cheers!
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 17870323
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.Clear
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

End Sub



Sub mySleep(ByVal deciSeconds As Long)
    For r = 1 To deciSeconds: Sleep 100: DoEvents: Next
End Sub
0
 

Author Comment

by:dtruong98
ID: 17883001
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 ??

Thanks,
DT
0
 
LVL 17

Accepted Solution

by:
vb_elmar earned 500 total points
ID: 17883486
>>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
0
 

Author Comment

by:dtruong98
ID: 17884226
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.

DT
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question