Solved VBA Macro Fails to Run - Error 52 Bad File name?

October 23, 2014 at 02:03:18
Specs: Windows 7
I am trying to create a macro to combine a number or powerpoint presentations into one to enable me to print a handout more simply.

Here is the script I have created:

Sub InsertFromList()
' Inserts all presentations named in LIST.TXT into current presentation
' in list order

On Error GoTo ErrorHandler

Dim sListFileName As String
Dim sListFilePath As String
Dim iListFileNum As Integer
Dim sBuf As String

' name of file containing files to be inserted
sListFileName = "List.txt"

' backslash terminated path to folder containing list file:
sListFilePath = ""

' Do we have a file open already?
If Not Presentations.Count > 0 Then
Exit Sub
End If

' If LIST.TXT file doesn't exist, create it
If Len(Dir$(sListFilePath & sListFileName)) = 0 Then
iListFileNum = FreeFile()
Open sListFilePath & sListFileName For Output As iListFileNum
' get file names
sBuf = Dir$(sListFilePath & "*.PPT")
While Not sBuf = ""
Print #iListFileNum, sBuf
sBuf = Dir$
Close #iListFileNum
End If

iListFileNum = FreeFile()
Open sListFilePath & sListFileName For Input As iListFileNum
' Process the list
While Not EOF(iListFileNum)
' Get a line from the list file
Line Input #iListFileNum, sBuf

' Verify that the file named on the line exists
If Dir$(sBuf) <> "" Then
Call ActivePresentation.Slides.InsertFromFile( _
sBuf, ActivePresentation.Slides.Count)
End If

Close #iListFileNum
MsgBox "DONE!"

Exit Sub
Call MsgBox("Error:" & vbCrLf & Err.Number & vbCrLf & Err.Description, _
vbOKOnly, "Error inserting files")
Resume NormalExit
End Sub

However when I attempt to run it, I get a Error 52 Bad File name or number. Which file name is it referring to?

Any help greatly appreciated

See More: VBA Macro Fails to Run - Error 52 Bad File name?

Report •

October 23, 2014 at 05:01:04
I'll take a look at this a little later, but I have a quick question...

Why not just write a macro to loop through the folder and print each file, one at a time?

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.

Report •

October 23, 2014 at 09:04:26
✔ Best Answer
First, a posting tip for this forum:

If you are going to post VBA code, please see this How To for instructions on using the pre tags so that the code is easier for us to read:

OK, I should point out that I don't do much with VBA for PowerPoint; VBA for Excel is where I have most of my expertise. That said, I used some basic VBA debugging techniques, such as Watches and Single Stepping to try an figure out what was going on.

FYI...those techniques are described in detail here:

As I Single Stepped through your original code (via F8) I noticed that the Error handler fired at this line:

If Len(Dir$(sListFilePath & sListFileName)) = 0 Then

When I reviewed that instruction, I noticed that it was using the sListFilePath variable, which your code set to that long string (shortened here so it fits on one line):

sListFilePath = ""

Well, I don't have that FilePath on my machine, so I created my own folder and used that path name in the instruction:

sListFilePath = "U:\PPT VBA Test\"

Once I did that, your code ran through to completion, inserting the slides and presenting the "Done" MsgBox.

Based on that result, I have to assume that your long file path string is incorrect. You might try getting rid of the %20's and just using spaces since all you are doing is building is a text string for VBA to use as a file path. Granted, your path is on your Intranet and mine is accessing a networked drive by drive letter, so I can only speculate here.

The other thing I noticed is that you have one spot in your path that shows the %20 twice in a row:


Is that correct? Maybe the %20's are OK to use, but the double %20 would be seen as 2 spaces. Does the FilePath really have 2 spaces after the 242?

I hope something here helps, even if it's just the link to the VBA Debugging tutorial.

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.

Report •
Related Solutions

Ask Question