Solved

Open/Save/Close .rtf in Wordpad Programatically

Posted on 2006-11-03
5
2,039 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
Comment Utility
Hi dtruong98,

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

Cheers!
0
 
LVL 17

Expert Comment

by:vb_elmar
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 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

5 Experts available now in Live!

Get 1:1 Help Now