Saving file to C:\Windows\system folder

pingme89

Board Regular
Joined
Jan 23, 2014
Messages
176
Office Version
  1. 365
Platform
  1. Windows
I have the following code to write a file to the Windows System folder.
I know I need Administrator rights to do so which I do. Is there a way in VBA to force it to write since the Logged in User has Administrator rights? If so, how can I do it?

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")


Dim Fileout As Object
Set Fileout = fso.CreateTextFile("C:\Windows\system\vba.txt", True, True)
Fileout.Write "Text"
Fileout.Close
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I'd try running Excel as administrator.

I'm not sure what you are attempting to do but I thought I would point out that it you are trying to save certain strings for all users to use on the computer you could use VBA.SaveSetting to write to a safe place in the registry.
 
Upvote 0
I'd try running Excel as administrator.

I'm not sure what you are attempting to do but I thought I would point out that it you are trying to save certain strings for all users to use on the computer you could use VBA.SaveSetting to write to a safe place in the registry.

To be specific, I want to be able to embed a date of expiry somewhere on the user's PC. I had never thought about the registry as I thought that would be more difficult. However, I think if it were possible, placing a date in the registry would be perfect.
Can you give me some advice as to how this could be accomplished?

I have one Excel Spreadsheet that I need to timebomb. I want to create a 2nd Excel Spreadsheet that would embed a date onto the PC (You suggestion of the Registry would be perfect). A copy of the Timebombed Spreadsheet would be placed on the User's PC. This spreadsheet would make sure the date would not surpass the Expiry date in the Registry. The Timebombed Spreadsheet would take the correct date from over the Internet so even changing the Local PC's date would not affect the Timebomb.
I am not very familiar with the Registry and would not know how to endeavor this. Any advice or code would be much appreciated.
 
Upvote 0
Oh, when you said "Timebomb" I realized that I had been answering a different question of yours. Didn't even pay attention to the user name. Hope you don't think I'm stalking you! :)

I am actually wrong on this one. VBA.SaveSettings will save files at the Local User level and not the computer level like I had believed. To answer your question, you actually only need to use the functions in VBA to edit the registry, there isn't anything more too it you have to worry about. I'll put some links to MSDN so you can see the documentation on the registry editing functions below but since this doesn't help you with having a global setting that all users can access lets move on.

VBA.Interaction.SaveSetting: https://msdn.microsoft.com/en-us/library/office/gg278814
VBA.Interaction.GetSetting: https://msdn.microsoft.com/en-us/library/office/gg278430
VBA.Interaction.DeleteSetting: https://msdn.microsoft.com/en-us/library/office/gg278763
VBA.Interaction.GetAllSettings: https://msdn.microsoft.com/en-us/library/office/gg278860

Could you add a folder to the C:\ Drive that only the users running the GetInternetTime macro would be able to edit? That might keep others from getting in.
 
Upvote 0
I am hesitant about putting a folder in the root of C: since it is the USER I am trying to prevent from altering the date of Expiry. The main spreadsheet would compare the GetInternetTime with the expiry date. If the Expiry date could be placed in the Registry, it would be much more difficult for the average person to attempt to edit the Registry. It doesn't need to be restricted to any user. I just need to hide it well in the Registry.

Any ideas?
 
Upvote 0
I've never edited the registry outside of the Methods I listed above, so I couldn't be of help there. I'm sure there is a Windows API you could hook into if you really wanted to go that route. I can't really recommend a good place to edit the registry at other than somewhere on the computer level. Just make sure you are being really careful in your code since editing the registry can make your computer unusable depending on what gets altered.
 
Upvote 0
I have managed to make the Timebomb work perfectly. Thank you.
I used the SaveSettings command to save to the registry.

There is only ONE problem. On a Windows 10 PC with Excel 2010, this work perfectly. However, on Windows 7 with Excel 2010, Excel freezes upon opening. I am guessing but the Function GetInternetTime() might be causing the issue. Can you imagine why this might be?
 
Upvote 0
Not really, does GetInternetTime hang when you run it by itself instead of in the open workbook event?
 
Upvote 0
Yes. It is only the GetInternetTime function that hangs.

I tested this by by the following code:



Public ExpiryDate As String
Public RealDate As Date





***************************************************************************************************************




Private Sub Label1_Click()


End Sub


Private Sub CommandButton1_Click()
RealDate = GetGoogleDateTimeGmt()
MsgBox "Today's date is : " & RealDate


End Sub


Private Sub CommandButton2_Click()


ExpiryDate = GetSetting("PITB", "User Settings", "Expiry")
MsgBox "License Expiry date is: " & ExpiryDate


End Sub


Private Sub CommandButton3_Click()


End Sub


Private Sub ExitProgram_Click()
Application.Close


End Sub










***************************************************************************************************************

I made a userform with 3 buttons. Clicking on commandbutton1 Excel hangs.
Clicking on commandbutton2 gives me the stored date in the Registry and it is fine and displays the msgbox.
 
Upvote 0

Forum statistics

Threads
1,223,636
Messages
6,173,483
Members
452,516
Latest member
archcalx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top