Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Code Samples & Tips
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Code Samples & Tips Share your recent discoveries and ideas with other users.

Opening Word Doc files (Desktop Only)

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-12-2009, 04:42 AM
linum's Avatar
Senior Member
 
Join Date: Sep 2008
Posts: 108
Default Opening Word Doc files and Save them as PDF (Desktop Only)

Hello friends. This is my small contribution to this great forum and it's my way of saying "thank you" for all the great help you have all given me.

FINALLY! You can now open Word Doc files and load them into your project (for Desktop only).

Well... the method I use basically uses Agraham's Pretty Printing Library to create a Richtextbox in your project, then it uses D2R.exe, a very small program I created (using Visual Basic 6), to literally convert the .doc file into a .rtf file. You can then open your .rtf file using the richtextbox.LoadFile(AppPath & "\file.rtf") command from the Pretty Printing Library.

I use a Shell command to invoke D2R.exe to convert the .doc file. Sample:

Code:
DocFile = Chr(34) & AppPath & "\Test.doc" & Chr(34)
Shell(AppPath & 
"\D2R.exe", DocFile)
Or, if you use the Threading Library:

Code:
DocFile = Chr(34) & AppPath & "\Test.doc" & Chr(34)
Process.Start(AppPath & 
"\D2R.exe", DocFile)
Do While Process.Running
  Sleep(
100)
Loop

REQUIREMENTS:
You must have Microsoft Word installed. You also need CutePDF Writer if you want to save your richtext to PDF (download here).








Version 4:
This version adds the ability to save your richtext files as PDF. It uses CutePDF Writer (download here) as the engine. You must first save your richtext file as "TempPDF-SaveAsPDF.rtf" then invoke D2R:

Code:
DocFile = Chr(34) & AppPath & "\TempPDF-SaveAsPDF.rtf" & Chr(34)
Shell(AppPath & 
"\D2R.exe", DocFile)
Or, if you use the Threading Library:

Code:
DocFile = Chr(34) & AppPath & "\TempPDF-SaveAsPDF.rtf" & Chr(34)
Process.Start(AppPath & 
"\D2R.exe", DocFile)
Do While Process.Running
  Sleep(
100)
Loop
The attachment includes an example on how to do this.

Version 3:
This version fixes the Winword.exe running process error.

Version 2:
This second version fixes the error were the app crashes if a different version of Word that wasn't Word 2003 was used. D2R now uses Late Binding so converting .doc files to .rtf will be a bit slower. I suggest you bump up the seconds in your app's timer to allow it the necesary time needed to do this. This version doesn't fix the Winword.exe error yet.

Version 1:
You must have Microsoft Word installed in order for this to work. Also, it will cause a funky error and it will open Microsoft Word if the process Winword.exe is actively running on your system (I'll fix this in the near future).


Here's an attachment containing the D2R.exe file and a sample sbp file that shows how this works. The sample is very self explanatory. Enjoy:
Attached Files
File Type: zip D2R_ver4.zip (20.4 KB, 38 views)

Last edited by linum : 03-16-2009 at 11:17 PM.
Reply With Quote
  #2 (permalink)  
Old 03-12-2009, 12:09 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

I'm afraid that I can't get this to work for me. I have Office XP Professional on my machines.

On my Vista Home Premium desktop D2R.exe freezes with a "D2R has stopped working" message. This happens even after modifying the properties of D2R,exe to run as an Administrator,

On my Compaq XP Home SP3 laptop I get an error that I can open with VS2005 debugger that tells me "Unhandled exception at 0x00000000 in D2R.exe : 0xC0000005: Access violation reading 0x00000000." That looks a bit like a null pointer problem

On my EEE PC with XP Home SP3, that doesn't have VS on it, I get a "D2R has encountered a problem and needs to close" message. The technical details in the optional error report to Microsoft are the same as on the laptop.

These errors happen whether WinWord is running or not when the program is started.
Reply With Quote
  #3 (permalink)  
Old 03-12-2009, 12:45 PM
derez's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Posts: 918
Awards Showcase
Beta Tester Competition Winner 
Total Awards: 2
Default

It runs on my IBM T60 Thinkpad, with word 2003.

Well done, hope you find why it doesn't run for Agraham (we all have to make sure that this man doesn't get angry...)
__________________
David Erez
Ramat Hasharon, Israel
Reply With Quote
  #4 (permalink)  
Old 03-12-2009, 12:57 PM
klaus's Avatar
Basic4ppc Expert
 
Join Date: Oct 2007
Location: Fully, Switzerland
Posts: 3,827
Awards Showcase
Forum Contributer Beta Tester Competition Winner 
Total Awards: 3
Default

Sorry, doesn't run for me either. "D2R has stopped working" message.
XP Media Center Edition, Word 2002

Best regards.

EDIT: Word files in the program's directory.
__________________
Klaus
Switzerland

Last edited by klaus : 03-12-2009 at 01:20 PM.
Reply With Quote
  #5 (permalink)  
Old 03-12-2009, 01:16 PM
derez's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Posts: 918
Awards Showcase
Beta Tester Competition Winner 
Total Awards: 2
Default

After playing with it some more - it opens only files that are placed at the same directory of the program, although RTF files are created elsewhere.
__________________
David Erez
Ramat Hasharon, Israel
Reply With Quote
  #6 (permalink)  
Old 03-12-2009, 01:56 PM
linum's Avatar
Senior Member
 
Join Date: Sep 2008
Posts: 108
Default

Good morning. Interesting. I don't have office XP only office 2003. Maybe that's why you get that error.

Here's the VB6 code:

Code:
Option Explicit
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim FileName
Dim CutName

Private 
Sub Form_Load()
    
    
If VBA.Command$ <> "" Then
        FileName = VBA.Command$
        CutName = VBA.Command$
        CutName = Replace(CutName, 
".doc"".rtf")
        Set oApp = New Word.Application
        Set oDoc = oApp.Documents.Open(FileName)
        oDoc.SaveAs FileName:=CutName, FileFormat:=wdFormatRTF
        oDoc.Close SaveChanges:=
False
        Set oDoc = Nothing
        oApp.Quit
        Set oApp = Nothing
        
End
        
'MsgBox VBA.Command$
    Else
        
End
    
End If
    
End Sub
I set a reference to Microsoft Word 10 or something like that. Maybe I need to rebuild it from a machine that is running Word XP. Is there something wrong with my code maybe? I'll mess around with it later today...
Reply With Quote
  #7 (permalink)  
Old 03-12-2009, 02:48 PM
linum's Avatar
Senior Member
 
Join Date: Sep 2008
Posts: 108
Default

Ok, I tried D2R on 3 separate machines. They are all running Office 2003 and D2R works perfectly.

I'll keep looking into this...
Reply With Quote
  #8 (permalink)  
Old 03-13-2009, 05:36 AM
linum's Avatar
Senior Member
 
Join Date: Sep 2008
Posts: 108
Default

Fixed Word version difference bug. See my first post under "Version 2" for details.
Reply With Quote
  #9 (permalink)  
Old 03-13-2009, 08:08 AM
klaus's Avatar
Basic4ppc Expert
 
Join Date: Oct 2007
Location: Fully, Switzerland
Posts: 3,827
Awards Showcase
Forum Contributer Beta Tester Competition Winner 
Total Awards: 3
Default

It works for me now !

Best regards.
__________________
Klaus
Switzerland
Reply With Quote
  #10 (permalink)  
Old 03-13-2009, 09:57 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Works fine for me now, even if a copy of Word is already running. Very neat! You can do away with the timer kludge by using the Process object from my Threading library

Code:
Sub Button1_Click
  OpenDialog1.Filter = 
"Doc Files|*.doc"
  OpenDialog1.File = AppPath & 
"\."
  
If OpenDialog1.Show <> cCancel Then
    DocFile = OpenDialog1.File
    Process.Start(AppPath & 
"\D2R.exe", DocFile)        
    WaitCursor(
True)
    
Do While Process.Running
      Sleep(
100)
      
Loop
    DocFile = StrReplace (DocFile, 
".doc"".rtf")   'changes extension name    
    rtb.Clear
    rtb.LoadFile(DocFile)
    
End If
End Sub
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems opening closing forms admben25 Questions (Windows Mobile) 3 11-03-2008 05:08 AM
Word wrap problem agraham Beta Versions 1 08-27-2008 11:02 AM
Opening a file argument? Hennell Questions (Windows Mobile) 5 03-10-2008 07:48 PM
Problems using rapi to send files to desktop sunnyboyj Questions (Windows Mobile) 4 01-06-2008 06:40 AM
b4ppc crashes on opening serialport (cfserialclass.dll) sloopa Questions (Windows Mobile) 1 06-12-2007 09:26 AM


All times are GMT. The time now is 04:21 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0