Opening Word Doc files (Desktop Only)

Discussion in 'Code Samples & Tips' started by linum, Mar 12, 2009.

  1. linum

    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:

    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:

    Last edited: Mar 16, 2009
  2. agraham

    agraham Expert Licensed 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.
     
  3. derez

    derez Expert Licensed 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...)
     
  4. klaus

    klaus Expert Licensed User

    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.
     
    Last edited: Mar 12, 2009
  5. derez

    derez Expert Licensed 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.
     
  6. linum

    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:

    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...
     
  7. linum

    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...
     
  8. linum

    linum Active Member Licensed User

    Fixed Word version difference bug. See my first post under "Version 2" for details.
     
  9. klaus

    klaus Expert Licensed User

    It works for me now !

    Best regards.
     
  10. agraham

    agraham Expert Licensed 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

    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
     
  11. linum

    linum Active Member Licensed User

    Version 3 now ready...

    Fixed the Winword.exe running process error.
     
  12. linum

    linum Active Member Licensed User

    You can now save your richtext into PDF. See Version 4 details under main post.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice