Hi Guys,

the project im currently creating is a basic form with calculations, which all works great, my task now is to save the form data (labels, textbox data etc) to a word document, now i have achieved it to do this, but i had to specify the name of the word doc (as code shows below) does anyone know how i can get the save button to pick up on a certain bite of data of the form (lets pick the date) and save the word document to the date name (i.e 12.12.2012) instead of what i have put (invoice.doc) i have posted all of the code except the calculations (as they are not important for this task)
many thanks for taking a look guys :)
p.s i know there is alot of objwriter code, but i didnt know how else to pull to data from the textboxes etc and write them elsewhere (in this case word doc)

    Private Sub cmd_Save_Click(sender As System.Object, e As System.EventArgs) Handles cmd_Save.Click
        '****************************************************************************************'
        'Creating variables for a new Word Document to save the form data including the file path'
        '****************************************************************************************'
        Dim New_Doc As String = "C:\Users\ComputerFirstAde\Desktop\Invoice.doc"
        Dim ObjWriter As New System.IO.StreamWriter(New_Doc)


        '*********************************************************************************************'
        'Pulling out the data from all the labels and textboxes reading to go to the new word document'
        '*********************************************************************************************'
        ObjWriter.Write("                        ")
        ObjWriter.Write(lbl_Invoice_Heading.Text)
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.Write(lbl_invoice_number.Text)
        ObjWriter.Write(" : ")
        ObjWriter.Write(tbox_Inv_Number.Text)
        ObjWriter.Write("               ")
        ObjWriter.Write(lbl_Invoice_Date.Text)
        ObjWriter.Write(" : ")
        ObjWriter.Write(tbox_Inv_Date.Text)
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.Write(lbl_Comp_Name.Text)
        ObjWriter.Write(" : ")
        ObjWriter.Write(tbox_Comp_Name.Text)
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.Write(lbl_Add_Line_1.Text)
        ObjWriter.Write(" : ")
        ObjWriter.Write(tbox_Add_Line1.Text)
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.Write(lbl_Add_Line_2.Text)
        ObjWriter.Write(" : ")
        ObjWriter.Write(tbox_Add_Line2.Text)
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.Write(lbl_Type.Text)
        ObjWriter.Write("       ")
        ObjWriter.Write(lbl_Quantity.Text)
        ObjWriter.Write("       ")
        ObjWriter.Write(lbl_Price.Text)
        ObjWriter.Write("       ")
        ObjWriter.Write(lbl_Totals.Text)
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.Write(lbl_15.Text)
        ObjWriter.Write("      ")
        ObjWriter.Write(tbox_Qty_15.Text)
        ObjWriter.Write("                    ")
        ObjWriter.Write(tbox_Price_15.Text)
        ObjWriter.Write("                    ")
        ObjWriter.Write(tbox_Total_15.Text)
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.Write(lbl_17.Text)
        ObjWriter.Write("       ")
        ObjWriter.Write(tbox_Qty_17.Text)
        ObjWriter.Write("                    ")
        ObjWriter.Write(tbox_Price_17.Text)
        ObjWriter.Write("                    ")
        ObjWriter.Write(tbox_Total_17.Text)
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.Write(lbl_19.Text)
        ObjWriter.Write("       ")
        ObjWriter.Write(tbox_Qty_19.Text)
        ObjWriter.Write("                   ")
        ObjWriter.Write(tbox_Price_19.Text)
        ObjWriter.Write("                   ")
        ObjWriter.Write(tbox_Total_19.Text)
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.Write("                 ")
        ObjWriter.Write(lbl_SubTotal.Text)
        ObjWriter.Write("       ")
        ObjWriter.Write(tbox_SubTotal.Text)
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.Write("                 ")
        ObjWriter.Write(lbl_Tax.Text)
        ObjWriter.Write("       ")
        ObjWriter.Write(tbox_Total_Tax.Text)
        ObjWriter.WriteLine()
        ObjWriter.WriteLine()
        ObjWriter.Write("                   ")
        ObjWriter.Write(tbox_Grand_Total.Text)

        ObjWriter.Close()
        '******************************'
        'creating the new word document'
        '******************************'
        If Not System.IO.File.Exists(New_Doc) Then
            System.IO.File.Create(New_Doc).Dispose()
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '**************************************************************************************'
        'Setting the date to automatically pull out todays date and send it to the invoice date'
        '**************************************************************************************'
        tbox_Inv_Date.Text = DateValue(Now)

    End Sub

Recommended Answers

All 11 Replies

1st of all this isn't a word file, but a text file with .doc extension. If you open one in notepad you'll see it's contents.

to save it with tbox_Inv_Date.Text as filename you should:
Dim New_Doc As String = "C:\Users\ComputerFirstAde\Desktop\" & tbox_Inv_Date.Text & ".doc"

hi Adam_K,
thanks for the fast reply
is it really that simple? as i used the date as an example, i will be trying to use it on a different txtbox, but i havent coded that yet

ok moving onto the word doc, i did try .docx but it wasn't having any of it, but when i took the 'x' away it saved as a word document, is there a more afficent way of saving to a word document, anything after office 2007 (im using 2010, but the second PC in my office is using 2007)

p.s i noticed you've answered other peoples threads today, fair play your on fire :)

Why do you have to write to a word file? If it's a requirement then you've got to use word to write to it's files.
I can't remember having come across an article with word, but this is doing pretty much the same thing with Excel: http://www.daniweb.com/software-development/vbnet/threads/419083/finding-the-next-row-in-excel-spreadsheet

If all you want to do is do some calculations and save a file - even print it - may I suggest building the entire thing in Word VBA? It will save you both time and it will be easier to debug and possibly maintain. After all once you declare the object you'll have to pass VBA commands to it.

Or you could use the RichTextBox control and write to an rtf file which is readable by Word.

Or just because it's word doesn't mean you can't use plain text

You can use both text and rtf, but would you call it a word file and give it extension .doc (or .docx) ? If this is a school project, will it get the grade?

I've got to ask the OP though, why Word files?

hi guys, thanks again for your replies,
no this isn't for a school project, this is for me, and i would like it to go to word, but i know that will bring its own problems in the future, my next step after writting the info to word, would be to put lines and grids etc (but thats later on)

Since you already know it's bad design, listen to logic and switch to an alternative.

It would save you a lot of trouble down the line to do it right from the begining.

ok,
so you suggest writting it to rtf (which in turn is readable in word (if needed)?

The newer office file formats such as "docx" and "xlsx" are in reality zip packages containing the files that define the contents in "XML" format. These can be created using: Open XML SDK 2.0 for Microsoft Office.

You can view the "XML" content of an office file by renaming it with a ".zip" extension and then opening the compressed folder in explorer or use a utility like "7Zip" to open the office file as an archive directly.

Here is a link to a three part article on creating a Word "Docx" file.
Creating Documents by Using the Open XML Format SDK 2.0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.