2017-09-01 91 views
0

這是我正面臨的問題。用於訪問特定文件的代碼,但用戶的機器已設置

通常情況下,用戶的機器設置有在C:\Users\User name\Signature文件夾下記錄的用戶圖形簽名文件,作爲在某些Excel進程中使用它的安全和安全的位置。但並非所有用戶的簽名文件都可以使用,因爲下面的代碼並不總是正確報告文件夾路徑。由於配置文件重建,我有一些用戶在機器設置中使用了兩個不同配置文件,分別位於C:\Users\文件夾下 - 查看Excel正在使用的特定圖形文件的位置時導致頭痛。我附加了用於搜索正確文件夾的代碼示例。

因此,您能否向我提供有關必須設置什麼設置,必須執行哪些代碼更改的信息,以確保可靠地訪問圖形文件,但是用戶的配置文件是在機器上設置的?

------------------ 
Main Module 
ChDrive "C" 
strPictureFilePath = MyDocs() 
strPictureFileName = "MySignature.jpg" 
ActiveSheet.Shapes.AddPicture Filename:=(strPictureFilePath & strPictureFileName), linktofile:=msoFalse, _ 
    savewithdocument:=msoCTrue, Left:=162, Top:=445, Width:=170, Height:=35 
------------------ 
Sub Module 
Option Explicit 
    ' Declare for call to mpr.dll. 
    Declare Function WNetGetUser Lib "mpr.dll" _ 
     Alias "WNetGetUserA" (ByVal lpName As String, _ 
     ByVal lpUserName As String, lpnLength As Long) As Long 
    Const NoError = 0  'The Function call was successful 
    Function GetUserName() 
     ' Buffer size for the return string. 
     Const lpnLength As Integer = 255 
     ' Get return buffer space. 
     Dim status As Integer 
     ' For getting user information. 
     Dim lpName, lpUserName As String 
     ' Assign the buffer size constant to lpUserName. 
     lpUserName = Space$(lpnLength + 1) 
     ' Get the log-on name of the person using product. 
     status = WNetGetUser(lpName, lpUserName, lpnLength) 
     ' See whether error occurred. 
     If status = NoError Then 
     ' This line removes the null character. Strings in C are null- 
     ' terminated. Strings in Visual Basic are not null-terminated. 
     ' The null character must be removed from the C strings to be used 
     ' cleanly in Visual Basic. 
     lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1) 
     Else 
     ' An error occurred. 
     MsgBox "Unable to get the name." 
     End 
     End If 
     GetUserName = lpUserName 
    End Function 
'-------------------------------------------------------------------------- 
Function MyDocs() As String 
    Dim strStart As String 
    Dim strEnd As String 
    Dim strUser As String 

    strUser = GetUserName() 
    strStart = "C:\Users\" 
    strEnd = "\Signature\" 

    MyDocs = strStart & strUser & strEnd 
End Function 
'-------------------------------------------------------------------------- 

回答

1

您可以Environ()

Function MyDocs() As String 
    Dim strStart As String 
    Dim strEnd As String 

    strStart = Environ("USERPROFILE") 
    strEnd = "\Signature\" 

    MyDocs = strStart & strEnd 
End Function 
+0

它工作得很好了。謝謝 –

相關問題