简体   繁体   English

将Excel工作簿另存为CSV日语文件时,宏中出现错误1004

[英]Error 1004 in my Macro while saving a Excel workbook as a CSV for Japanese

I have written a simple Macro for saving all the Workbooks as separate CSV files. 我编写了一个简单的宏,用于将所有工作簿另存为单独的CSV文件。 This works fine on my local machine (English Lang) for paths like * D:\\MyFolder* . 在我的本地计算机(英语)上,对于* D:\\ MyFolder *之类的路径,这可以正常工作。

But when I am trying the same Macro on another windows machine with Japanese language enabled I am getting 1004 error for SaveAS method. 但是,当我在启用日语的另一台Windows计算机上尝试使用同一宏时, SaveAS方法却收到1004错误。

File paths like D:¥MyFolder¥ 文件路径,例如D:¥MyFolder¥

Below is the my code which is causing the error: 以下是导致错误的我的代码:

pathSeperator = Application.PathSeparator pathSeperator = Application.PathSeparator

strPath = InputBox("Enter EXISTING Directory path like d:\\someDirectoryName, d:", , , 1000) strPath = InputBox(“输入现有目录路径,例如d:\\ someDirectoryName,d:”,“,”,1000)

 SaveToDirectory = strPath & pathSeperator & "csv" & pathSeperator If Dir(strPath & pathSeperator & "csv", vbDirectory) = "" Then fso.CreateFolder SaveToDirectory Else fso.DeleteFolder strPath & pathSeperator & "csv" fso.CreateFolder SaveToDirectory End If For Each WS In ThisWorkbook.Worksheets newName = WS.Name & "-" & Format(Date, "yyyy-mm-dd") & "-" & Format(Time, "hhmmss") WS.Copy ActiveWorkbook.SaveAs SaveToDirectory & newName, xlCSVMSDOS, Local:=True ActiveWorkbook.Close Savechanges:=False Next 

On the Japanese language machine have you tried changing the font on the visual basic editor to Japanese fonts? 在日语机器上,您是否尝试过将Visual Basic编辑器上的字体更改为日语字体?

This can be done from the tool->options->format tab. 可以从工具->选项->格式选项卡中完成。

Edit 22/08/13 编辑13/08/22

A bit of a longshot, but I've read that the Japanese Yen character in ASCII is the same as the / charcter on english language machines, as such using Chr(92) should work in both. 有点远,但是我已经读到ASCII中的日元字符与英语机器上的/字符相同,因此使用Chr(92)可以在两种语言中同时使用。 On an English language machines it would appear as / whislt on a Japanese machine it would have the yen symbol. 在英语机器上,它将在日语机器上显示为/ whislt,带有日元符号。 A simple test would be to run the following macro on a Japanese machine and see what happens. 一个简单的测试是在日文机器上运行以下宏,然后看看会发生什么。

Sub TestSeperator()

MsgBox Chr(92)

End Sub

If this is the case then you need to make changes like the ones below: 在这种情况下,您需要进行如下更改:

SaveToDirectory = strPath & Chr(92) & "csv" & Chr(92)
If Dir(strPath & Chr(92) & "csv", vbDirectory) = "" Then

fso.CreateFolder SaveToDirectory
Else

fso.DeleteFolder strPath & chr(92) & "csv"
fso.CreateFolder SaveToDirectory

I have tried out your code on my English language machine and managed to raise a 1004 error when I entered the directory path including the final "\\" 我在我的英语计算机上尝试了您的代码,并在输入包含最后一个“ \\”的目录路径时成功引发了1004错误

I have modified the code so that it adds the path seperator if it is not present and the rest of the code assumes it is already in strPath. 我已经修改了代码,以便在不存在路径分隔符的情况下添加路径分隔符,其余的代码假定该路径分隔符已经在strPath中。

pathSeperator = Application.PathSeparator

strPath = InputBox("Enter EXISTING Directory path like d:\someDirectoryName, d:", , , 1000)
Set fso = New FileSystemObject

If Right(strPath, 1) <> pathSeperator Then 'added if clause
    strPath = strPath & pathSeperator
End If

SaveToDirectory = strPath & "csv" & pathSeperator  'Removed one pathSeperator
If Dir(strPath & pathSeperator & "csv", vbDirectory) = "" Then

fso.CreateFolder SaveToDirectory
Else

fso.DeleteFolder strPath & "csv" 'Removed one pathSeperator
fso.CreateFolder SaveToDirectory

End If

For Each WS In ThisWorkbook.Worksheets
    newName = WS.Name & "-" & Format(Date, "yyyy-mm-dd") & "-" & Format(Time, "hhmmss")
    WS.Copy
    ActiveWorkbook.SaveAs SaveToDirectory & newName, xlCSVMSDOS, Local:=True
    ActiveWorkbook.Close Savechanges:=False
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM