PDA

View Full Version : [SOLVED:] How to include excel files?



Kilroy
06-07-2020, 02:55 PM
Hello everyone. Hope you are well. I have pieced together the following code but I need to include the possibility that an excel file type may be selected. Any guidance appreciated.


Sub DeleteTypesOfFiles()
'Kilroy
Dim strFilename As String
Dim strDocName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFilename = Dir$(strPath & InputBox("File type? aterisk.type"))
While Len(strFilename) <> 0
Set oDoc = Documents.Open(strPath & strFilename)
MyFile = ActiveDocument.path & "\" & ActiveDocument.Name
ActiveDocument.Close (wdDoNotSaveChanges)
Kill MyFile
strFilename = Dir$()
Wend
End Sub

macropod
06-07-2020, 07:32 PM
It's not apparent to me why you have:

Set oDoc = Documents.Open(strPath & strFilename)
MyFile = ActiveDocument.path & "\" & ActiveDocument.Name
ActiveDocument.Close (wdDoNotSaveChanges)
Kill MyFile
There is no point I can see in opening the document, only to close & delete it. Surely all you need is:

Kill strPath & strFilename
In any event:

MyFile = ActiveDocument.path & "\" & ActiveDocument.Name
could be reduced to:

MyFile = oDoc.FullName

Kilroy
06-07-2020, 08:21 PM
Paul thanks for your input. It wasn’t apparent to me whythat statement was in there either, so I removed it and ran a couple of testsusing your suggestions. It really was what was causing all of my issues withthe different file types. I also used the “Kill strPath & strFilename” andthe code now doesn’t need the “MyFile” statements at all. Runs very smooth andmuch quicker.

Question: You moved this post to Misc? I was asking a wordvba question so it’s unclear to me why it should be here.

Thanks again!

macropod
06-08-2020, 12:22 AM
Question: You moved this post to Misc? I was asking a wordvba question so it’s unclear to me why it should be here.
Indeed, because without the deleted lines and with your requirement to include Excel, for example, nothing in the code is Word-specific - it could apply equally to any Office application.