Thisworkbook.path& " \"返回错误76

时间:2015-03-05 21:31:07

标签: xml vba excel-vba excel

单击此按钮时,我收到文件未找到错误。 它是一个xml文件,它以另一个程序使用的格式接收输入和吐出另一个xml文件,并使用示例文件来构建它。我是一名技术人员,而不是程序员,而且我很难让这个工作。

该行

FileCopy ThisWorkbook.Path + "\" + "sample 800A.xml", xmfile 

是一个给我一个错误的,但我的理解是,如果我在与此工作簿相同的文件夹中有一个名为sample 800A的xml文件,我不应该找到一个找不到文件的错误。我把所有代码都放在上下文按钮中。

Private Sub CommandButton1_Click()
r = ActiveCell.Row
'folder = Cells(r, 1).Text + ". " + Cells(r, 4).Text + "+" + Cells(r,   5).Text + "." + Cells(r, 6).Text
'pathfolder = ThisWorkbook.Path + "\" + folder

'MkDir (pathfolder)

xmfile = Cells(r, 4).Text + "-" + Cells(r, 5).Text + "." + Cells(r, 6).Text + "-Static Feeder.xml"
del = Left(xmfile, 5)
xmfile = Replace(xmfile, del, "\" + Cells(r, 1).Text + ". ")

xmfile = ThisWorkbook.Path + "\00. XML Files" + xmfile 'xmfile2 = pathfolder + "\test.xml"

PUs = Application.WorksheetFunction.Floor_Math(1.08 * Cells(r, 15) * Cells(r, 12), 10)
PUe = Application.WorksheetFunction.Ceiling_Math(1.08 * Cells(r, 15) *     Cells(r, 12), 10)
 OL1 = Cells(r, 20).Text
OL2 = Cells(r, 22).Text
STL = Cells(r, 24).Text
GND = Cells(r, 26).Text

If Cells(r, 11) > 2 Then
FileCopy ThisWorkbook.Path + "\" + "sample 800A.xml", xmfile
End If

If Cells(r, 11) <= 2 Then
FileCopy ThisWorkbook.Path + "\" + "sample 6A.xml", xmfile
End If

Open xmfile For Input As #1
Dim Textline As String
Do While Not EOF(1)
Line Input #1, Textline
Loop

If Cells(r, 11) > 2 Then
Textline = Replace(Textline, ">111<", ">" + Str(PUs) + "<")
Textline = Replace(Textline, ">112<", ">" + Str(PUe) + "<")
Textline = Replace(Textline, ">222<", ">" + OL1 + "<")
Textline = Replace(Textline, ">333<", ">" + OL2 + "<")
Textline = Replace(Textline, ">444<", ">" + STL + "<")
Textline = Replace(Textline, ">555<", ">" + GND + "<")
End If

If Cells(r, 11) <= 2 Then
PUs = 1.13 * Cells(r, 14)
PUe = 1.17 * Cells(r, 14)
Textline = Replace(Textline, ">1.11<", ">" + Str(PUs) + "<")
Textline = Replace(Textline, ">1.12<", ">" + Str(PUe) + "<")
Textline = Replace(Textline, ">2.22<", ">" + OL1 + "<")
Textline = Replace(Textline, ">3.33<", ">" + OL2 + "<")
Textline = Replace(Textline, ">4.44<", ">" + STL + "<")
Textline = Replace(Textline, ">5.55<", ">" + UNB + "<")
End If


Close #1
Open xmfile For Output As #1
Print #1, Textline

Close #1
MsgBox ("CPC XML file created for " + xmfile)

End Sub

2 个答案:

答案 0 :(得分:1)

尝试:

FileCopy ThisWorkbook.Path & "\" & "sample 800A.xml", xmfile

答案 1 :(得分:0)

我的猜测是你无法访问该文件。它并没有真正写在任何地方(我可以轻松找到),但如果用户无法修改文件,也会显示“找不到文件”错误。因为对于当前用户,该文件不存在。

确保用户可以访问该文件并对其进行更改,而不会弹出Windows UAC(用户帐户控制)警告。如果他们无法改变它,这个副本也会失败

修改

实际上,在再次阅读你的问题后,我意识到出了问题。我不认为filecopy会抛出错误。我们从documentation filecopy中看到抛出错误52,55,53(找不到文件)。你看到一个错误76?这通常与mkDir调用有关。但你的mkDir已被注释掉了。你确定xmfile正是你想要的吗?关于文件复制的一个棘手的问题是,如果目标目录不存在,它将失败。你无法做到

Call FileCopy("c:\somefile.txt","c:\a\difffilename.txt")

如果目录“c:\ a”不存在

有时vba停止的地方并不总是发生错误的地方,这种方式很奇怪