Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-25-2017, 11:38 AM
gerison gerison is offline Save Document as Text File Windows 10 Save Document as Text File Office 2010 64bit
Novice
Save Document as Text File
 
Join Date: Nov 2017
Posts: 8
gerison is on a distinguished road
Default Save Document as Text File


Hello! I'm new here and to be frank, I have no clue how vba works. Nevertheless I need to solve a particular problem. I tested the script in https://www.msofficeforums.com/word-...rent-file.html to save the current .docx firl as .txt. It works quite right. Nevertheless the new file always is stored in the root documents folder. Not in the folder in which the to be converted file is saved. How can I solve this problem? I need the file to be in the same directory. In addtion: I would afterwards have to open the txt file in the standard application for .txt files. (maybe word is closing in the same moment). Can anyon provide a solution for that.
Reply With Quote
  #2  
Old 11-25-2017, 09:42 PM
gmayor's Avatar
gmayor gmayor is offline Save Document as Text File Windows 10 Save Document as Text File Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,103
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Use instead:
Code:
Sub saveastxt()
Dim RetVal As Object
Dim strName As String
    On Error Resume Next
    ActiveDocument.Save
    If ActiveDocument.Path = "" Then
        MsgBox "Document not saved!"
        GoTo lbl_Exit
    End If
    strName = Replace(ActiveDocument.FullName, ".docx", ".txt")
    ActiveDocument.SaveAs2 FileName:=strName, FileFormat:=wdFormatText
    ActiveDocument.Close 0
    RetVal = Shell("notepad.exe " & strName, 1)
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 11-26-2017, 10:01 AM
gerison gerison is offline Save Document as Text File Windows 10 Save Document as Text File Office 2010 64bit
Novice
Save Document as Text File
 
Join Date: Nov 2017
Posts: 8
gerison is on a distinguished road
Default

Thank you so much. Works like a charm. Just two things would top it all. 1)The docx file closes 2)The docx file is deleted. I will try to figure it out, but would appreciate help nevertheless. Thank you. gerald
Reply With Quote
  #4  
Old 11-26-2017, 11:09 AM
gmaxey gmaxey is offline Save Document as Text File Windows 7 32bit Save Document as Text File Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Do you mean the .txt file closes because the docx file already was closing:

Code:
Sub saveastxt()
Dim RetVal As Object
Dim strDelete As String
Dim strName As String
    On Error Resume Next
    ActiveDocument.Save
    If ActiveDocument.Path = "" Then
        MsgBox "Document not saved!"
        GoTo lbl_Exit
    End If
    strDelete = ActiveDocument.FullName
    strName = Replace(ActiveDocument.FullName, ".docx", ".txt")
    ActiveDocument.SaveAs2 FileName:=strName, FileFormat:=wdFormatText
    ActiveDocument.Close 0
    'RetVal = Shell("notepad.exe " & strName, 1) 'Don't open the saved .txt file
    Kill strDelete
lbl_Exit:
    Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 11-27-2017, 12:55 AM
gerison gerison is offline Save Document as Text File Windows 10 Save Document as Text File Office 2010 64bit
Novice
Save Document as Text File
 
Join Date: Nov 2017
Posts: 8
gerison is on a distinguished road
Default

Thank you so much. Works Great! Gerald
Reply With Quote
  #6  
Old 11-27-2017, 07:15 AM
gerison gerison is offline Save Document as Text File Windows 10 Save Document as Text File Office 2010 64bit
Novice
Save Document as Text File
 
Join Date: Nov 2017
Posts: 8
gerison is on a distinguished road
Default Save doc as text

Hi Again! I experience problems with *.doc Documents. The file is deleted, the txt file is not created. The application to open the new file renders the warning, that the file could not be found.... made several testruns and could reduce it to simply to the *.doc file. *.docx works fine. I even changed the makro where ther is *.docx to *.doc. but it didn't solve the issue.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Save Document as Text File Word VBA - Split Document By Headings - Save File Name As Heading Name jc491 Word VBA 7 01-21-2022 11:04 AM
Save Document as Text File Corrupted file? I'm unable to save changes to Word document MikeW Word 4 02-08-2017 09:41 AM
Vba code to save document as pdf using document property text and rename folder. staicumihai Word VBA 1 12-21-2015 07:39 AM
VBA save a NEW unsave document without prompting user for a file name KSP Word VBA 5 09-02-2014 01:42 PM
Save Document as Text File Word Macro: Save file as text with current file name jabberwocky12 Word VBA 2 10-22-2010 12:23 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:04 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft