Microsoft Office Forums Word Macro: Save file as text with current file name

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-05-2010, 11:21 PM
jabberwocky12 jabberwocky12 is offline Word Macro: Save file as text with current file name Windows 7 Word Macro: Save file as text with current file name Office 2007
Novice
Word Macro: Save file as text with current file name
 
Join Date: Sep 2010
Posts: 2
jabberwocky12 is on a distinguished road
Default Word Macro: Save file as text with current file name

(My knowledge of VB is close to non-existent - I just want to record and run a macro)

Using MS-Word 2007.

I have about 100 files, named "1.doc", "2.doc", etc.
I need to edit them, and then convert them all to text files, using "File | Save As."

Using "1.doc" as my example, I record a macro, and go through the entire "File | Save As" process - merely changing the file type to text, and then the formatting, and saving it as "1.txt"

It works fine with this file, but, when I run the macro on "2.doc", it wants to save it ALSO as "1.txt", because the macro actually records the file name (not the key strokes). (See the macro below).

So, I need it to get the current file name, and save the file as CurrentFileName.txt.

I understand (I think) that the process I'm looking for is:

1. Define a CurrentFileName variable as a type string
2. Read the current file name into that CurrentFileName variable
3. Determine the length of CurrentFileName
4. Remove the last three chars from CurrentFileName (assuming that they are all .doc)
5. Add the chars "txt" to CurrentFileName
6. Then, in the macro below, instead of
ActiveDocument.SaveAs FileName:="1.txt"
write
ActiveDocument.SaveAs FileName:=CurrentFileName


Macro:
~~~~~~
----

Sub FileSaveClose()
'
' FileSaveClose Macro
'
'
ActiveDocument.SaveAs FileName:="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
ActiveWindow.Close
End Sub

-----

Please help with steps 1-5 above. (In your explanation, please treat me like a 5-year old - I promise I won't be offended :-)

Thanks in advance.
Reply With Quote
  #2  
Old 09-06-2010, 01:27 AM
jabberwocky12 jabberwocky12 is offline Word Macro: Save file as text with current file name Windows 7 Word Macro: Save file as text with current file name Office 2007
Novice
Word Macro: Save file as text with current file name
 
Join Date: Sep 2010
Posts: 2
jabberwocky12 is on a distinguished road
Default Quick Update

After hunting through various snippets of code, I have done this:

Dim sNewFileName As String

sNewFileName = ActiveDocument.FullName
sNewFileName = sNewFileName + ".txt"

Then, where the file is saved, instead of:
ActiveDocument.SaveAs FileName:="1.txt"
I have:
ActiveDocument.SaveAs FileName:=sNewFileName

These lines get the current file name and add the extension ."txt" to it.

At this stage, it looks messy, cos the file is then saved as "1.doc.txt" but I have a small freeware package that does batch file renaming, so it's easy to strip that ".doc" from it.

So, the whole macro (including convertin it to text and closing the window), now looks like this:

---
Sub SaveAsText()
'
' SaveAsText Macro
'
'

Dim sNewFileName As String

sNewFileName = ActiveDocument.FullName
sNewFileName = sNewFileName + ".txt"

Selection.Rows.ConvertToText Separator:=wdSeparateByTabs, NestedTables:= _
True
ActiveDocument.SaveAs FileName:=sNewFileName, _
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
ActiveWindow.Close

-----
Reply With Quote
  #3  
Old 10-22-2010, 12:23 PM
Kimberly Kimberly is offline Word Macro: Save file as text with current file name Windows 7 Word Macro: Save file as text with current file name Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Posts: 517
Kimberly is on a distinguished road
Default

How about

Code:
 
Sub saveastxt()
 ActiveDocument.SaveAs2 FileName:=Replace(ActiveDocument.Name, "docx", "txt")
 FileFormat = wdFormatText
End Sub
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro will not save to normal.dot file when exiting bobbraun Word 1 09-28-2010 06:26 AM
Word Macro: Save file as text with current file name Save/Send to file Dawn1231a Outlook 1 08-05-2010 01:10 PM
linking a word file and an excell file on a macbook pro Fleur Office 0 07-14-2010 08:19 PM
Runtime error 5487 - Word cannot complete the save to to file permission error franferns Word 0 11-25-2009 05:35 AM
Word Macro: Save file as text with current file name File conversion Warning Box On Save ding dong Word 9 04-12-2009 05:46 AM


All times are GMT -7. The time now is 01:32 AM.


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