Solved

Create a word document in VB

Posted on 2003-11-19
8
645 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 64
vb6 connector to SQL Server 2 37
Error when passing an argument to a vb6 that contains  '/' 7 37
Visual Studio 2005 text editor 10 38
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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…

770 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