Isaac
Lifelong Learner
- Local time
- Today, 01:03
- Joined
- Mar 14, 2017
- Messages
- 10,896
note - cross posted on stack overflow
I have some code that is giving me an unexpected result. And there IS something 'different' about the end-user this is happening to - she recently was a recipient of my company's modernization initiative, which involved being sent a new Microsoft Surface laptop (with win 10 of course) that supposedly is 'better compatible with Office 365' the way we are going to be using it.
Ok so here is what happens. I create a Scripting.Filesystemobject. I set an object variable (meant to be a Textstream), by fso.createtextfile. When stepping through the code one line at a time, I can see the file (filename.vbs) being created. (When the CreateTextFile line executes, I watch the folder - Bam, there it is in the folder). When the line executes to populate it with some text, that works fine. But as soon as the Textstream.CLOSE line operates--which, normally, is the point at which you have a saved, stable file in the folder--as that line executes, the file just disappears. Gone.
I wondered if it was a weird permissions, group policy thing. Because I was using %appdata% location. So I tested can she manually create a text file in that location? Yep. Can she manually create a VBS file, and run it, in that location? Yep.
I've tried both %appdata% and %documents%. It happens to both locations.
This code is in an Excel workbook beforeclose event, but I put this in the Access VBA thread because it's pure VBA really, and the problem has nothing to do with Excel, and the Excel forum gets less traffic.
Again, note:
1) I've tried both %appdata% and %documents%
2) She can manually create text files in both locations - AND manually create AND execute vbs files, I've tested it live
3) It works for 2 other people who have been testing it, using various excel versions, but not on the newly deployed laptop from I.T.
I've seen group policies before that limited VBS files, but not like this. In those cases it was clear: You couldn't run them, wscript and cscript were disabled. Here, we can create them and run them manually, but Textstream.Close makes them disappear.
My money is on either anti-virus crap or GPO, but I could be wrong, does anyone have any insight or what might be going on?
I have some code that is giving me an unexpected result. And there IS something 'different' about the end-user this is happening to - she recently was a recipient of my company's modernization initiative, which involved being sent a new Microsoft Surface laptop (with win 10 of course) that supposedly is 'better compatible with Office 365' the way we are going to be using it.
Ok so here is what happens. I create a Scripting.Filesystemobject. I set an object variable (meant to be a Textstream), by fso.createtextfile. When stepping through the code one line at a time, I can see the file (filename.vbs) being created. (When the CreateTextFile line executes, I watch the folder - Bam, there it is in the folder). When the line executes to populate it with some text, that works fine. But as soon as the Textstream.CLOSE line operates--which, normally, is the point at which you have a saved, stable file in the folder--as that line executes, the file just disappears. Gone.
I wondered if it was a weird permissions, group policy thing. Because I was using %appdata% location. So I tested can she manually create a text file in that location? Yep. Can she manually create a VBS file, and run it, in that location? Yep.
I've tried both %appdata% and %documents%. It happens to both locations.
This code is in an Excel workbook beforeclose event, but I put this in the Access VBA thread because it's pure VBA really, and the problem has nothing to do with Excel, and the Excel forum gets less traffic.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'initiate full backup vbs script:
Dim ts As Object, fso As Object, strScriptText As String, strScriptPath As String
'populate our variable with the full text of the script: found on QLoader in this range:
strScriptText = ThisWorkbook.Worksheets("QLoader").Range("z_BackupScriptText").Value
'replace the text "placeholder" with this workbook's actual full path/name so the vbs can do a file copy backup:
strScriptText = Replace(strScriptText, "placeholder", ThisWorkbook.FullName)
'fire up FSO:
Set fso = CreateObject("scripting.filesystemobject")
'determine the new VBS file's path
strScriptPath = Environ("AppData") & "\Filename_" & Format(Now, "yymmddhhmmss") & ".vbs"
'create our textstream object:
Set ts = fso.createtextfile(strScriptPath) 'this works fine--once executes, I can see the file in the folder
'write our script into it
ts.write strScriptText 'this works fine
'save and close it
ts.Close 'when this line executes, the file DISAPPEARS*****
'GO:
Shell "wscript " & strScriptPath, vbNormalFocus 'and by this point, of course, the path/file is not found, because the file disappeared during the last line execute
End Sub
Again, note:
1) I've tried both %appdata% and %documents%
2) She can manually create text files in both locations - AND manually create AND execute vbs files, I've tested it live
3) It works for 2 other people who have been testing it, using various excel versions, but not on the newly deployed laptop from I.T.
I've seen group policies before that limited VBS files, but not like this. In those cases it was clear: You couldn't run them, wscript and cscript were disabled. Here, we can create them and run them manually, but Textstream.Close makes them disappear.
My money is on either anti-virus crap or GPO, but I could be wrong, does anyone have any insight or what might be going on?
Last edited: