Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleTranslate a Word file into a text file
KeywordsOffice, Word, translate, text file
CategoriesOffice
 
Create a Word server object. Make it open the file and save it as a text file.

Note that you need to use the Project menu's References command to add a reference to Microsoft Word 8.0 Object Library to make this work.

 
Private Sub Command1_Click()
Dim dir_name As String
Dim file_name As String
Dim word_server As Word.Application

    Screen.MousePointer = vbHourglass
    DoEvents

    file_name = txtFile.Text
    If InStr(file_name, ".") > 0 Then file_name = _
        Left$(file_name, InStr(file_name, ".") - 1)

    dir_name = txtDirectory.Text
    If Right$(dir_name, 1) <> "\" Then dir_name = dir_name _
        & "\"

    ' Create the Word server object.
    On Error GoTo OpenError
    Set word_server = New Word.Application
    On Error GoTo 0

    ' Open the file.
    word_server.ChangeFileOpenDirectory dir_name
    word_server.Documents.Open _
        FileName:=file_name & ".doc", _
        ConfirmConversions:=False, _
        ReadOnly:=False, _
        AddToRecentFiles:=False, _
        PasswordDocument:="", _
        PasswordTemplate:="", _
        Revert:=False, _
        WritePasswordDocument:="", _
        WritePasswordTemplate:="", _
        Format:=wdOpenFormatAuto

    ' Save the file.
    word_server.ActiveDocument.SaveAs _
        FileName:=file_name & ".txt", _
        FileFormat:=wdFormatText, _
        LockComments:=False, _
        Password:="", _
        AddToRecentFiles:=True, _
        WritePassword:="", _
        ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, _
        SaveNativePictureFormat:=False, _
        SaveFormsData:=False, _
        SaveAsAOCELetter:=False

    word_server.Quit
    Set word_server = Nothing

    Screen.MousePointer = vbDefault
    MsgBox "Done"
    Exit Sub
    
OpenError:
    Screen.MousePointer = vbDefault
    MsgBox "Error" & Str$(Error.Number) & _
        " opening Word." & vbCrLf & _
        Error.Description
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated