Macro to save as txt in same location as doc file

S

Steve S

I need to make a macro for my boss, that save an existing word DOC file to
the same location as the original, but as a TXT, but if possible I need to
change the extension to .ASM

Could someone help me out, as I've never done this before.

Thanks
Steve
 
J

Jay Freedman

Steve said:
I need to make a macro for my boss, that save an existing word DOC
file to the same location as the original, but as a TXT, but if
possible I need to change the extension to .ASM

Could someone help me out, as I've never done this before.

Thanks
Steve

Hi Steve,

The main objective, saving a text file with a .ASM extension, can be done
with one statement of VBA code. There are some other things to take care of,
though.

One detail is to make sure that any changes in the document get saved in the
Word document format before trying to save as text.

In the sample below, I've assumed that the document's current extension is
..doc, and just replaced the last four characters with .asm. The original
extension doesn't have to be .doc, or even four characters, in which case
the macro will misname the file. If that's a problem, we can rework the
macro to take more care.

The actual work gets done in the three-line statement that starts with
".SaveAs". I chose to use the Unicode text file format, but you could use
wdFormatText or wdFormatTextLineBreaks or one of the others (see the VBA
help topic on the SaveAs method).

Finally, the SaveAs leaves the text version visible in the Word window, so
the macro re-opens the original document and closes the text one.

Public Sub SaveAsASM()
' save a copy of the current document
' as a text file with an ASM extension

Dim OldFilePathAndName As String
Dim NewFilePathAndName As String
Dim myDoc As Document
Set myDoc = ActiveDocument

With myDoc
' first make sure any changes in the
' document have been saved (in DOC form)
If Not .Saved Then .Save

OldFilePathAndName = .FullName
' change the extension -- assume it was .doc
NewFilePathAndName = Left$(OldFilePathAndName, _
Len(OldFilePathAndName) - 4) _
& ".asm"

' save the text version
.SaveAs FileName:=NewFilePathAndName, _
FileFormat:=wdFormatUnicodeText, _
AddToRecentfiles:=False
End With

' redisplay original document
Documents.Open FileName:=OldFilePathAndName
Documents(NewFilePathAndName).Close _
SaveChanges:=wdDoNotSaveChanges
End Sub

See http://www.gmayor.com/installing_macro.htm if you need instructions.
 
S

Steve S

Thanks your Grrrrreat!

I'll send it to him and see if it does the trick, looks good to me.

Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top