Creating a word doc in vb and formating??

Posted on 2006-11-07
Last Modified: 2010-04-30

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..
Question by:dkim18
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 76

Expert Comment

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
LVL 76

Accepted Solution

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

Author Comment

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.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 76

Expert Comment

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

Author Comment

ID: 17899971

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.
LVL 76

Expert Comment

ID: 17900068
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.

Author Comment

ID: 17900211
Got it.
Thanks much.

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

615 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