Solved

Open/Save/Close .rtf in Wordpad Programatically

Posted on 2006-11-03
5
2,073 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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now