Opening Word Doc files (Desktop Only)

linum

Active Member
Licensed User
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:

B4X:
DocFile = Chr(34) & AppPath & "\Test.doc" & Chr(34)
Shell(AppPath & "\D2R.exe", DocFile)

Or, if you use the Threading Library:

B4X:
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:

B4X:
DocFile = Chr(34) & AppPath & "\TempPDF-SaveAsPDF.rtf" & Chr(34)
Shell(AppPath & "\D2R.exe", DocFile)

Or, if you use the Threading Library:

B4X:
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:
 

Attachments

  • D2R_ver4.zip
    20.4 KB · Views: 320
Last edited:

agraham

Expert
Licensed User
Longtime User
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.
 

derez

Expert
Licensed User
Longtime User
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...)
 

derez

Expert
Licensed User
Longtime User
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.
 

linum

Active Member
Licensed User
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:

B4X:
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...
 

linum

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

I'll keep looking into this...
 

linum

Active Member
Licensed User
Fixed Word version difference bug. See my first post under "Version 2" for details.
 

agraham

Expert
Licensed User
Longtime User
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

B4X:
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
 
Top