vba vba上的用户配置文件环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42091960/
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
UserProfile environ on vba
提问by Alex
First I'm new to vba coding . I wrote a form in Access 2013 - VBA and created a function that generates a PDF/txt document on button click, thing is that boss wants it to save on a shared folder that′s located on %userprofile%
path - like C:\Users\<username>\folder
and we have a lot of users.
首先,我是 vba 编码的新手。我在 Access 2013 - VBA 中编写了一个表单,并创建了一个在按钮单击时生成 PDF/txt 文档的函数,问题是老板希望它保存在位于%userprofile%
路径上的共享文件夹中- 就像C:\Users\<username>\folder
我们有很多用户。
How can I add to the path of the SaveAs2 that i′m using without having to hardcode to each user?
如何添加到我正在使用的 SaveAs2 的路径中,而不必对每个用户进行硬编码?
Code is like:
代码是这样的:
file.SaveAs2 = ("C:\Users\username\folder\filename.pdf")
I tried defining code like:
我尝试定义如下代码:
Dim filepath as string
filepath = environ("USERPROFILE")
and then:
进而:
file.saveas2 = (filepath &"\folder\filename.pdf")
but still no success.
但仍然没有成功。
Thanks for any help
谢谢你的帮助
回答by RyanL
Sounds like you're trying to return the username of the logged in user?
听起来您正在尝试返回登录用户的用户名?
Add a module, insert this code:
添加一个模块,插入以下代码:
Option Compare Database
Declare Function wu_GetUserName Lib "advapi32" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long
Function NetworkUser() As String
Dim lngStringLength As Long
Dim sString As String * 255
lngStringLength = Len(sString)
sString = String$(lngStringLength, 0)
If wu_GetUserName(sString, lngStringLength) Then
NetworkUser = Left$(sString, InStr(sString, Chr(0)) - 1)
Else
NetworkUser = "Unknown"
End If
End Function
Then if you want to return the network user, try something like this:
然后,如果您想返回网络用户,请尝试以下操作:
filepath = = "C:\Users\" & networkuser() & "\folder\filename.pdf"
If you want to return the 'My Documents' folder, you could use something similar to what you were attempting above. This is wrapped in a function.
如果您想返回“我的文档”文件夹,您可以使用类似于上面尝试的内容。这被包装在一个函数中。
Public Function MyDocsPath() As String
MyDocsPath = Environ$("USERPROFILE") & "\My Documents"
End Function
Then call it.
然后调用它。
filepath = MyDocsPath & \filename.pdf")