"Bad file name or number" when FileCopy

mintz

Board Regular
Joined
Aug 5, 2015
Messages
129
Untitled.png


A1 has some Chinese text
When I change the text in A1 to English it works fine
It doesn't work with the Chinese text
How do I fix this?
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I would think its part of being able to use unicode properly.

Using the root directory of C can cause problems
 
Upvote 0
I would think its part of being able to use unicode properly.

Using the root directory of C can cause problems

It's exactly what I thought,it has something to do with the encoding

I tried saving to my usb disk with identical result

How do I get around that?
 
Upvote 0
It seems that the VBA FileCopy Method converts the strings that are passed to it to ANSI .. To overcome this issue, you can use the Unicode Alias of the CopyFile API .. I personnaly use it to handle files titled with Arabic strings.

Here is the VBA FileCopy alternative for unicode :

Code:
Option Explicit

#If VBA7 Then
    Declare PtrSafe Function CopyFile Lib "kernel32" Alias "CopyFileW" (ByVal lpExistingFileName As LongPtr, ByVal lpNewFileName As LongPtr, ByVal bFailIfExists As Long) As Long
    Declare PtrSafe Function MyMsgBoxAlias Lib "user32" Alias "MessageBoxW" (ByVal hwnd As LongPtr, ByVal lpText As LongPtr, Optional ByVal lpCaption As LongPtr, Optional ByVal wType As Long = 0) As Long
#Else
    Declare Function CopyFile Lib "kernel32" Alias "CopyFileW" (ByVal lpExistingFileName As Long, ByVal lpNewFileName As Long, ByVal bFailIfExists As Long) As Long
    Declare Function MyMsgBoxAlias Lib "user32" Alias "MessageBoxW" (ByVal hwnd As Long, ByVal lpText As Long, ByVal lpCaption As Long, Optional ByVal wType As Long=0) As Long
#End If


Public Sub FileCopyW(SourceFile As String, DestinationFile As String, ByVal OverWrite As Boolean)
    If CopyFile(StrPtr(SourceFile), StrPtr(DestinationFile), CLng(Not OverWrite)) = 0 Then
        Select Case Err.LastDllError
            Case 2
                MsgBox "Wrong File Name.", vbCritical
            Case 3
                MsgBox "Wrong Path.", vbCritical
            Case 80
                If MyMsgBox("The file :   (" & DestinationFile & ")" & vbCrLf & "Already exists." & vbCrLf & _
                vbCrLf & "Do you want to overwrite it ? ", vbYesNo + vbExclamation + vbSystemModal) = vbYes Then
                    Call CopyFile(StrPtr(SourceFile), StrPtr(DestinationFile), 0)
                End If
         End Select
    End If
End Sub


Public Function MyMsgBox(ByVal Prompt As String, Optional ByVal Buttons As VbMsgBoxStyle, Optional ByVal Title As String = vbNullChar) As VbMsgBoxResult
    MyMsgBox = MyMsgBoxAlias(0, StrPtr(Prompt), StrPtr(Title), Buttons)
End Function

And here is an example of how you would use it :
Code:
Sub Test()
    Dim PicName As String
    
    PicName = Range("A1").Text & ".jpg"
    
    FileCopyW SourceFile:="C:\Users\Daniel\Downloads\" & PicName, _
              DestinationFile:="C:\" & PicName, _
              OverWrite:=False
End Sub
 
Upvote 0
Can I fix this my code:

Sub SaveAsTxtFiles()
Dim xDir, dest_folder As String
Dim xFile As String
Dim xRow, n, i As Long
n = Sheets("TXT").Range("H1").Value
dest_folder = "C:\Users\Administrator\Desktop\PLL TXT"
For i = 2 To n Step 5
xDir = Sheets("TXT").Range("G" & i).Value
xFile = Dir(xDir & Application.PathSeparator & "*")
Do Until xFile = ""
xRow = Application.Match(xFile, Range("A:A"), 0)
If xRow > 0 Then
FileCopy xDir & Application.PathSeparator & xFile, dest_folder & Application.PathSeparator & Cells(xRow, "H").Value
End If
xFile = Dir
Loop
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,175
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top