VB6 Question

I’m looking for a way to save a text box to a MSWord .doc using VB6. I’ve tried some things but they aren’t working. If I can get it to work I also need to set margins and font size and font name.

I’d Appricate any help I can get with this.

There are two methods to do this:

  1. Save the file in RichText format (.rtf). This can be read using Word. Put a RichTextBox on a form, insert the text into the RichTextBox control, and use the SaveFile method of the RichTextBox.
  2. If you want to save it in .doc format directly, go here:
    It explains exactly how to do it in the More Information section.

Wow. I would have used VBA or something. Nice!

VBA would only work if PaPPy was writing a VBA program in the first place. If he wants to save a .doc file from VB, he’ll have to do it externally.

First, add a reference to the “Microsoft Word 10.0 Object Library” from the Project->References menu.

This subgroutine will create a new word document and write a block of text to it:

Sub WriteWordDoc(content As String, _
                 filename As String, _
                 Optional font As String = "Verdana", _
                 Optional fontsize As Integer = 10)
    Dim app As Word.Application
    Dim doc As Word.Document
    'get an instance of the MS Word application
    Set app = CreateObject("Word.Application")
    'create a new document
    Set doc = app.Documents.Add
    'initialize the document and write the content
    With doc.content
        .font.Name = font
        .font.Size = fontsize
        .Text = content
    End With
    'save the document and close Word
    doc.SaveAs filename
    Set doc = Nothing
    Set app = Nothing
End Sub

The app.Quit line is important, otherwise it will keep Word open in the background. If you add the line “app.Visible = true” and remove “app.Quit” then you will see Word open with your new document.

Will a similar setup work with other versions of office? (and won’t the ‘doc.Close’ make ‘app.Visible = true’ a little useless? Slight bug.)

I would suggest you try using C# or VB.NET if you’re just getting started with VB. The VS 6.0 tools won’t be supported forever and you can do everything (and a lot more) with the .NET SDK.

Here is a sample class that has a method to save the information to a Word document. It only has two properties, Name and Bio. The Name will be written in 12pt Bold and the Bio in 10pt normal font. There is an overload that defaults to hiding and closing Word.

public class Person
	private string name;
	private string bio;

	public Person()
		name = string.Empty;
		bio = string.Empty;

	public Person(string n, string b)
		name = n;
		bio = b;

	public string Name
		get { return this.name; }
		set { this.name = value; }

	public string Bio
		get { return this.bio; }
		set { this.bio = value; }

	//overload of the ToWord method that does not display Word
	public void ToWord(string filename)
		this.ToWord(filename, false);

	//ToWord method writes the bio information to a new Word document.
	//the Show argument specifies whether we keep Word open.
	public void ToWord(string filename, bool show)
		//pointer to Word application
		Word.Application app = new Word.Application();

		//create a new document
		object opt = Missing.Value;
		Word.Document doc = app.Documents.Add(ref opt, ref opt, ref opt, ref opt);
		Word.Paragraph p;

		//add a new paragraph and write the person's name
		p = doc.Content.Paragraphs.Item(1);
		p.Range.Font.Name = "Verdana";
		p.Range.Font.Size = 12;
		p.Range.Font.Bold = 1;
		p.Range.Text = this.name;

		//add a new paragraph and write out the bio
		p = doc.Content.Paragraphs.Item(2);
		p.Range.Font.Name = "Verdana";
		p.Range.Font.Size = 10;
		p.Range.Font.Bold = 0;
		p.Range.Text = this.bio;

		//save the document
		object file = filename;
		doc.SaveAs(ref file, ref opt, ref opt, ref opt, ref opt, 
				ref opt, ref opt, ref opt, ref opt, ref opt, ref opt, 
					ref opt, ref opt, ref opt, ref opt, ref opt);
		if (!show)
			doc.Close(ref opt, ref opt, ref opt);
			app.Quit(ref opt, ref opt, ref opt);
			app.Visible = true;