【发布时间】:2012-07-24 07:51:41
【问题描述】:
我正在使用 Visual Basic 为 Autodesk Inventor 编写宏。我创建了一个调用文件对话框的宏,请参见下面的代码。一切正常,除非用户将文件名放入一个句点和一个大于零的数字。
例如,如果用户将 testfile.test 放入框中并点击确定。当我询问他们使用 .FileName 放在那里的内容时,我得到“testfile.test”。就像我应该的那样。
但是,如果用户输入 testfile.1 或 testfile.10 或 testfile.1mdksj 或任何大于零的数字,那么我将返回“testfile”。由于某种原因,期间和期间之后的所有内容都会被删除。
这是什么原因?这是 Visual Basic 中的错误还是我做错了什么?
'Set up the file dialog
Dim oFileDlg As FileDialog
' Create a new FileDialog object.
Call ThisApplication.CreateFileDialog(oFileDlg)
'Define the filter to select part and assembly files or any file.
oFileDlg.Filter = "All Files (*.*)|*.*"
'Define the part and assembly files filter to be the default filter.
oFileDlg.FilterIndex = 1
'Set the title for the dialog.
oFileDlg.DialogTitle = "Save File As"
'Tell the dialog box to throw up and error when cancel is hit by user
oFileDlg.CancelError = True
'Show the file dialog
On Error Resume Next
oFileDlg.ShowSave
'save the user specified file
Dim newFileName As String
newFileName = oFileDlg.FileName
更新:
我最终做了以下“破解”以使事情在处理期间仍然有效:
oFileDlg.fileName = sFname & "."
oFileDlg.ShowSave
fullName = Left$(oFileDlg.fileName, Len(oFileDlg.fileName) - 1)
这在 Windows 7 和 Windows 10 上运行了很长一段时间。不幸的是,Windows 10 创意更新似乎改变了文件对话框的工作方式。使用上面的代码,如果名称中没有句点,则 fullName 将返回空白,如果名称中有句点,则从左侧开始截断第一个句点。
我不太确定 Windows 10 发生了什么变化,但它几乎摧毁了我的 hack。 Windows 7 仍然可以正常工作,而创意更新之前的 Windows 10 可以正常工作。我最终执行了以下操作,以使一切在我上面提到的 Windows 版本中再次正常工作。
oFileDlg.fileName = sFname & ".00"
oFileDlg.ShowSave
fullName = Left$(oFileDlg.fileName, Len(oFileDlg.fileName) - 3)
【问题讨论】:
-
ThisApplication.CreateFileDialog(oFileDlg)是做什么的? -
来自 Autodesk Inventor 2012 COM API 参考:“创建新 FileDialog 对象的方法。FileDialog 对象类似于 Microsoft 常用对话框控件,允许您重复使用 Inventor 打开保存对话框。”