View Single Post
  #14   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Problem running macros in Excel filenames containing specialcharacters like []

Thanks for posting--now google has it for anyone who needs it.

But it maybe easier to just rightclick and saveas a nice name <vbg.

" wrote:

I found a solution which does workaround this problem. On Workbook_Open
a copy of the workbook with [, ] replaced by (, ) in the filename will
be saved. The "Save As" will automatically close the original file and
keep the new one with the modified filename open. After saving the copy
the original workbook containing [, ] in the filename will be deleted.
As a result, the loading of a workbook containing [,] in the filename
takes a bit longer but the opening process behaves way as if the user
opens a regular workbook.

Here the code:

Sub Workbook_Open()
Dim oldFileName, curFileName, newFileName As String

' Read current filename and replace [, ] through (, )
' This is required because toolbar buttons wouldn't work
curFileName = GetFileName()
newFileName = Replace(Replace(curFileName, "[", "("), "]", ")")

' If file contained [], replace them with ()
If (Not curFileName = newFileName) Then
' Create and open the same file without []
Excel.ActiveWorkbook.SaveAs (Excel.ActiveWorkbook.Path & "\" &
newFileName)
End If

' Remove the old file containing []
oldFileName = Replace(Replace(newFileName, "(", "["), ")", "]")
If (Not oldFileName = newFileName) Then
oldFileName = FileSystem.Dir(Excel.ActiveWorkbook.Path & "\" &
oldFileName)
If (oldFileName < "") Then
FileSystem.Kill (Excel.ActiveWorkbook.Path & "\" &
oldFileName)
End If
End If
End Sub


--

Dave Peterson