Solved

Creating a word doc in vb and formating??

Posted on 2006-11-07
7
164 Views
Last Modified: 2010-04-30
Hi,

I am extracting data from Excel worksheet and write to the WORD file.
I wrote VB code on the EXCEL itself in modules
So each record will be a word doc. I am writing values to the word doc.

so I am using  something like,
Set pen = createTextFile(filename)
pen.WriteLine(put stuff here)

 Can I format the text I write??? (FONT, BOLD, COLOR...etc)

Example will be like this:

1) Name:  XXXX
2)Address: XXXX
3)Title:  XXXX

XXXXX are coming from Excel..
Thanks
0
Comment
Question by:dkim18
  • 4
  • 3
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17893103
I don't know where the text file fits in.

This code creates a new Word document, takes data from the Active worksheet and places it in the new Word document.
It bolds the data from the worksheet.
It uses early binding, so you need to set a reference to the Word object tlibrary
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 17893110
Forgot the code:

Option Explicit

Sub WriteWordDoc()
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim rng As Word.Range
   
    Set wdApp = New Word.Application
    wdApp.Visible = True
    Set wdDoc = wdApp.Documents.Add
    Set rng = wdDoc.Range
    rng.Text = "1) Name: "
    rng.Collapse wdCollapseEnd
    rng.InsertAfter ActiveSheet.Cells(2, 1) & vbCr
    rng.Font.Bold = True
    rng.Collapse wdCollapseEnd
    rng.InsertAfter "2) Address: "
    rng.Font.Bold = False
    rng.Collapse wdCollapseEnd
    rng.InsertAfter ActiveSheet.Cells(2, 2) & vbCr
    rng.Font.Bold = True
    rng.Collapse wdCollapseEnd
    rng.Collapse wdCollapseEnd
    rng.InsertAfter "3) Title: "
    rng.Font.Bold = False
    rng.Collapse wdCollapseEnd
    rng.InsertAfter ActiveSheet.Cells(2, 3) & vbCr
    rng.Font.Bold = True
End Sub
0
 

Author Comment

by:dkim18
ID: 17899500
Thanks much.

So I should use the WORD.Application instead of FSO object.
I will try this and let you know if it worked out.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17899544
You have to use something that understands the Word document. I know Word does.
0
 

Author Comment

by:dkim18
ID: 17899971
GrahamSkan,

Thanks so much.
I got it working.
I will need to look into the WORD library.

One more question.
wdApp.Selection.typeText text:="DDFDFDFDF" <==== I guess this is another way to write to the doc??

I wonder this can also handle the formating I need to do..
Again thanks millions.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17900068
Thanks.
You can use Selection.TypeText. Personally, I try to avoid using the Selection object. It can make the code difficult to understand, so more difficult to maintain. Also there is only one such object per application, while you can have as many Range objects as you like.
0
 

Author Comment

by:dkim18
ID: 17900211
Got it.
Thanks much.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
LINQ return type for nested group query 6 70
Controlling which port to download from 4 71
VBA color chart bars 12 76
vb6 connector to SQL Server 2 34
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now