Log in

View Full Version : Batch convert files to RTF



asset
04-28-2013, 04:24 AM
Hi!
I'm trying to make a script which allows to choose files and then batch convert them into RTF files.
I have a part for the RTF convert (below). Could someone tell me how to write a part for the loop which will execute this code on all chosen files?
Thanks!


Sub SaveAsRTF()

'getting active document path (without filename and extension
FileName = CreateObject("scripting.filesystemobject").getbasename(ActiveDocument.Name)

'saving as RTF
ActiveDocument.SaveAs FileName:=ActiveDocument.Path & "\" & FileName & ".rtf", FileFormat:=wdFormatRTF
End Sub

macropod
04-28-2013, 05:56 AM
Assuming you want to be able to select a series of files, try:
Sub MakeRTF()
Application.ScreenUpdating = False
Dim strFile As Variant, wdDoc As Document, strDocName As String
'create FileDialog object as File Picker dialog box
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
'use Show method to display File Picker dialog box and return user's action
.InitialFileName = "C:\Users\" & Environ("UserName") & "\Documents\*.doc"
If .Show = -1 Then
'step through each string in the FileDialogSelectedItems collection
For Each strFile In .SelectedItems
Set wdDoc = Documents.Open(FileName:=strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
strDocName = Left(.FullName, InStrRev(.FullName, ".")) & "RTF"
.SaveAs2 FileName:=strDocName, FileFormat:=wdFormatRTF, AddToRecentFiles:=False
.Close SaveChanges:=False
End With
Next
End If
End With
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

asset
04-28-2013, 09:32 AM
Thanks Paul! I'll try this. May I ask a question about: If .Show = -1 Then

What does "-1" do here?

macropod
04-28-2013, 02:09 PM
Basically, it's a True/False test that checks whether you've pressed the dialog's OK button.

asset
04-28-2013, 07:54 PM
Thanks Paul!