Supercharge your PowerPoint productivity with
Supercharge your PPT Productivity with PPTools - Click here to learn more.

Proud member of

PPTools

Image Export converts PowerPoint slides to high-quality images.

PPT2HTML exports HTML even from PowerPoint 2010 and 2013, gives you full control of PowerPoint HTML output, helps meet Section 508 accessibility requirements

Merge Excel data into PowerPoint presentations to create certificates, awards presentations, personalized presentations and more

Resize your presentations quickly and without distortion

Language Selector switches the text in your presentation from one language to another

FixLinks prevents broken links when you distribute PowerPoint presentations

Shape Styles brings styles to PowerPoint. Apply complex formatting with a single click.

General-purpose routine for writing a string to a text file

A number of the other routines here collect information into a string variable then display it as a messagebox.

That's handy for quickies, but what if you want a more permanent record of the info your macro has collected?

Write it to file! These two subroutines accept a filename and a string and writes the string out to the file, and then open the file for editing/viewing/printing in Notepad.

Sub WriteStringToFile(pFileName as String, pString as String)

	Dim intFileNum as Integer
	
	intFileNum = FreeFile
                ' change Output to Append if you want to add to an existing file
                ' rather than creating a new file each time
	Open pFilename For Output As intFileNum
	Print #intFileNum, pString
	Close intFileNum

End Sub

Sub SendFileToNotePad(pFileName as String)
	Dim lngReturn as Long

	lngReturn = Shell("NOTEPAD.EXE " & pFileName, vbNormalFocus)

End Sub

So for example, you could do this:

Sub Test()
	' Create a test string
	Dim strTestString as String
	strTestString = "Now is the time for, alas, poor Yorick, to Dunsinane woods to come.  With gibberish like this." 
	
	' And a name for the file we're going to write
	Dim strTestFile as String
	strTestFile = "C:\TEMP\TESTFILE.TXT"	' be sure you have a C:\TEMP folder or it won't work

	' Write it to a file
	Call WriteStringToFile(strTestFile, strTestString)

	' and now let's see the file
	Call SendFileToNotePad(strTestFile)

End Sub

See How do I use VBA code in PowerPoint? to learn how to use this example code.


Did this solve your problem? If so, please consider supporting the PPT FAQ with a small PayPal donation.
Page copy protected against web site content infringement by Copyscape Contents © 1995 - 2022 Stephen Rindsberg, Rindsberg Photography, Inc. and members of the MS PowerPoint MVP team. You may link to this page but any form of unauthorized reproduction of this page's contents is expressly forbidden.

Supercharge your PPT Productivity with PPTools

content authoring & site maintenance by
Friday, the automatic faq maker (logo)
Friday - The Automatic FAQ Maker

General-purpose routine for writing a string to a text file
http://www.pptfaq.com/FAQ00514_General-purpose_routine_for_writing_a_string_to_a_text_file.htm
Last update 07 June, 2011
Created: