|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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. |
|
#2
|
|||
|
|||
|
Re: VB6 Question
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: http://support.microsoft.com/default...12&Product=vbb It explains exactly how to do it in the More Information section. |
|
#3
|
|||||
|
|||||
|
Re: VB6 Question
Wow. I would have used VBA or something. Nice!
|
|
#4
|
|||
|
|||
|
Re: VB6 Question
Quote:
|
|
#5
|
|||
|
|||
|
Re: VB6 Question
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: Code:
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
doc.Close
app.Quit
Set doc = Nothing
Set app = Nothing
End Sub
|
|
#6
|
|||||
|
|||||
|
Re: VB6 Question
Quote:
|
|
#7
|
|||
|
|||
|
Re: VB6 Question
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. Code:
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
doc.Content.InsertParagraphAfter();
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
doc.Content.InsertParagraphAfter();
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);
}
else
{
app.Visible = true;
}
}
}
Last edited by seanwitte : 27-05-2004 at 23:18. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question of the Week [05-02-04]: The Very Merry Month of May | EddieMcD | Rumor Mill | 10 | 05-05-2004 14:02 |
| MIM's question of the day on programming. | Gene F | Programming | 3 | 24-02-2004 16:32 |
| A question about control system options | computhief263 | Control System | 7 | 04-02-2004 14:46 |
| Chief Delphi Site Question | Mike Bonham | General Forum | 1 | 16-02-2002 22:18 |