Advertisement
Advertisement
| 06.11.2008 at 08:29PM PDT, ID: 23478243 |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: |
Sub Run_As_Administrator()
ActiveWorkbook.Save
' Source: http://www.cpearson.com/excel/vbe.aspx
' You must have a Reference as follows:
' Tools --> References --> Microsoft Visual Basic for Applications Extensibility 5.3
' You also need to enable programmatic access to the VBA Project.
' In Excel 2003 and earlier, go the Tools menu (in Excel, not in the VBA editor),
' choose Macros and then the Security tab.
' In that dialog, click on the Trusted Publishers tab and check the
' "Trust access to the Visual Basic Project" setting
' Lastly, you must add UserForm object to the workbook, called UserForm1
Dim TempForm As VBComponent
Dim LabelHeading As MSForms.Label
Dim LabelUsername As MSForms.Label
Dim TextBoxUsername As MSForms.TextBox
Dim LabelPassword As MSForms.Label
Dim TextBoxPassword As MSForms.TextBox
Dim CommandOK As MSForms.CommandButton
Dim CommandCancel As MSForms.CommandButton
' Hide VBE window to prevent screen flashing
Application.VBE.MainWindow.Visible = False
' Create the UserForm
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
TempForm.Properties("Width") = 300
TempForm.Properties("Height") = 200
TempForm.Properties("Caption") = "Re-Open This Workbook With Alternate Credentials"
' Add the Labels and Textboxes
Set LabelHeading = TempForm.Designer.Controls.Add("forms.Label.1")
With LabelHeading
.Caption = "Re-Open This Workbook As Alternate User"
.Height = 15
.Width = 380
.Left = 10
.Top = 5
End With
Set LabelUsername = TempForm.Designer.Controls.Add("forms.Label.1")
With LabelUsername
.Caption = "Username: "
.Height = 15
.Width = 100
.Left = 10
.Top = 25
End With
Set LabelPassword = TempForm.Designer.Controls.Add("forms.Label.1")
With LabelPassword
.Caption = "Password: "
.Height = 15
.Width = 100
.Left = 10
.Top = 45
End With
Set TextBoxUsername = TempForm.Designer.Controls.Add("forms.TextBox.1")
With TextBoxUsername
.Text = "development/Administrator"
.Height = 15
.Width = 100
.Left = 120
.Top = 25
End With
Set TextBoxPassword = TempForm.Designer.Controls.Add("forms.TextBox.1")
With TextBoxPassword
.Height = 15
.Width = 100
.Left = 120
.Top = 45
.PasswordChar = "*"
End With
' Add the Cancel button
Set CommandCancel = TempForm.Designer.Controls.Add("forms.CommandButton.1")
With CommandCancel
.Caption = "Cancel"
.Height = 18
.Width = 44
.Left = 30
.Top = 70
End With
' Add the OK button
Set CommandOK = TempForm.Designer.Controls.Add("forms.CommandButton.1")
With CommandOK
.Caption = "OK"
.Height = 18
.Width = 44
.Left = 110
.Top = 70
.Default = True
End With
' Add event-hander subs for the CommandButtons
With TempForm.CodeModule
x = .CountOfLines
.InsertLines x + 1, "Sub CommandButton1_Click()"
.InsertLines x + 2, " Unload Me"
.InsertLines x + 3, "End Sub"
.InsertLines x + 4, "Sub CommandButton2_Click()"
.InsertLines x + 5, " Set objFSO = CreateObject(""Scripting.FileSystemObject"")"
.InsertLines x + 6, " Set objNetwork = CreateObject(""WScript.Network"")"
.InsertLines x + 7, " strComputerName = objNetwork.ComputerName"
.InsertLines x + 8, " strPSExecPath = ThisWorkbook.Path & ""\PSexec.exe"""
.InsertLines x + 9, " strPSExecPath = objFSO.GetFile(strPSExecPath).ShortPath"
.InsertLines x + 10, " strExcelPath = objFSO.GetFile(Application.Path & ""\Excel.exe"").ShortPath"
.InsertLines x + 11, " strExcelFilePath = objFSO.GetFile(ActiveWorkbook.Path & ""\"" & ActiveWorkbook.Name).ShortPath"
.InsertLines x + 12, " strUserName = TextBox1.Text"
.InsertLines x + 13, " strPassword = TextBox2.Text"
.InsertLines x + 14, "' MsgBox ""cmd /k ping localhost -n 3 >nul && "" & strPSExecPath & "" -accepteula -d -i -u "" & strUserName & "" -p "" & strPassword & "" \\"" & strComputerName & "" "" & strExcelPath & "" \"""""" & strExcelFilePath & ""\"""""""""
.InsertLines x + 15, " Shell ""cmd /k ping localhost -n 3 >nul && "" & strPSExecPath & "" -accepteula -d -i -u "" & strUserName & "" -p "" & strPassword & "" \\"" & strComputerName & "" "" & strExcelPath & "" \"""""" & strExcelFilePath & ""\"""""", vbShowNormal"
.InsertLines x + 16, "' ActiveWorkbook.Close False"
.InsertLines x + 17, " ActiveWorkbook.Saved = True"
.InsertLines x + 18, " Application.Quit"
.InsertLines x + 19, "' Unload Me"
.InsertLines x + 20, "End Sub"
End With
' Show the form
VBA.UserForms.Add(TempForm.Name).Show
' Delete the form
ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=TempForm
End Sub
|