Solved

Creating a word doc in vb and formating??

Posted on 2006-11-07
7
163 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 76

Expert Comment

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

Author Comment

by:dkim18
Comment Utility
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
Comment Utility
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
Comment Utility
Got it.
Thanks much.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

16 Experts available now in Live!

Get 1:1 Help Now