Start MS Word?

D

dimm

Hi all,

I'm trying to open Microsoft Word using VBA. When I execute my code
something processes for a second but nothing happens......I have ticked the
'Microsoft Word 10.0 Object Library' box in the Tools>Referances menu. And I
have the following code:

Sub StartWord()
Dim wrdApp As Word.Application
Set wrdApp = New Word.Application
End Sub

Any ideas are appreciated....
 
D

Dave Peterson

WrdApp.visible = true

might make you find it easier.

Here's one that I saved:

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim myPWD As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\doc10.doc"
myPWD = "mypassword"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

WDDoc.WritePassword = myPWD
WDDoc.Close savechanges:=True

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

Using the reference is very nice for testing purposes--you'll see the
intellisense. But lots of times, it causes heartache and pain when you
distribute it to others and they have a different version of excel.

Then the slightly slower late binding (As Object) is much safer.
 
D

dimm

Thanks folks, thats great, really great. They all worked a treat. :) Thankyou.

I started another post now asking how to copy data values (Not the
functions) from excel cells into my now open MS Word document. Any ideas are
appreciated?
 

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