Solved

Create a word document in VB

Posted on 2003-11-19
8
653 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

809 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