louis.b
louis.b

Reputation: 11

Export string values to .txt file

I begin in VBA and for my job, I have to analyse excel sheets and export my resume to .txt file.

I was able to create the name of my saving file ("NomFichier") - Yes, it's french ;) - and Also my path by choosing the emplacement of the saving file ("Emplacement")

During my analyse, i stock my values into a string ("Hor (30)")

I am now searching ways to export the values of the string to .txt file using the path and name of the file...Can somebody help me ?

Here is my code for the name of the file and the finding path processus :

   'Nom du tableau deviens nom du fichier .txt
   'Name of the file to be save

    Dim NomFichier As String
    NomFichier = Tableau_X & "_" & Titre
    
    'Emplacement du fichier txt à enregistrer
    'Location of the file to be save

    Dim Emplacement As String
    Dim diaFolder As FileDialog

    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    diaFolder.Show
    Emplacement = diaFolder.SelectedItems(1)
    Set diaFolder = Nothing

"..."

Upvotes: 1

Views: 1020

Answers (2)

AAron Leckinger
AAron Leckinger

Reputation: 1

I always put the string into a local Access table and use DoCmd.TransferText...

Upvotes: 0

maca2kx
maca2kx

Reputation: 15

To write to a text file you need the file path (which you have) and the next available file number, which is easy to get. Then you 'open' the file number for modification.

Dim fileNum As Integer

fileNum = FreeFile

Open Emplacement For Output As fileNum
Print #fileNum, "write your information here"
Close fileNum

You can use as many Print statements as you need or use an array together with a loop or a variety of other ways to add your data. The #fileNum refers to the file you're modifying with the Print statement and the next bit can be a variable or hard coded.

This code creates a new file but you can use Input to read from a file and Append to add onto an existing file. You can read more on ways to interact with files here: https://www.thespreadsheetguru.com/blog/vba-guide-text-files

Upvotes: 1

Related Questions