Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

General discussion

Word "Save As" Macro w/Current File Name

Feb 16, 2009 11:28PM 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)?

Thanks,

' 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:= _
False
End Sub

Discussion is locked

- Collapse -
Current and static file names?
Feb 17, 2009 3:47AM 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?

Mark

- Collapse -
Current (Static) vs Dynamic File Names
Feb 17, 2009 4:20AM PST

Mark,

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",

Thanks,

Chris

- Collapse -
It's not one instruction.
Feb 17, 2009 6:50AM 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.

Kees

- Collapse -
Same Problem
Jun 25, 2010 5:44AM 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.

Scott

- Collapse -
Re: macro
Jun 25, 2010 5:51AM PDT

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.

Kees

- Collapse -
I did see it
Jun 25, 2010 6:20AM PDT

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
Jun 25, 2010 6:36AM PDT

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.

Mark

- Collapse -
Re: macro
Jun 25, 2010 6:42AM PDT

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.

Kees

- Collapse -
Changing the filename from one extension to another.
Sep 5, 2012 6:10AM PDT

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
Dec 27, 2013 8:58AM PST

hi,

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
Selection.Copy
ChangeFileOpenDirectory _
"MAREK CONNELL HD:Users:marekconnell:Desktop:TEACHING:Teaching with microsoft word:"
Dim strTemp As String
Dim MyData As DataObject
Set MyData = New DataObject

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

- Collapse -
No need to cross post.
Dec 27, 2013 9:59AM PST

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