Opening files according to extension

  • Thread starter Carlos Chalhoub
  • Start date
C

Carlos Chalhoub

Hi listmates,

I've inherited a UserForm from 1997 that I'm trying to update for our use in
this year. A few things have changed in the underlying macros and I've had
to add a couple of checkboxes. Previsously all the checkboxes ran the same
browse directory and opened Word files. However, the 2 new checkboxes must
open HTML files in txt format. How can I make the macro capture the
extension of the file and open it accordingly? I've added the main piece of
the macro. Thanks for your help.

Carlos


Function PerformGlobalChange(inputChoice As Integer, aKeyword As String,
aTitle As String, aSubject As String, aAuthor As String, aCompany As String)

Dim resp As Integer
Dim fileNames As String
Dim selectChoice As Integer
' Initialize variables

fileNames = "*.<Please enter proper file extension>"

If Windows.Count <> 0 Then
MsgBox "Please close all windows and try again.", , "SOLCORP"
End
End If

resp = MsgBox("Do you want to make global changes to the documentation
files?", vbAbortRetryIgnore + vbYesNo, "SOLCORP")
If resp = vbNo Then End

fileNames = InputBox("Enter the names of the files you want to
process.", "SOLCORP", fileNames)
If fileNames = "" Then End

MsgBox "In the next window, change to the directory where the files are,
and then click Open.", _
vbDefaultButton1, "SOLCORP"

Call ListDirectoryAndFiles(fileNames)

********* This is where I want it to decide how to open the files*********
For i = 1 To fs.FoundFiles.Count
currentFile = fs.FoundFiles.Item(i)
If fileNames = "*.htm" Then
Documents.Open Format = wdOpenFormatText
Else: Documents.Open fileName:=currentFile
End If
'***********************************************************

' Perform global changes
choice = inputChoice
If choice \ 512 = 1 Then
choice = choice Mod 512
Call mdFixHTMLCode
End If
If choice \ 256 = 1 Then
choice = choice Mod 256
Call mdOpenLinksinNewWindow
End If
If choice \ 128 = 1 Then
choice = choice Mod 128
Call ChangeCompany(aCompany)
End If
If choice \ 64 = 1 Then
choice = choice Mod 64
Call ChangeAuthor(aAuthor)
End If
If choice \ 32 = 1 Then
choice = choice Mod 32
Call ChangeSubject(aSubject)
End If
If choice \ 16 = 1 Then
choice = choice Mod 16
Call ChangeTitle(aTitle)
End If
If choice \ 8 = 1 Then
choice = choice Mod 8
Call AcceptRevision
End If
If choice \ 4 = 1 Then
choice = choice Mod 4
Call ChangeKeywords(aKeyword)
End If
If choice \ 2 = 1 Then
choice = choice Mod 2
Call RemoveTags("pcr^#^#")
End If
If choice Mod 2 = 1 Then
Call RemoveTags("fs^#^#")
End If

ActiveDocument.Close Savechanges:=wdSaveChanges
Next i

MsgBox "Macro completed.", vbDefaultButton1, "SOLCORP"

End Function
 
D

Doug Robbins - Word MVP

Hi Carlos,

Instead of

If fileNames = "*.htm" Then

Use

If LCase(Right(currentFile, 3)) = "htm" Then

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
C

Carlos Chalhoub

Hi Doug,

When I add this line, I get a: "Compile error in hidden module". What's
happening?

Thanks
Carlos
 
D

Doug Robbins - Word MVP

Insert a MsgBox currentFile and see what it returns.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
C

Carlos Chalhoub

Hi Doug,

I tried putting the msgbox after "currentFile = fs.FoundFiles.Item(i)", then
at different other spots, but I still get the compile error right after I
click OK on the form. Any other ideas?

Carlos
 
D

Doug Robbins - Word MVP

Remove all of my suggestions and see if you still get the Compile Error. If
so, it's something else.

Or, investigate the use of the FileDialog(msoFileDialogFilePicker)

This example does something else, but shows you how it is used.

'Declare a variable as a FileDialog object.
Dim fd As FileDialog, SelectedTemplate As String

'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Use a With...End With block to reference the FileDialog object.
With fd
.Title = "File New"
'Restrict the user to selecting one template
.AllowMultiSelect = False
'Set the path
.InitialFileName = "C:\Documents and Settings\Doug\Application
Data\Microsoft\Templates\"
'Use the Show method to display the File Picker dialog box and
return the user's action.
'The user pressed the action button.
If .Show = -1 Then
'vrtSelectedItem is a String that contains the path of each
selected item.
SelectedTemplate = .SelectedItems(1)
'The user pressed Cancel.
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
MsgBox "You selected " & SelectedTemplate
'Documents.Add SelectedTemplate

or this (originally posted by someone else - sorry I forget by who it was)
handles multiple files:

'Declare a variable as a FileDialog object.

Dim fd As FileDialog



'Create a FileDialog object as a File Picker dialog box.

Set fd = Application.FileDialog(msoFileDialogFilePicker)



'Declare a variable to contain the path

'of each selected item. Even though the path is a String,

'the variable must be a Variant because For Each...Next

'routines only work with Variants and Objects.

Dim vrtSelectedItem As Variant



'Use a With...End With block to reference the FileDialog object.

With fd



'Use the Show method to display the File Picker dialog box and
return the user's action.

'The user pressed the action button.

If .Show = -1 Then



'Step through each string in the FileDialogSelectedItems
collection.

For Each vrtSelectedItem In .SelectedItems



'vrtSelectedItem is a String that contains the path of each
selected item.

'You can use any file I/O functions that you want to work
with this path.

'This example simply displays the path in a message box.

MsgBox "The path is: " & vrtSelectedItem



Next vrtSelectedItem

'The user pressed Cancel.

Else

End If

End With



'Set the object variable to Nothing.

Set fd = Nothing

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
C

Carlos Chalhoub

Hi Doug,

I found the problem. I needed to add "fileName:=currentFile" to the first
Documents.Open.

For i = 1 To fs.FoundFiles.Count
currentFile = fs.FoundFiles.Item(i)
If LCase(Right(currentFile, 3)) = "htm" Then
Documents.Open fileName:=currentFile, Format:=wdOpenFormatText
Else: Documents.Open fileName:=currentFile
End If

Thanks for your jelp.
Carlos
 
C

Carlos Chalhoub

Hi Doug,

The macro just fixed calls a private sub "AcceptRevision". I'm trying to
make it save the file in doc format first, then in rtf format second.. My
code below does not seem to be saving in rtf format. I think the myDocname
variable is conflicting with the currentfile variable in the calling macro
(see the beginning of this post), but I'm not sure how to fix this. Can you
help? Thanks.

*****************************************
Private Sub AcceptRevision()
ActiveDocument.AcceptAllRevisions
ActiveDocument.Save

myDocname = ActiveDocument.Name
pos = InStr(myDocname, ".")
If pos > 0 Then
myDocname = Left(myDocname, pos - 1)
myDocname = myDocname & ".rtf"
ActiveDocument.SaveAs fileName:=myDocname,
FileFormat:=wdFormatRTF
End If
ActiveDocument.Close Savechanges:=wdDoNotSaveChanges
End Sub
*****************************************

Best regards
Carlos
 
D

Doug Robbins - Word MVP

Are you sure that it is not being saved in rtf format and that you cannot
see it in the directory because you are using File>Open with the document
type set to Word Documents. Change it to All Files and see if it's there.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
C

Carlos Chalhoub

I am using Windows Explorer to look at the files and I just realized that
the rtf files are being saved to the folder just above the subfolder I'm
working on (i.e. one level up), while the doc files are being saved to the
right subfolder. Quite close, but not there yet. I'm guessing that there
should be some reference to the same folder in the macro, any ideas?

Thanks again.

Carlos
 
D

Doug Robbins - Word MVP

Use ActiveDocument.FullName to get the path and filename

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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