Results 1 to 3 of 3

Thread: VBA code to create n number of txt/doc/xls files with specific size.

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    6

    VBA code to create n number of txt/doc/xls files with specific size.

    Hi all,,,,,,,,,

    Plzzzzzzz help me with the vb code to create n number of files with specific size.

    I have a code which would create text/word/excel files .....But I tried this code to create 'n' number of files....

    To be clear.....I need to create 1000 txt files with the content as 'Hello World1' 'Hello word2'.............'Hello World1000' respectively..........

    And If i specify the size of the file in a text box then it should create the file with that size.....

    But I couldn't make it...................help me this out..........

    The following is the code ........

    Public Sub veeru()

    Const FILENAME = "C:\Users\veeru\Desktop\myfile.xls"

    Dim My_filenumber As Integer

    My_filenumber = FreeFile

    Open FILENAME For Output As #My_filenumber

    Write #My_filenumber, "Hello"

    Close #My_filenumber End Sub



  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: VBA code to create n number of txt/doc/xls files with specific size.

    Have a look into For..Next loops
    The Write line could look like this:

    Code:
    Write #My_filenumber, "Hello World "  & Cstr(i)
    With i beeing the Looping Variable.

    BTW This is the VB Forum, for VBA questions, the Office Development section is suited better!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA code to create n number of txt/doc/xls files with specific size.

    Const FILENAME = "C:\Users\veeru\Desktop\myfile.xls"
    why would you create text files with .xls extension?

    to make text files of specific size you would need to pad the string written to the file with some character or spaces, to make the difference between the string size and the desired file size, using either the string or space methods
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width