如何设置由macros创build的文件的位置

我正在通过macros将Excel文件转换为文本文件,我希望文本文件的位置与Excel工作表位置相同。

我的代码是:

Dim strPath As String strPath = "MyFileName.dat" Dim fnum As Integer fnum = FreeFile() Open strPath For Output As #fnum 'my code Close #fnum 

运行时总是进入文档。 我试过“../MyFileName.dat”,它与我尝试把excel工作表中的一些地点,但没有与所有的工作。

什么是正确的方法来做到这一点。 谢谢。

假设有问题的工作簿是ActiveWorkbook,这将工作。 它使用FullName获取工作簿的完整path,并为工作簿的数据文件名称进行replace:

 Sub test() Dim wb As Excel.Workbook Dim strPath As String Set wb = ActiveWorkbook strPath = Replace(wb.FullName, wb.Name, "MyFileName.dat") Dim fnum As Integer fnum = FreeFile() Open strPath For Output As #fnum 'my code Close #fnum End Sub 
Interesting Posts