vba 如何在宏编程中获取当前目录名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13068090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current directory name in macros programming?
提问by Mohit
I need to know how can i get current directory variable in macros programming. I am saving a file on desktop like this : C:\Users\deadlock\Desktop\data.html. Is it possible to replace the C:\Users\deadlock\Desktop\ with the current directly variable?
我需要知道如何在宏编程中获取当前目录变量。我正在像这样在桌面上保存一个文件:C:\Users\deadlock\Desktop\data.html。是否可以用当前的直接变量替换 C:\Users\deadlock\Desktop\ ?
Here is my small piece of code:
这是我的一小段代码:
ActiveWorkbook.ShowPivotChartActiveFields = True
With ActiveWorkbook.PublishObjects.Add(xlSourceSheet, _
"C:\Users\deadlock\Desktop\data.htm", "Sheet1", "", xlHtmlStatic, "data_9438", "")
.Publish (True)
.AutoRepublish = False
Any Code-snippet will highly be appreciated.
任何代码片段都将受到高度赞赏。
Thanks in advance..
提前致谢..
回答by Alex K.
The current directory is available via VBA's curdir()
& the directory of the current workbook via ActiveWorkbook.Path
.
当前目录可通过 VBAcurdir()
和当前工作簿的目录通过ActiveWorkbook.Path
.
Edit;
编辑;
Dim current As String
current = CurDir$()
'// root dirs have a \ others do not; normalize
If Right$(current, 1) <> "\" Then current = current & "\"
With ActiveWorkbook.PublishObjects.Add(xlSourceSheet, _
current & "data.htm", "Sheet1", "", xlHtmlStatic, "data_9438", "")
.Publish (True)
....
回答by Siddharth Rout
Is is possible to replace the C:\Users\deadlock\Desktop\ with the current directly variable?
是否可以用当前的直接变量替换 C:\Users\deadlock\Desktop\?
Is this what you are trying?
这是你正在尝试的吗?
Option Explicit
Sub Sample()
Dim strPath As String
Dim Myar As Variant
strPath = "C:\Users\deadlock\Desktop\data.htm"
Myar = Split(strPath, "\")
'~~> This would give you something like
'~~> C:\Users\Siddharth Rout\Documents\data.htm
Debug.Print CurDir & "\" & Myar(UBound(Myar))
End Sub