Can't seem to open existing Word documents

A

Android

Hi,

Could someone please advise me on opening existing Word
documents.

I have the following macro in an Excel file. I want to
open a Word documents & do stuff with them. Can't seem to
be able to open it.

This version (of many) gives no errors, but also does not
open my files.

What am I missing???

Regards,

Android.

----------------------------------------
Private Sub Workbook_Open()
Dim i As Integer
Dim ProjectsList(2) As String

Dim oWord As Word.Application
Dim WordWasNotRunning As Boolean
Dim oDoc As Word.Document
Dim myDialog As Word.Dialog
Dim UserButton As Long

Set oWord = GetObject(, "Word.Application")

FoundFileIndex = 2
ProjectsList(1) = "C:\Test\TestDoc1.doc"
ProjectsList(2) = "C:\Test\TestDoc1.doc"

For i = 1 To FoundFileIndex
Set oDoc = oWord.Documents.Open(ProjectsList(i))
Next i

Set oDoc = Nothing
Set oWord = Nothing

End Sub
 
C

Cindy Meister -WordMVP-

Hi Android,
I have the following macro in an Excel file. I want to
open a Word documents & do stuff with them. Can't seem to
be able to open it.

This version (of many) gives no errors, but also does not
open my files.

What am I missing???
I'm guessing if you looked in the Task Manager you'd see an
instance of winword.exe for each time you've run this code...

I'd say you need this:

oWord.Activate
oWord.Visible = True
Private Sub Workbook_Open()
Dim i As Integer
Dim ProjectsList(2) As String

Dim oWord As Word.Application
Dim WordWasNotRunning As Boolean
Dim oDoc As Word.Document
Dim myDialog As Word.Dialog
Dim UserButton As Long

Set oWord = GetObject(, "Word.Application")

FoundFileIndex = 2
ProjectsList(1) = "C:\Test\TestDoc1.doc"
ProjectsList(2) = "C:\Test\TestDoc1.doc"

For i = 1 To FoundFileIndex
Set oDoc = oWord.Documents.Open(ProjectsList(i))
Next i

Set oDoc = Nothing
Set oWord = Nothing

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

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

Harold Kless[MSFT}

I made a few revisions to your code sample and it does work.
I set the variable as objects - using late binding. If you set a reference
to MSWord, then you can declare the variables are applications, documents,
etc. We have found that late binding does perform better with Word and
Excel.

Private Sub WordDoc_Open()


Dim i As Integer
Dim ProjectsList(2) As String

Dim oWord As Object
Dim WordWasNotRunning As Boolean
Dim oDoc As Object
Dim myDialog As Object
Dim UserButton As Long

Set oWord = CreateObject("Word.Application")

FoundFileIndex = 2
oWord.Visible = True

ProjectsList(1) = "C:\Test\TestDoc1.doc"
ProjectsList(2) = "C:\Test\TestDoc2.doc"

For i = 1 To FoundFileIndex
Set oDoc = oWord.Documents.Open(ProjectsList(i))
Next i

Set oDoc = Nothing
Set oWord = Nothing


End Sub


Harold Kless, MCSD
Support Professional
Microsoft Technical Support for Business Applications
(e-mail address removed)

--


This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 

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