PDA

View Full Version : Problem with a macro



OdiN
12-19-2007, 04:52 PM
I am having a strange problem with a macro.

The macro is a batch print macro which takes all files in a particular folder and prints them all out. The macro works fine on my computer, and others, but one user is having problems with hers. The only difference is that it's a laptop, but it's got the same version of Office so I don't think that would make any difference.

The error is:

Run-time error 5981

Could not open macro storage


I have done some googling and the suggestions I have found - reinstalling Office and checking for broken references has not worked.

I am posting the code below - maybe there is a problem with it? The bolded line is where the error occurs. Any ideas?





Sub BatchPrint()

Dim fs, f, fc, f1
Dim Cnt As Integer
Dim varExt As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder("S:\Batch Print")
Set fc = f.Files
Cnt = 0

Dim myDoc As Word.Document

For Each f1 In fc

varExt = LCase(Right(f1.Name, 4))
If varExt = LCase(".doc") Then
Application.Documents.Open FileName:=f & "\" & f1.Name, Visible:=False
Set myDoc = Application.Documents(f & "\" & f1.Name)
myDoc.PrintOut
myDoc.Close savechanges:=False
Set myDoc = Nothing
varExt = ""
End If

Cnt = Cnt + 1

Next

MsgBox "Batch Print has processed " & Cnt & " files successfully.", vbOKOnly, "Batch Printing Completed"


End Sub

fumei
12-20-2007, 11:04 AM
There is probably a network connection that is different for the laptop.

Application.Documents.Open FileName:=f & "\" & _
f1.Name, Visible:=False
Set myDoc = Application.Documents(f & "\" & f1.Name)

can be written as one instruction.

Set myDoc = Application.Documents.Open FileName:=f & "\" & _
f1.Name, Visible:=False

I do not understand what you are attempting with your variables Filename, Ext etc. You can text on each file (f1) itself without using any variables.

You have a FileList as an array, but never do anything with it.

OdiN
12-20-2007, 11:16 AM
Yeah I noticed I had the array still in there, I don't know why I didn't remove it.

The FileName was for a test...I need to get rid of that.

I just threw this together really quickly when it was originally made, it worked, so I didn't bother with it anymore.

Network connections are the same network wide.

OdiN
12-20-2007, 11:25 AM
I just tried a direct reference to the folder as well, i.e. \\server\share

Still the same error.

I updated the code in the original post to clean it up like I just did for the file.