0

Basically, I want to create a new file and write in it in a directory on the PC, pointed to by the %TEMP% variable. However, the revised code below does not work:

Dim oFile
Dim shell
Set oShell = CreateObject("WScript.Shell")
user = oShell.ExpandEnvironmentStrings("%Temp%")
Set oFile = CreateObject("Wscript.Shell")
Set oFile = oFile.CreateTextFile("%Temp%\d.txt")
oFile.WriteLine "here is my contant"
oFile.Close

Error Message:

run time error
line no: 3
object required

Old Code

Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
FileName = "%TEMP%\myfile.txt"
Set tf = fso.CreateTextFile(FileName, True)

If I use the file name "C:\myfile.txt" it works fine.

Error Message:

Path not found

1

2 Answers 2

2

In VBA, you can just use Environ("TEMP") to expand the Environment variable - if this does not work in VBScript, you may need to bind the WScript.Shell object and use the ExpandEnvironmentStrings property instead, like so:

Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%TEMP%") & "\myfile.txt"
Set oShell = Nothing

Following from comments below

Here is a "fully fixed" code:

'Declare variables/objects first
Dim fso AS Object, oFile AS Object
Dim oShell AS Object, FileName AS String

'This bit turns "%TEMP%" into a real file path
Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%Temp%\d.txt")
Set oShell = Nothing 'Tidy up the Objects we no longer need

'This bit creates the file
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(FileName)
oFile.WriteLine "here is my content"
oFile.Close
Set oFile = Nothing 'Tidy up the Objects we no longer need
Set fso = Nothing 'Tidy up the Objects we no longer need
Sign up to request clarification or add additional context in comments.

9 Comments

oShell.ExpandEnvironmentStrings("%TEMP%\myfile.txt")
sir not working :Dim oShell,tf Set oShell = CreateObject("WScript.Shell") FileName = oShell.ExpandEnvironmentStrings("%TEMP%")& "\myfile.txt" Set tf = oShell.CreateTextFile(FileName, True)
@minikumari "Doesn't work" is not a valid problem description. How exactly does your modified attempt "not work"? Does it still give you an error? A different one? Please edit your question.
@minikumari If you use a MsgBox to display FileName after you set it, does it display a valid path (navigate there manually?) Also, do you actually have permissions to view/edit the folder, or is it restricted?
Dim oFile Dim shell Set oShell = CreateObject("WScript.Shell") user = oShell.ExpandEnvironmentStrings("%Temp%") Set oFile = CreateObject("Wscript.Shell") Set oFile = oFile.CreateTextFile("%Temp%\d.txt") oFile.WriteLine "here is my contant" oFile.Close error in line no: 3 object required.
|
-1

you can use Environ("temp") to write to C:\Users\[username]\AppData\Local\Temp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.