VBS将自身复制到启动文件夹中

时间:2017-03-07 21:25:35

标签: vba vbscript

这是我的代码: 我想帮助我的朋友。 大多数代码都有效,但我不知道如何使代码的第一部分获得该人的用户名。 (我的用户名)

   set wshShell  = CreateObject("Wscript.Shell")

   sSourceFile   = "C:\My UserName\Downloads\Word\Word.VBS"
   sTargetFolder = "C:\My UserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

   sCmd = "%comspec% /c copy """ & sSourceFile & """ """ & sTargetFolder & """ /Y"

   wshShell.Run sCmd, 0, True


   Set objWord = CreateObject("Word.Application")

   objWord.Visible = True
   Set objDoc = objWord.Documents.Add()
   Set objSelection = objWord.Selection

   objSelection.Font.Name = "Calibri"
   objSelection.Font.Size = "9"
   objSelection.TypeText "===================================================================================================="
   objSelection.TypeParagraph()

   objSelection.Font.Name = "Calibri"
   objSelection.Font.Size = "12"
   objSelection.TypeText "___________________________________________________________________________"
   objSelection.TypeParagraph()

   objSelection.Font.Name = "Algerian"
   objSelection.Font.Size = "77"
   objSelection.TypeText "EQUACHALK"
   objSelection.TypeParagraph()

   objSelection.Font.Name = "Calibri"
   objSelection.Font.Size = "12"
   objSelection.TypeText "--------------------------------------------------------------------------------------------------------------------------"
   objSelection.TypeParagraph()

   objSelection.Font.Name = "Calibri"
   objSelection.Font.Size = "12"
   objSelection.TypeText "--------------------------------------------------------------------------------------------------------------------------"
   objSelection.TypeParagraph()

   objSelection.Font.Name = "Algerian"
   objSelection.Font.Size = "49"
   objSelection.TypeText "YOU CAN'T STOP ME"
   objSelection.TypeParagraph()

   objSelection.Font.Name = "Calibri"
   objSelection.Font.Size = "12"
   objSelection.TypeText "___________________________________________________________________________"
   objSelection.TypeParagraph()

   objSelection.Font.Name = "Calibri"
   objSelection.Font.Size = "9"
   objSelection.TypeText "===================================================================================================="
   objSelection.TypeParagraph()

如何将此代码复制到用户计算机的“开始”菜单中?

谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

Application.UserName将为您提供用户在安装Office套件时输入的名称,但这可能不是您想要的。

Environ$("UserName")将从Windows环境变量中获取用户名。如果设置了。

这个更长,更复杂,但也会返回用户名(由微软提供的示例)。

 ' 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

   Sub 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

      ' Display the name of the person logged on to the machine.
      MsgBox "The person logged on this machine is: " & lpUserName

   End Sub

获得用户ID后,您可以继续处理其余部分。我并不完全确定你的意思"将此代码复制到开始菜单中#34;。您无法将VBA代码放在要执行的文件中。它需要附加到某种Office主机应用程序。

如果要在开始菜单中插入Word文档(或其快捷方式),可以在创建开始菜单快捷方式时进行一些搜索,如果碰到墙壁则询问有关该文档的问题。

答案 1 :(得分:0)

Set wshShell = CreateObject( "WScript.Shell" )

strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

WScript.Echo "User Name: " & strUserName

此代码可以帮助您获取发送给谁的用户名或其他内容。根据你的代码判断,你想把它放在开头,而不是我的用户名,你可以把strUserName放在那里。