Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2005

Tip: Looking for answers? Try searching our database.

Macro to save as txt in same location as doc file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve S - 13 Jun 2005 18:27 GMT
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
Jay Freedman - 13 Jun 2005 19:34 GMT
> 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
[quoted text clipped - 4 lines]
> 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.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

Steve S - 13 Jun 2005 20:21 GMT
Thanks your Grrrrreat!

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

Thanks again

>> 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
[quoted text clipped - 63 lines]
>
> See http://www.gmayor.com/installing_macro.htm if you need instructions.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.