1

I have most of the codes but cannot run it. Some basic info is that my username is nywongab, I want it as a text file and the text file placed on the desktop. Thanks

Sub createsth()        
    Dim abbyFilesystem As filesystemobject    
    Dim abbyfile As File

    Set abbyFilesystem = CreateObject("scripting.filesystemobject")   
    Set abbyfile =_
        abbyFilesystem.createtextfile("C:\Users\nywongab\Desktop"_ 
        & ".txt")_

    Dim result As String
    result = "A"

    abbyfile.write (result)
End Sub
2
  • What is your issue exactly? Commented May 26, 2019 at 19:40
  • First issue: have you added the required reference? Second issue: your file path\name is incomplete Commented May 26, 2019 at 20:00

1 Answer 1

1

Not quite clear the question.. though from the subject you have a problem with writing to file. From the code is obviously not working, but you were close.

If you are trying to get the username or the desktop path more dynamically, you can use Environ like so:

Sub createsth()

Dim pathDesktop As String
pathDesktop = Environ("USERPROFILE") & "\Desktop\"

Dim pathSave As String
pathSave = pathDesktop & Environ("USERNAME") & ".txt"

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

Dim abbyFile As Object
Set abbyFile = abbyFileSystem.CreateTextFile(pathSave)

Dim result As String
result = "A"

abbyFile.Write result
abbyFile.Close

End Sub
Sign up to request clarification or add additional context in comments.

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.