132 votes

Comment créer et écrire dans un fichier txt en utilisant VBA

J'ai un fichier qui est ajouté ou modifié manuellement en fonction des entrées. Comme la plupart des contenus sont répétitifs dans ce fichier, seules les valeurs hexadécimales changent, je veux en faire un fichier généré par un outil.

Je veux écrire les codes C qui vont être imprimés dans ce .txt fichier.

Quelle est la commande pour créer un .txt en utilisant VBA, et comment écrire dans le fichier

188voto

Ben Points 14995

Utilisez FSO pour créer le fichier et y écrire.

Dim fso as Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile as Object
Set oFile = FSO.CreateTextFile(strPath)
oFile.WriteLine "test" 
oFile.Close
Set fso = Nothing
Set oFile = Nothing    

Voir la documentation ici :

69voto

Bhanu Sinha Points 16
Open ThisWorkbook.Path & "\template.txt" For Output As #1
Print #1, strContent
Close #1

Plus d'informations :

64voto

Marcus Mangelsdorf Points 1020

Pour approfondir La réponse de Ben :

Si vous ajoutez une référence à Microsoft Scripting Runtime et tapez correctement la variable fso vous pouvez profiter de l'autocomplétion (Intellisense) et découvrez les autres fonctionnalités de l'application FileSystemObject .

Voici un exemple complet de module :

Option Explicit

' Go to Tools -> References... and check "Microsoft Scripting Runtime" to be able to use
' the FileSystemObject which has many useful features for handling files and folders
Public Sub SaveTextToFile()

    Dim filePath As String
    filePath = "C:\temp\MyTestFile.txt"

    ' The advantage of correctly typing fso as FileSystemObject is to make autocompletion
    ' (Intellisense) work, which helps you avoid typos and lets you discover other useful
    ' methods of the FileSystemObject
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    Dim fileStream As TextStream

    ' Here the actual file is created and opened for write access
    Set fileStream = fso.CreateTextFile(filePath)

    ' Write something to the file
    fileStream.WriteLine "something"

    ' Close it, so it is not locked anymore
    fileStream.Close

    ' Here is another great method of the FileSystemObject that checks if a file exists
    If fso.FileExists(filePath) Then
        MsgBox "Yay! The file was created! :D"
    End If

    ' Explicitly setting objects to Nothing should not be necessary in most cases, but if
    ' you're writing macros for Microsoft Access, you may want to uncomment the following
    ' two lines (see https://stackoverflow.com/a/517202/2822719 for details):
    'Set fileStream = Nothing
    'Set fso = Nothing

End Sub

34voto

user1704282 Points 4

D'une manière simple et sans trop de redondance.

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

    Dim Fileout As Object
    Set Fileout = fso.CreateTextFile("C:\your_path\vba.txt", True, True)
    Fileout.Write "your string goes here"
    Fileout.Close

-11voto

Zack Brightman Points 1
Dim SaveVar As Object

Sub Main()

    Console.WriteLine("Enter Text")

    Console.WriteLine("")

    SaveVar = Console.ReadLine

    My.Computer.FileSystem.WriteAllText("N:\A-Level Computing\2017!\PPE\SaveFile\SaveData.txt", "Text: " & SaveVar & ", ", True)

    Console.WriteLine("")

    Console.WriteLine("File Saved")

    Console.WriteLine("")

    Console.WriteLine(My.Computer.FileSystem.ReadAllText("N:\A-Level Computing\2017!\PPE\SaveFile\SaveData.txt"))
    Console.ReadLine()

End Sub()

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X