Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Creating a word doc in vb and formating??

Posted on 2006-11-07
Medium Priority
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 2000 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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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

670 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