#1
|
|||
|
|||
Word macro question
I'm putting together a word macro with the ultimate goal of upon execution the macro looping through all rtf files in a folder and saving them as docs. In it's current state the macro loops through the files as expected. However i'm required to click the save button for each file. Ideally it would automatically take care of this. Any guidance would be greatly appreciated. Here's the macro in it's current state: Code:
Sub RTFtoDOC() Dim FName As String Dialogs(wdDialogFileOpen).Show Application.ScreenUpdating = False FName = Dir("*.rtf") While (FName > "") Application.Documents.Open FileName:=FName _ , ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _ PasswordDocument:="", PasswordTemplate:="", Revert:=False, _ WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _ wdOpenFormatAuto, XMLTransform:="", Encoding:=1252 ActiveDocument.SaveAs FileName:=FName, FileFormat:=wdFormatDocument ActiveDocument.Close 'SaveChanges:=wdSaveChanges FName = Dir() Wend Application.ScreenUpdating = True End Sub Last edited by macropod; 08-26-2015 at 05:04 PM. Reason: Added code tags & formatting |
#2
|
||||
|
||||
It seems to me your code could be reduced to:
Code:
Sub RTFtoDOC() Dim FName As String Dialogs(wdDialogFileOpen).Show Application.ScreenUpdating = False FName = Dir("*.rtf") While (FName > "") Application.Documents.Open FileName:=FName, ReadOnly:=True, AddToRecentFiles:=False ActiveDocument.SaveAs FileName:=FName, FileFormat:=wdFormatDocument ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges FName = Dir() Wend Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks for the reply. When I submit the suggested code the macro errors out.
"Run-time error '5155': we cant save this file because its read only. To keep your changes, you'll need to save the document with a new name or in a different location" Any idea how to get around this? Thanks, Mike. |
#4
|
||||
|
||||
I assume the Dialogs line was meant to prompt for a folder where the RTFs were going to be found. This is not happening with your code so I've changed that part.
The error you were encountering was due to the fact that you were trying to overwrite the currently open document. This code below saves it with a changed suffix so the error is avoided. Code:
Sub RTFtoDOC() Dim FName As String, fldr As FileDialog, sItem As String Dim sNewName As String Set fldr = Application.FileDialog(msoFileDialogFolderPicker) With fldr .Title = "Select a Folder" .AllowMultiSelect = False .InitialFileName = strPath .Show sItem = .SelectedItems(1) & Application.PathSeparator End With FName = Dir(sItem & "*.rtf") While (FName > "") Application.Documents.Open FileName:=sItem & FName, ReadOnly:=True, AddToRecentFiles:=False sNewName = Split(FName, ".")(0) ActiveDocument.SaveAs FileName:=sItem & sNewName, FileFormat:=wdFormatDocument ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges FName = Dir() Wend End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
That works. Thank you very much for your assistance.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro Question: Need help making a macro to highlight the first word in every sentence | LadyAna | Word | 1 | 12-06-2014 10:39 PM |
Question about macro in word 2013 | tombags | Word | 1 | 06-25-2014 04:56 PM |
Macro Needed to Insert Asnwer to A Question in Multiple Choice Format Question | rsrasc | Word VBA | 7 | 03-28-2014 12:28 PM |
A newbie question: a must to save macro word file as .docm? | tinfanide | Word VBA | 6 | 12-06-2011 03:02 PM |
Macro question | PaulY | Word VBA | 1 | 12-22-2010 11:21 PM |