How to save word doc in text file in any location

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kamlesh21630
    New Member
    • Nov 2013
    • 16

    How to save word doc in text file in any location

    Hi All,

    I wanted to save my word file into text format using macro in word 2007.

    Below snippet code for saving doc into text but we in below code we are giving path and i wanted to run this macro for any doc in any path.

    Code:
    Sub Macro4()
    '
    ' Macro4 Macro
    '
    '
        ChangeFileOpenDirectory "C:\Documents and Settings\User\Desktop\Reverend\"
        ActiveDocument.SaveAs FileName:="Chapter 1.txt", FileFormat:=wdFormatText, _
             LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
            :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
            SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
            False, Encoding:=65001, InsertLineBreaks:=False, AllowSubstitutions:= _
            False, LineEnding:=wdCRLF
    End Sub
  • kamlesh21630
    New Member
    • Nov 2013
    • 16

    #2
    Any way i just got.

    Code:
    Sub ChangeDocsToTxtOrRTFOrHTML()
    'with export to PDF in Word 2007
        Dim fs As Object
        Dim oFolder As Object
        Dim tFolder As Object
        Dim oFile As Object
        Dim strDocName As String
        Dim intPos As Integer
        Dim locFolder As String
        Dim fileType As String
        On Error Resume Next
        locFolder = InputBox("Enter the folder path to DOCs", "File Conversion", "C:\Documents and Settings\kamlesh.singh\Desktop\New Folder")
        Select Case Application.Version
            Case Is < 12
                Do
                    fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML", "File Conversion", "TXT"))
                Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML")
            Case Is >= 12
                Do
                    fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML or PDF(2007+ only)", "File Conversion", "TXT"))
                Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML" Or fileType = "PDF")
        End Select
        Application.ScreenUpdating = False
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set oFolder = fs.GetFolder(locFolder)
        Set tFolder = fs.CreateFolder(locFolder & "Converted")
        Set tFolder = fs.GetFolder(locFolder & "Converted")
        For Each oFile In oFolder.Files
            Dim d As Document
            Set d = Application.Documents.Open(oFile.Path)
            strDocName = ActiveDocument.Name
            intPos = InStrRev(strDocName, ".")
            strDocName = Left(strDocName, intPos - 1)
            ChangeFileOpenDirectory tFolder
            Select Case fileType
            Case Is = "TXT"
                strDocName = strDocName & ".txt"
                ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatText
            Case Is = "RTF"
                strDocName = strDocName & ".rtf"
                ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatRTF
            Case Is = "HTML"
                strDocName = strDocName & ".html"
                ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatFilteredHTML
            Case Is = "PDF"
                strDocName = strDocName & ".pdf"
    
                ' *** Word 2007 users - remove the apostrophe at the start of the next line ***
                'ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
                
            End Select
            d.Close
            ChangeFileOpenDirectory oFolder
        Next oFile
        Application.ScreenUpdating = True
    End Sub

    Comment

    Working...