Solved

Create a word document in VB

Posted on 2003-11-19
8
627 Views
Last Modified: 2010-05-01
How can I create a word document in Word a simple syntax example will suffice. I' m a newbie in VB I have been doing ASP, so it may be similar in asp. Thanks. Also which object library do I use.
0
Comment
Question by:7of9121098
  • 4
  • 4
8 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 9781674
reference ms word object library under project/references:

Dim wdApp as New Word.Application
Dim wdDoc as Word.Document

set wdDoc = wdApp.documents.Add ("<path to new file>")

'' add whatever you want to the doc

wdDoc.save

wdDoc.close
set wdDoc = nothing

wdApp.quit
set wdApp = nothing
0
 

Author Comment

by:7of9121098
ID: 9782601
How do I add whatever you want to the doc, what property?

0
 
LVL 18

Expert Comment

by:bobbit31
ID: 9782656
wdApp.Selection.TypeText "Hello World"
wdApp.Selection.TypeParagraph '' this is a newline/cr

if you want to do bold and the like... i'd suggest recording a new macro. Type stuff, make stuff bold, insert picture, whatever... then go to edit the macro and you can see the code on how to do it.
0
 

Author Comment

by:7of9121098
ID: 9782861
bobbit31, I get an error when I try to create the new word document. Did I do something wrong?

set wdDoc = wdApp.documents.Add ("c:\temp\newfile.doc")
0
Highfive Gives IT Their Time Back

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!

 
LVL 18

Expert Comment

by:bobbit31
ID: 9782905
what's the error?
0
 

Author Comment

by:7of9121098
ID: 9782972
The word document has to exist otherwise I get an error: path invalid...error.
I works fine it the newfile.doc exists in the path, otherwise a error occurs.

How do I make it so I can create a new word document....instead of have a preexisting document in the directory path?
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 9783140
oops, just do:

wdApp.documents.Add
set wdDoc = wdApp.ActiveDocument

then

wdDoc.saveas "C:\temp\newfile.doc"
0
 

Author Comment

by:7of9121098
ID: 9783250
Your the man!, thanks....

  Dim wdApp As New Word.Application
  Dim wdDoc As Word.Document

  wdApp.documents.Add
  wdApp.Selection.TypeText "Hello World"
  wdApp.Selection.TypeParagraph 'new line
  wdApp.Selection.TypeText "Hello World2"
  Set wdDoc = wdApp.ActiveDocument

  wdDoc.saveas "C:\temp\newfile.doc"
  wdDoc.Close
  Set wdDoc = Nothing

  wdApp.quit
  Set wdApp = Nothing
0

Featured Post

Highfive Gives IT Their Time Back

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…
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…

747 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

11 Experts available now in Live!

Get 1:1 Help Now