Word object in VB

O

Ognjen

Hi

I have to write some text into Word, by VB, end I got an error: ""ActiveX
component can't create object", and in run-time, my program stops working! I
have find, in MSDN, am example about using excel as object type, but there
are some constants, perhaps only for excel? Can I wind, and where, these
const for word, or how can I, without an error, link word an VB? Can anyone
send me an example?


Sub GetExcel()
Dim MyXL As Object ' Variable to hold reference
' to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final release.

' Test to see if there is a copy of Microsoft Excel already running.
On Error Resume Next ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
There is an error when I change everything into word object!!!
Err.Clear ' Clear Err object in case error occurred.
DetectExcel
Set MyXL = GetObject("c:\vb4\MYTEST.XLS")

.....


Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024 '????
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Sub
 
P

Peter Hewett

Hi Ognjen

If you're using the Word object and you're having problems with it what's the point of
posting the Excel code???

Post the code you're having problems with!

Cheers - Peter


Hi

I have to write some text into Word, by VB, end I got an error: ""ActiveX
component can't create object", and in run-time, my program stops working! I
have find, in MSDN, am example about using excel as object type, but there
are some constants, perhaps only for excel? Can I wind, and where, these
const for word, or how can I, without an error, link word an VB? Can anyone
send me an example?


Sub GetExcel()
Dim MyXL As Object ' Variable to hold reference
' to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final release.

' Test to see if there is a copy of Microsoft Excel already running.
On Error Resume Next ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
There is an error when I change everything into word object!!!
Err.Clear ' Clear Err object in case error occurred.
DetectExcel
Set MyXL = GetObject("c:\vb4\MYTEST.XLS")

....


Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024 '????
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Sub

HTH + Cheers - Peter
 
O

Ognjen

Hi Peter

Thank you for attempt to help me, but it seems that we don't understand
each other. The example which I put was for Excel, but I need a same, or
similar one (code), for Word!
 
P

Peter Hewett

Hi Ognjen

Probably the easiest way to work with Word is to create a reference to the Word object
library. You need to create a project reference to the earliest version of Word you want
your code to work with.

The Excel example you posted uses late binding. Late binding is nowhere as efficient as
early binding plus you don't get intellisense and you can get runtime errors because the
compiler cannot resolve late bound references.

Once you create a Reference to the Word object Library, you need to instantiate Word:


Public Sub MyCode()
Dim wdApp As Word.Application
Dim docNew as Word.Document

' Instantiate Word
Set wdApp = New Word.Application

' Create a new document based on Normal.dot
Set docNew = wdApp.Documents.Add

' Add some text
With docNew.Content
.InsertAfter "This is the first paragraph" & vbcr
.InsertAfter "This is the second paragraph"
End With

' Save the document
docNew.SaveAs "C:\Temp\Test.doc"
docNew.Close wdDoNotSaveChanges

' We're done with Word
wdApp.Quit
Set wdApp = Nothing
End Sub

HTH + Cheers - Peter
 
C

Cindy M -WordMVP-

Hi Ognjen,
Thank you for attempt to help me, but it seems that we don't understand
each other. The example which I put was for Excel, but I need a same, or
similar one (code), for Word!
These newsgroups aren't here to provide you with a finished solution, but
to lead you to develop your own solution. Few of us have the time to
analyze the Excel code you posted to figure out what you want, then write
it in Word VBA.

You should post the code that causes the error message you quote in your
original post. Indicate on which line the error is occurring. And describe
what it is you expect the code to do, if it would work correctly. Then we
can, one hopes, show you how to correct your code.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top