0
votes

So what I am trying to do is take a word document, save it as a plaint text .txt with the line breaks enabled. I was told a macro would be an easy way to do this, and I've had some success. My biggest problem right now is that it is not saving the document as it's current file name. I looked around and people have said to use ActiveDocument.Name, but for some reason that's not working for me and I end up with the document saved as a txt file but literally named ActiveDocument.Name. This is my first attempt at anything VBA related, so it's probably some simple little syntax error that I can't see. Here's my current code:

Sub WordtoTxtwLB()
'
' WordtoTxtwLB Macro
'
'
    ActiveDocument.SaveAs2 FileName:= _
        "\\Path\Path\FILENAME.txt", FileFormat:= _
        wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, _
        WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
         SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
        False, Encoding:=1252, InsertLineBreaks:=True, AllowSubstitutions:=False, _
         LineEnding:=wdCRLF, CompatibilityMode:=0
End Sub

Note: I did change the path and file name in my code to generic ones (i.e. 'Path' and 'FILENAME') just to make things a little easier/clearer. SO if I had a document called cat.doc, I would want the macro to save it as a .txt file with line breaks and with the same file name. Any help/suggestions?

1

1 Answers

2
votes

The name FILENAME is within quotes, so that it'll be used as a literal. Separate it out of the quotes to be used as a variable. Also, if you use ActiveDocument.Name, it'll have the suffix as part of the string ("somefile.doc"); you'll have to remove the suffix.

   Sub WordtoTxtwLB()
    '
    ' WordtoTxtwLB Macro
    '
    '
        Dim fileName As String
        myFileName = "New_File"

        ActiveDocument.SaveAs2 FileName:= _
            "\\Path\Path\" & myFileName & ".txt", FileFormat:= _
            wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, _
            WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
             SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
            False, Encoding:=1252, InsertLineBreaks:=True, AllowSubstitutions:=False, _
             LineEnding:=wdCRLF, CompatibilityMode:=0
    End Sub