Office & Productivity Software forum

General discussion

Word "Save As" Macro w/Current File Name

by chesbaycruiser / February 16, 2009 11:28 PM PST

Greetings. I know only enough about macros & programming to be dangerous, so I am hoping this is a simple, syntax question. I am trying to create a macro in MS Word 2003 (via the recorder) that simply saves the file in a new format (.rtf) in a new location.

My question is, in the code below, what is the proper syntax to save the file using the current filename, instead of the static filename "2244328-42569.rtf" (the file used to record the macro)?


' Save_As_RTF Macro
' Macro recorded 2/17/2009 by TCB
ActiveDocument.SaveAs FileName:="2244328-42569.rtf", FileFormat:= _
wdFormatRTF, LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
End Sub

Discussion is locked
You are posting a reply to: Word "Save As" Macro w/Current File Name
The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our CNET Forums policies for details. All submitted content is subject to our Terms of Use.
Track this discussion and email me when there are updates

If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.

You are reporting the following post: Word "Save As" Macro w/Current File Name
This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Sorry, there was a problem flagging this post. Please try again now or at a later time.
If you believe this post is offensive or violates the CNET Forums' Usage policies, you can report it below (this will not automatically remove the post). Once reported, our moderators will be notified and the post will be reviewed.
Collapse -
Current and static file names?
by MarkFlax Forum moderator / February 17, 2009 3:47 AM PST

I'm no expert with Word Macros, but there are things you say in your post that I am not clear about.

1] Where does that "2244328-42569" come from?

2] If you use Save as, then you would normally use File > Save as, retain the original file name, but change the 'Save as type' to Rich Text Format.

3] So, using the recorder, it will record your steps above, ie File > Save as, (change file type), click Save. The file name should not have altered. Then stop the recorder and view the macro.

Is that what happens when you try it that way?


Collapse -
Current (Static) vs Dynamic File Names
by chesbaycruiser / February 17, 2009 4:20 AM PST


The process you describe is exactly what I did. The problem is that I have a lot of files that I would like to run this macro on, so I need the filename to be "dynamic"....that is to save each file under it's own name with the new (.rtf) extension.

When I used the Macro Recorder it retained the filename from the original document(22443238-42569.doc). Now every time I run the macro, it saves the file with that same (static) filename.

I would think that all I need to do is replace "2244328-42569.rtf" with the proper syntax in the instructions below to solve the problem....I just can't find what that might be.

ActiveDocument.SaveAs FileName:="2244328-42569.rtf",



Collapse -
It's not one instruction.
by Kees Bakker / February 17, 2009 6:50 AM PST

The Word specific part is ActiveDocument.Name or ActiveDocument.Fullname to determine the name of the active document.
You can easily see what they are by inserting for example a line with Msgbox (ActiveDocument.Fullname) in your macro.

Now all you've got to do: replace the .doc by .rtf and you've got the new filename. That's not a Word specific method. That's just general VBA string handling:
- len function to determing the length
- subtract 3
- left function to take the left part
- concatenate with "rtf"
I'll leave that to you.


Collapse -
Same Problem
by scottieowen / June 25, 2010 5:44 AM PDT

Just curious if you ever found a solution to you problem with the macro for save as. I am having the same issue and have not found any documents to solve it yet. Any help would be appreciated.


Collapse -
Re: macro
by Kees Bakker / June 25, 2010 5:51 AM PDT
In reply to: Same Problem

Did you do what I suggested in my post of Feb 17 (the post right above yours)? I still think it tells exactly what you should do.


Collapse -
I did see it
by scottieowen / June 25, 2010 6:20 AM PDT
In reply to: Re: macro

Yes I looked at it, but I am novice at writing scripts and macros. I have done some work with access and filemaker programs, but have just started using macros with excel and word (within the last two days).

Like the person who origionated this post, I also am just looking to replace the recorded macro's file name with the current document filename that is open.

I have found a way to do it with selection.text, but I have to highlight the text that I want to use as the file name. Below is a copy of my current macro.

Sub ConvertToText()
' ConvertToText Macro
ChangeFileOpenDirectory _
"C:\Documents and Settings\Scott\My Documents\PVFPD Fire Prevention\Word converted text files\St 43\"
ActiveDocument.SaveAs FileName:= _
"10544 Spenceville Rd-Galante Orthodontic PP.txt", FileFormat:= _
wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, Encoding:=20127, InsertLineBreaks:=False, AllowSubstitutions:= _
False, LineEnding:=wdCRLF
End Sub

Collapse -
I think you missed someting
by MarkFlax Forum moderator / June 25, 2010 6:36 AM PDT
In reply to: I did see it

I'm no expert on Word macros, but Kees pointed out that the macro should be looking for ActiveDocument.Name or ActiveDocument.Fullname

In your macro you just have ActiveDocument

Find the full name of the document, then save it with a different file extension.


Collapse -
Re: macro
by Kees Bakker / June 25, 2010 6:42 AM PDT
In reply to: I did see it

So now you save it with the name:
10544 Spenceville Rd-Galante Orthodontic PP.txt
and the format
wdFormatText (which I assume means text).

So you would have to program the macro
(1) to construct another filename (from the current one, by replacing the extension) and use that
and change it
(2) to use the rtf-format in stead of the text format

That's rather basic VBA-programming.


Collapse -
Changing the filename from one extension to another.
by Lynne.Patterson / September 5, 2012 6:10 AM PDT
In reply to: Re: macro

pdfFileName = ActiveDocument.FullName ' get the current file name
lengthFileName = Len(pdfFileName) ' get the length of the current file name (includes the path)
pdfFileName = Left(pdfFileName, lengthFileName - 3) + "pdf" ' changes the extension (shave off three characters and add three new ones.)

ActiveDocument.ExportAsFixedFormat OutputFileName:=pdfFileName, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False

Collapse -
Word, macros, Add .docx as file extension
by lowlevelpass / December 27, 2013 8:58 AM PST


I'm playing around with the following code to auto save as the current document title.

Problem is, the final output file is in word format but doesn't have any file extension on the right end.

Please advise.

Sub Macro1()
' Macro1 Macro
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldDate
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
ChangeFileOpenDirectory _
"MAREK CONNELL HD:Users:marekconnell:Desktop:TEACHING:Teaching with microsoft word:"
Dim strTemp As String
Dim MyData As DataObject
Set MyData = New DataObject

strTemp = MyData.GetText(1)
ActiveDocument.SaveAs fileName:=(strTemp) & ".doc"
End Sub

Collapse -
No need to cross post.
by R. Proffitt Forum moderator / December 27, 2013 9:59 AM PST

Locking the other very old thread. This thread is more than 1774 days old and the other nearly a thousand olde.

Popular Forums
Computer Help 51,912 discussions
Computer Newbies 10,498 discussions
Laptops 20,411 discussions
Security 30,882 discussions
TVs & Home Theaters 21,253 discussions
Windows 10 1,672 discussions
Phones 16,494 discussions
Windows 7 7,855 discussions
Networking & Wireless 15,504 discussions


The most beautiful phone ever has one wildly annoying issue

The Samsung Galaxy S8's fast speeds and fantastic curved screen make it a top phone for 2017, but the annoying fingerprint reader could sour your experience.