In a macro I want to open unnamed files like *.*?

B

Barbara

I want to sequentially open all rtf files in a folder, manipulate them, save
as word files with the same name. There are over 200 and the names may change
- so I don't want to specify the actual filename.
 
K

Karl E. Peterson

Barbara said:
I want to sequentially open all rtf files in a folder, manipulate
them, save as word files with the same name. There are over 200 and
the names may change - so I don't want to specify the actual filename.

You'll probably want to punch up the VBA helpfiles, and take a look at the
Dir() command. Use it to loop the folder, collecting all the filenames
within an array, then process each element in the array.
 
B

Barbara

Thanks Karl - I am a SAS programmer and unfamiliar with VBA. I haven't found
VBA helpfiles - I will check the CD.

Barbara
 
K

Karl E. Peterson

Barbara said:
Thanks Karl - I am a SAS programmer and unfamiliar with VBA. I
haven't found VBA helpfiles - I will check the CD.

Easiest way, generally, is to type a command of interest, put the cursor on
it, and press F1. Is that not working?

Anyway, your "basic Dir loop" goes something like this:

Dim files() As String
Dim fn As String
Dim n As Long

fn = Dir("*.*")
Do While Len(fn)
ReDim Preserve files(0 To n) As String
files(n) = fn
n = n + 1
fn = Dir()
Loop

Hope that helps...
 
B

Barbara

Karl,

I tried to paste this in, but I know nothing - could you please show me
where to put in the text manipulation code? I just want to open a file and
save it in word using this code for one of the files:

Documents.Open FileName:="""C:\OCOG\Documents\Common results\adverse
events1st28daysaeonlyQQ102.rtf""", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "Normal"
End With
ActiveDocument.SaveAs FileName:="C:\OCOG\Documents\Common
results\adverse events1st28daysaeonlyQQ102.doc", _
FileFormat:=wdFormatDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="",
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False,
SaveFormsData _
:=False, SaveAsAOCELetter:=False
ActiveWindow.Close

Oh yes the f1 worked - but I did not understand the help menu.
Thanks

Barbara
 
K

Karl E. Peterson

Barbara said:
I tried to paste this in, but I know nothing - could you please show
me where to put in the text manipulation code? I just want to open a
file and save it in word using this code for one of the files:

After running the code I offered, you end up with an array of files. You
then need to iterate the array, performing whatever operations you want on
them. Perhaps create another subroutine that accepts a filename to work
on...

Public Sub ProcessFile(ByVal FileName As String)
' Do stuff here to the file
End Sub
Oh yes the f1 worked - but I did not understand the help menu.
Thanks

Hmmmm... That, along with familiarity with Google, are the two things that
will determine your success in this endeavor.
 
G

Graham Mayor

Try

Sub SaveAllRTFAsDoc()
Dim FirstLoop As Boolean
Dim myFile As String
Dim strDocName As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long

PathToUse = InputBox("Path To Use?", "Path", "D:\My Documents\\")

On Error Resume Next
Documents.Close SaveChanges:=wdPromptToSaveChanges
FirstLoop = True
myFile = Dir$(PathToUse & "*.rtf")
While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
If FirstLoop Then
With ActiveDocument
Selection.PageSetup.Orientation = wdOrientLandscape
End With
FirstLoop = False

Response = MsgBox("Do you want to process " & _
"the rest of the files in this folder", vbYesNo)
If Response = vbNo Then Exit Sub
Else
With ActiveDocument
Selection.PageSetup.Orientation = wdOrientLandscape
End With
End If
strDocName = ActiveDocument.FullName
intPos = InStrRev(strDocName, ".")
strDocName = Left(strDocName, intPos - 1)
strDocName = strDocName & ".doc"
myDoc.SaveAs FileName:=strDocName, _
FileFormat:=wdFormatDocument
myDoc.Close SaveChanges:=wdDoNotSaveChanges
myFile = Dir$()
Wend
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jonathan West

Barbara said:
I want to sequentially open all rtf files in a folder, manipulate them,
save
as word files with the same name. There are over 200 and the names may
change
- so I don't want to specify the actual filename.

Hi Barbara,

If you did a full install of Office, there should be a file on your system
called Batch Conversion Wizard.wiz, probably in a folder C:\Program
Files\Microsoft Office\Templates\1033 or similar.

Navigate to that file and double-click on it. The Batch Conversion wizard
will start and take you step by step through selecting the files you want to
convert.
 
B

Barbara

Thanks - I did look it up on Google and now I understand it better. I got to
the point where I could manipulate all the files sequentially, but not where
I could change the .rtf to .doc. I believe I will have to have two loops for
this. However, being able to change the style of all the tables was the main
point - and having them remain rtf does not seem to affect the cut and paste
ability of the tables for my users - at least on my machine!

What I am learning is that I should let SAS do what it does best and learn
VBA so that Word can do what it does best!

Barbara
 
B

Barbara

Thanks - I'll see how this does.

Barbara

Graham Mayor said:
Try

Sub SaveAllRTFAsDoc()
Dim FirstLoop As Boolean
Dim myFile As String
Dim strDocName As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long

PathToUse = InputBox("Path To Use?", "Path", "D:\My Documents\\")

On Error Resume Next
Documents.Close SaveChanges:=wdPromptToSaveChanges
FirstLoop = True
myFile = Dir$(PathToUse & "*.rtf")
While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
If FirstLoop Then
With ActiveDocument
Selection.PageSetup.Orientation = wdOrientLandscape
End With
FirstLoop = False

Response = MsgBox("Do you want to process " & _
"the rest of the files in this folder", vbYesNo)
If Response = vbNo Then Exit Sub
Else
With ActiveDocument
Selection.PageSetup.Orientation = wdOrientLandscape
End With
End If
strDocName = ActiveDocument.FullName
intPos = InStrRev(strDocName, ".")
strDocName = Left(strDocName, intPos - 1)
strDocName = strDocName & ".doc"
myDoc.SaveAs FileName:=strDocName, _
FileFormat:=wdFormatDocument
myDoc.Close SaveChanges:=wdDoNotSaveChanges
myFile = Dir$()
Wend
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

Barbara

Jonathan,

This was not installed - and it may just be the ticket to keep me from
making many loops.

Barbara
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top