Temp Filename causing error?

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Temp Filename causing error?

Post by Rudi »

Hi,

As below, I have code that checks if the current file in a folder is the activeworkbook. If it is, I skip the workbook and move to the next. However, the objFile.Name is looking at what seems to be a temporary file and the ThisWorkbook.Name is the non-temporary file. See image below for question on how to deal with this. TX.

Code: Select all

   For Each objFile In objFolder.Files
      If objFile.Name = ThisWorkbook.Name Then GoTo nxtLoop   '"~$Template_Combine.xlsm"
      Workbooks.Open objFile
SP1-Mon,19-8.jpg
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Temp Filename causing error?

Post by HansV »

You could use

Code: Select all

      If objFile.Name = ThisWorkbook.Name Or Left(objFile.Name) = "~$" Then GoTo nxtLoop
By the way, to open the workbook, use objFile.Path. This returns the full name including the path, while the Name property returns only the filename without the path.

And, as you no doubt know, it is generally not necessary to select workbooks, sheets or ranges in a macro. Code runs more efficiently if you don't.
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Temp Filename causing error?

Post by Rudi »

TX. That did the trick....with one small change...

Code: Select all

If objFile.Name = ThisWorkbook.Name Or Left(objFile.Name, 2) = "~$" Then GoTo nxtLoop
Thanks for that recommendation about the Path. Its updated now.

About the selection statements....TX. I am aware of that and its just in there while I am testing the code and stepping through it. I delete those selection pairs when the code is tested and working smoothly. TX
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Temp Filename causing error?

Post by HansV »

Sorry, yes. It should have been Left(..., 2) of course. (It was just air code :blush:)
Best wishes,
Hans