运行文件重命名文本字符串作为VBA语句 [英] Run file rename text string as a VBA statement
问题描述
我想使用存储在MS Access子窗体文本框中的VBA命令重命名文件.可能需要注意的是,VBA命令是使用来自数据库查询的连接公式生成的,并复制到了文本框中.
I want to rename files using a VBA command stored in an MS Access subform text box. It may be of note the VBA commands are generated using a concatenation formula from a database query and copied into the text box.
这实际上是我尝试使用的代码,但我得到了runtime error 2434 - The expression you entered contains invalid syntax
.可能比使用eval()命令更好.
This is essentially the code I'm attempting to use but I get runtime error 2434 - The expression you entered contains invalid syntax
. There may be a better way than using the eval() command.
Public Function BulkRenameFile()
Dim script As String
script = "Name ""c:\ipfimport\PE2258N2754\2620.jpg"" As ""c:\ipfimport\PE2258N2754\PE2258N2754_PH1_20141216_2620.jpg"""
BulkRenameFile = Eval(script)
MsgBox ("Photo Renaming Complete")
End Function
任何帮助将不胜感激.
推荐答案
最后,我更改了查询中的字段表达式,该表达式构建了从VBA代码到DOS的重命名命令.我将DOS重命名代码的完整数据表列粘贴到文本框中.文本框将导出为.txt文件,然后在Windows DOS命令外壳中作为.bat运行.
In the end i changed the field expression in my query which builds a rename command from a VBA code to a DOS one. I paste the complete datasheet column of DOS rename codes into a textbox. The text box is exported as a .txt file and then run as a .bat in windows DOS command shell.
示例DOS重命名命令粘贴到文本框中:
example DOS rename command pasted into a textbox:
rename "c:\ipfimport\PE2152N2380\*7112.jpg" "PE2152N2380_LT_T25_20140516_7112.jpg"
rename "c:\ipfimport\PE2152N2380\*7113.jpg" "PE2152N2380_LT_T25_20140516_7113.jpg"
最终的工作代码:
Function FileRename()
Dim fso As New FileSystemObject
Dim stream As TextStream
strPath = [Forms]![File Rename > Main].Text_FilePathBase.Value
strFile = "Rename.bat"
Set stream = fso.CreateTextFile(strPath & strFile, True)
Debug.Print strPath & strFile
strForm = [Forms]![File Rename > Main].Text_PasteRenameScript.Value
stream.Write strForm
stream.Close
Set fso = Nothing
Set stream = Nothing
Call Shell(strPath & strFile, vbNormalFocus)
MsgBox ("Renaming Complete")
End Function
这篇关于运行文件重命名文本字符串作为VBA语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!