Solución:
Utilice FSO para crear el archivo y escribir en él.
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
Vea la documentación aquí:
- http://technet.microsoft.com/en-us/library/ee198742.aspx
- http://technet.microsoft.com/en-us/library/ee198716.aspx
Open ThisWorkbook.Path & "template.txt" For Output As #1
Print #1, strContent
Close #1
Más información:
- Documentos de Microsoft:
Open
declaración - Documentos de Microsoft:
Print #
declaración - Documentos de Microsoft:
Close
declaración - wellsr.com: VBA escribe en un archivo de texto con
Print
Declaración - Apoyo administrativo :
Workbook.Path
propiedad
Para desarrollar la respuesta de Ben:
Si agrega una referencia a Microsoft Scripting Runtime
y escribe correctamente la variable fso usted puede aprovechar el autocompletado (Intellisense) y descubre las otras fantásticas funciones de FileSystemObject
.
Aquí hay un módulo de ejemplo completo:
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:tempMyTestFile.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
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)