Go to Post Maybe FIRST will just take 148's Tumbleweed and use it as the game piece for next year. - AndyB [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 26-05-2004, 01:22
PaPPy PaPPy is offline
Registered User
AKA: Brian Papile
None #0069 (HYPER)
Team Role: Alumni
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Quincy, Massachusetts
Posts: 34
PaPPy is on a distinguished road
Send a message via AIM to PaPPy
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.
__________________
~-PaPPy-~

__________________
  #2   Spotlight this post!  
Unread 26-05-2004, 17:33
Guest
 
Posts: n/a
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   Spotlight this post!  
Unread 26-05-2004, 21:27
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: VB6 Question

Wow. I would have used VBA or something. Nice!
  #4   Spotlight this post!  
Unread 26-05-2004, 21:30
Guest
 
Posts: n/a
Re: VB6 Question

Quote:
Originally Posted by Astronouth7303
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.
  #5   Spotlight this post!  
Unread 26-05-2004, 21:52
seanwitte seanwitte is offline
Registered User
None #0116
Team Role: Engineer
 
Join Date: Nov 2002
Location: Herndon, VA
Posts: 378
seanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant future
Send a message via AIM to seanwitte
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
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.
  #6   Spotlight this post!  
Unread 27-05-2004, 07:32
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: VB6 Question

Quote:
Originally Posted by seanwitte
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
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.)
  #7   Spotlight this post!  
Unread 27-05-2004, 11:54
seanwitte seanwitte is offline
Registered User
None #0116
Team Role: Engineer
 
Join Date: Nov 2002
Location: Herndon, VA
Posts: 378
seanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant future
Send a message via AIM to seanwitte
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.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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


All times are GMT -5. The time now is 06:53.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi