PDA

View Full Version : Macro to rename in "save-as" file.



HersheyEd
01-12-2012, 02:33 PM
I am new to this forum and have no experience directly in VBA, but I have made some basic Word 2010 macros.

I have a list of about 300 files that I need to do a save-as in .txt format and save with .txt instead of the current .doc extension. My limited record macro is retaining the name of the file I record the macro on for all of the files and wanting to overwrite which is not what I want it to do.

Any help to rename would be greatly appreciated.

Ed

macropod
01-13-2012, 03:33 AM
Hi Ed,

Try:
Sub DocumentsToText()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
.SaveAs strFolder & "\" & Split(.Name, ".")(0), FileFormat:=wdFormatText, AddToRecentFiles:=False
.Close SaveChanges:=False
End With
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function