Solved

Create Word in VB

Posted on 2001-06-07
40
360 Views
Last Modified: 2008-02-01
Hi Experts,

Can anyone tell me how to activate Word document when end user clicked on the command button in VB environment?

Operations:
1)Ability in writing a string of text into word document and saved it in the background job.
2)Need to preview the word document and print.

Pls advise!  
0
Comment
Question by:yenling
  • 18
  • 9
  • 6
  • +2
40 Comments
 
LVL 8

Expert Comment

by:Navid
ID: 6166175
Q1:
Sub open_documents()
Documents.Open FileName:="C:\my documents\document.doc"
end sub
Q2:
sub type_a_text()
Selection.TypeText Text:="I am typing a text."
end sub
Q3:
sub preview_and_printout ()
ActiveDocument.PrintPreview
ActiveDocument.Printout
end sub
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6166201
Hi, In addition, you need to create an instance of word object using either 1 of the method below:

1. Working Directly with Objects

Dim Doc as Word.Document
set Doc = CreateObject("Word.Document")
...do whatever with the document...
Doc.PrintOut
Doc.SaveAs filename
Set Doc = Nothing

2. Working Through the Application
Dim WordApp as Word.Application
Dim Doc as Word.Document
Set WordApp = new Word.Application
Set Doc = WordApp.Documents.Add
...do whatever with the document...
Set Doc = Nothing
Set WordApp = Nothing
0
 

Author Comment

by:yenling
ID: 6166288
Can someone give me a working example?

I'm new to this!
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6166318
Hi, firstly i'm not used in Vb / Word integration:

You can set the content of a word document by:

myDoc.Content.Text = "Hello World"


For the inset picture: (Don't Know but here is a stupid alternative method)
Firstly copy your image in ClipBoard , then
myDoc.Content.PasteSpecial
   

To Preview the document:
myDoc.PrintPreview

To Print the document:
myDoc.PrintOut


'Hope help little.
0
 

Author Comment

by:yenling
ID: 6166350
I want to add text to an existing word document.Pls advise!
0
 

Author Comment

by:yenling
ID: 6166359
It don't work.Can u show me an example on adding text to an existing word document?Thanks!

Private Sub Command3_Click()
Dim WordApp As Word.Application
Dim Doc As Word.Document
Set WordApp = New Word.Application
Set Doc = WordApp.Documents.Add
Doc.Content.Text = "Hello World"
Doc.SaveAs "D:\temp\a.doc"
Set Doc = Nothing
Set WordApp = Nothing
End Sub

0
 
LVL 8

Expert Comment

by:Navid
ID: 6166364
Private Sub Command3_Click()
Selection.TypeText Text:="I am typing a text."
end sub
0
 
LVL 8

Expert Comment

by:Navid
ID: 6166371
Since I assumed that you are creating a macro in a word documents, you do not have to create instance of word.

Private Sub Command3_Click()
Documents.Open FileName:="C:\my documents\document.doc"
Selection.TypeText Text:="I am typing a text."
ActiveDocument.PrintPreview
ActiveDocument.Printout
end sub
0
 

Author Comment

by:yenling
ID: 6166394
How to create macro in word document?
0
 
LVL 8

Expert Comment

by:Navid
ID: 6166401
Open word
"Tools"
"Macro"
"Macros..."
"create new"
0
 

Author Comment

by:yenling
ID: 6166417
What are the steps I need to do?Can you teach me step by step?
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6166422
Hi yenling, try:

modify

myDoc.Content.Text = "Hello World"

to

myDoc.Content.Text = myDoc.Content.Text & vbcrlf & "Hello World"

?


0
 
LVL 8

Expert Comment

by:Navid
ID: 6166441
yenling!
You know how to create a command button right? We start with creating a command button in word.
("View" , "Toolbars"  and check "Control toolbox". That will give you option to create a command button)
When you are done, double click on commandbutton. That should open VB. Now add these lines to VB:
'-----------
Private Sub CommandButton1_Click()
Documents.Open FileName:="C:\my documents\document.doc"
Selection.TypeText Text:="I am typing a text."
 ActiveDocument.PrintPreview
ActiveDocument.Printout
End Sub
'----------------
Now close VB and go back to your command button that you had created earlier.
Make sure that "design mode" is not active(not pushed down) or the button will not work.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6166473
Hi yenling, to confirm the development environment, you use VBA (macro in word) or Visual Basic?
0
 

Author Comment

by:yenling
ID: 6166554
Which is the better method?Pls advise!
0
 

Author Comment

by:yenling
ID: 6166567
I need to insert text to word document in the next line whenever inserting operation is done.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6166572
No, it's depending on what you want to do.

If you want to create a separate program from MS Word, then choose Microsof Visual Basic, else choose Microsoft Word which includes VBA that perform 'like' VB.
0
 

Author Comment

by:yenling
ID: 6166598
automation error!!!help
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 20 total points
ID: 6166616
Hi yenling, try this to append text to word document:


myDoc.Content.Text = myDoc.Content.Text & "Hello World!" & vbCrLf & Date


It should solve your problem.
________________________________

OR use this:

Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Add ' create a new document
    ' or
    'Set wrdDoc = wrdApp.Documents.Open("C:\Foldername\Filename.doc") ' open an existing document
    ' example word operations
    wrdDoc
        For i = 1 To 100
            .Content.InsertAfter "Here is a example test line #" & i
            .Content.InsertParagraphAfter
        Next i
        If Dir("C:\Foldername\MyNewWordDoc.doc") <> "" Then
            Kill "C:\Foldername\MyNewWordDoc.doc"
        End If
        .SaveAs ("C:\Foldername\MyNewWordDoc.doc")
        .Close ' close the document
    End With
    wrdApp.Quit ' close the Word application
    Set wrdDoc = Nothing
    Set wrdApp = Nothing


AND view

http://www.erlandsendata.no/english/vba/ole/controlword.htm

0
 

Author Comment

by:yenling
ID: 6166641
Sub EditWord()

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document

Set wrdApp = CreateObject("Word.Application")
Set wrdDoc = wrdApp.Documents.Open("C:\LogBk.doc")

With wrdDoc
    .Content.InsertAfter "Here is a example test line#"
    .Content.InsertParagraphAfter
    .SaveAs ("C:\LogBk.doc")
    .Close
End With

wrdApp.Quit

Set wrdDoc = Nothing
Set wrdApp = Nothing

End Sub

ERROR:AUTOMATION ERROR!!!

PLS HELP!
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 3

Expert Comment

by:nigelrowe
ID: 6166772
What's your set up. 95, 98, NT, 2000?
0
 

Author Comment

by:yenling
ID: 6166838
win 98
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6166874
Do you have DCOM98 installed?
0
 

Author Comment

by:yenling
ID: 6166947
nope.How?
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6166986
I think its probably on the CD, if not you can obtain it from MS site. You have to install this to use OLE automation.
0
 

Author Comment

by:yenling
ID: 6167020
I will download DCOM98....now....
0
 

Author Comment

by:yenling
ID: 6167132
In the references,I had already include OLE automation.What is the problem now?Pls advise!
0
 

Author Comment

by:yenling
ID: 6167136
it still don't work...automation error again
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6167169
Did you install DCOM98?
0
 

Author Comment

by:yenling
ID: 6167385
I had run the exe file of DCOM98....what other steps have i left out?pls advise......
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6167496
Try this...

Add a reference Microsoft Word Object Library to a VB project, then

Option Explicit
Private objWordApp As Word.Application

Private Sub Command1_Click()

    Set objWordApp = New Word.Application
    objWordApp.Visible = True

End Sub

Anything?

If still no go, what's the exact error message?
0
 
LVL 8

Expert Comment

by:Navid
ID: 6167684
Since you are so new with VB. I suggest you to start with recording Macros in Word. You can edit the recorded Macros and learn.
What nigelrowe and  ryancys are suggesting is to create a word object in a pure VB invirement. I think it is way too hard to start with for a newbie which doese not know anything about VB and is looking for such a simple commands. You can not use the pure VB commands in Macros created in Word.
Just listen to me please:
Open a new word document. Open "Tools" > "Macro" > "Record a Macro"
Now do what ever you like. Add a text,printview,Print and so on. When you have done everything you want this Macro to do for you. Again go to "Tools" > "Macro" > "Stop recording"
Now your Macro is ready. Just Make a command button as I described earlier and connect the button to the Macro.
Start with this my friend. Later can you edit macros and when you have started to understand what these commands do, you can create your own Macro or VB script in a pure VB invirement.
If somebody wants to learn how to drive a car, it is not such a good idea to ask him start with driving a bus.
0
 

Author Comment

by:yenling
ID: 6168038
I've already added a reference Microsoft Word Object Library to a VB project.What is causing the problem?
0
 

Expert Comment

by:Douminou
ID: 6168590
Reference: Microsoft Word 9.0 Object Library

Here are some basics commands you can use.  As Navid sais, you can go in word, record a macro and see the code that has been written in the macro with the Visual Basic editor that is in Word.  The code is 100% compatible from VBA (for Word) and Visual Basic.  All you have to to is to put your Word object in front of the command you want to do.

'Declare My Word Object
Dim GoWord as Word.Application
Set goWord = New Word.Application

'Add a new document in Word
GoWord.Documents.Add

'Go to a specified bookmark in the document
goWord.Selection.GoTo What:=wdGoToBookmark, Name:=yourbookmark

'Type some text:
GoWord.Selection.TypeText Text:=your text

Almost all the commands I use in a Word document is under the Word Object.Selection.  You should find what you want there.

Hope it helps.
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6169039
What is the exact error message, copy and paste it to your next post!!!
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6169059
I appreciate your concern, but I must cite an an English proverb; Nothing ventured, nothing gained. The Word object model is very well documented if you take care to install VBA help when you install Word.
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6169064
Oops, that was intended for Navid
0
 

Author Comment

by:yenling
ID: 6169850
Run-time error -2147417846 (80001010a)
Automation error.

Pls advise!
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6170540
yenling...
I'll get back to you on monday, when I'm at work!! I'm at home right now, and I haven't got access to MDSN.
0
 

Author Comment

by:yenling
ID: 6173467
My solution as follows(Microsoft bug with Norton AntiVirus 2000):
http://support.microsoft.com/support/kb/articles/q246/0/18.asp

it's resolved.

Thanks everyone,especially nigelrowe for his helpfulness!I'm sorry that I need to give points to ryancys who answered my doubts first by recommending me to an useful site.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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

762 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

21 Experts available now in Live!

Get 1:1 Help Now