Saving file as TXT from Word

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Saving file as TXT from Word

Post by Robie »

Hi

Whenever I use the ActiveDocument.SaveAs2 command as below, it displays the conversion dialog (I am sure I did not get it before, i.e. it just saved the document). Can someone please shed some light on this as I would prefer to just save the document and get on with it without asking the user for conversion options?

BTW: The document being saved is opened as a blank document.
47.png

Code: Select all

    If bTestJobUpdates = True Then
        ' -------------------------------------- FOR TESTING ----------------------------------------------------
        If MsgBox("TEST output created - save?", vbYesNo) = vbYes Then
            ActiveDocument.SaveAs2 FileName:=sLocalFileName, FileFormat:=wdFormatText, LineEnding:=wdLFOnly
        End If
    Else
        ' -------------------------------------- FOR REAL -------------------------------------------------------
        If MsgBox("Batch output ready - update?", vbYesNo) = vbYes Then
            ActiveDocument.SaveAs2 FileName:=sSavedFilename, FileFormat:=wdFormatText, LineEnding:=wdLFOnly
        End If
    End If
Thanks.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Saving file as TXT from Word

Post by HansV »

You could turn off the confirm conversion dialog before saving the document:

Code: Select all

    Options.ConfirmConversions = False
or if you'd rather not turn it off altogether, you can save and restore the setting:

Code: Select all

    Dim blnConfirm As Boolean
    ' Save setting
    blnConfirm = Options.ConfirmConversions
    ' Turn confirm off
    Options.ConfirmConversions = False

    ' Code to save document goes here
    ...

    ' Restore setting
    Options.ConfirmConversions = blnConfirm
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Saving file as TXT from Word

Post by Robie »

HansV wrote:You could turn off the confirm conversion dialog before saving the document:

Code: Select all

    Options.ConfirmConversions = False
or if you'd rather not turn it off altogether, you can save and restore the setting:

Code: Select all

    Dim blnConfirm As Boolean
    ' Save setting
    blnConfirm = Options.ConfirmConversions
    ' Turn confirm off
    Options.ConfirmConversions = False

    ' Code to save document goes here
    ...

    ' Restore setting
    Options.ConfirmConversions = blnConfirm
Thanks Hans but no joy :sad: . It is still displaying the dialog. I even tried simply by opening a blank document and calling this function without all the checks so that it would just save the text documet but the result it exactly the same.
BTW: The ConfirmCoversions is already FALSE when we save the setting.

I moved the saving part in a separate function just to make sure it is kept simple.

Code: Select all

Function SaveDocumentAsTextFile(ByRef tDoc As Document)
    Dim blnConfirm As Boolean
    
    ' Save setting
    blnConfirm = Options.ConfirmConversions
    ' Turn confirm off
    Options.ConfirmConversions = False

    If MsgBox("TEST output created - save?", vbYesNo) = vbYes Then
        tDoc.SaveAs2 FileName:="C:\TextDocument.txt", FileFormat:=wdFormatText, LineEnding:=wdLFOnly
   End If
    ' ----------------------------------------------------------------------------------------------------------
    ' Restore setting
    Options.ConfirmConversions = blnConfirm
    ' Close the file without any changes (as it has been saved already)
    tDoc.Close SaveChanges:=wdDoNotSaveChanges
End Function

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Saving file as TXT from Word

Post by Robie »

Hi Hans

I found something called Application.DisplayAlerts. If I switch that off then it *doesn't* display the dialog.

I don't know if it a good idea or not to switch of alerts before saving the document as text. Would it also switch of error messages (e.g. if the document could not be saved)?

Regards
Robie

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Saving file as TXT from Word

Post by HansV »

Sorry about that, I see now that the Confirm Conversions setting has nothing to do with this.

Does it help if you insert the following above the lines that save the document:

Application.DisplayAlerts = False

and below them

Application.DisplayAlerts = True
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Saving file as TXT from Word

Post by Robie »

HansV wrote:Sorry about that, I see now that the Confirm Conversions setting has nothing to do with this.

Does it help if you insert the following above the lines that save the document:

Application.DisplayAlerts = False

and below them

Application.DisplayAlerts = True
Thanks Hans. That works. Only question is would it display an error message if say the file could not be saved?

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Saving file as TXT from Word

Post by HansV »

Setting DisplayAlerts to False does not suppress error messages, it only chooses the default option in "standard" messages.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Saving file as TXT from Word

Post by Robie »

HansV wrote:Setting DisplayAlerts to False does not suppress error messages, it only chooses the default option in "standard" messages.
Thanks.

Now, all working smoothly. :clapping: