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
  • 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.
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Updates not working for MS Windows 7 12 164
using web browser with BING 40 119
VB6 - Convert HH:MM into Decimal 8 54
Export Data to Different .csv Files 26 104
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 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