PDA

View Full Version : Solved: Opening a Word document from Excel in the running instance of Word



lynnnow
12-22-2006, 04:11 AM
Hi,

I've got a list of Word documents that I need to open and work with. Since this list is pretty long and the files have a particular order to be done, I have made a chronological list and I open the files from Excel itself. So far I have to CreateObject and the file is opened in a new instance of Word. My objective is to open the Word documents in the current instance instead of closing and opening Word all the time. The reason I am doing this is because the number of files is huge and it is easy just opening the file(s) from Excel itself.

So far this is what I have managed to do:


Sub DocOpen()
Dim wdNew As Object
Dim File2Open As String

With Application.FileSearch
.LookIn = "D:\Lincoln\Today's Work\" & Format(ActiveCell.Offset(0, -1).Value, "mmmm dd")
.FileName = ActiveCell.Value & "-"
.FileType = msoFileTypeWordDocuments
.Execute
If .FoundFiles.Count <> 0 Then
Set wdnew = CreateObject("Word.Application")
wdnew.Visible = True
i = 1
Do While i <= .FoundFiles.Count
File2Open = .LookIn & "\" & .FileName & i '& ".doc"
On Error Resume Next
wdnew.documents.Open File2Open
i = i + 1
Loop
Else
MsgBox "No files of this name could be found", vbExclamation, "...::: Lynx's Corner :::..."
End If
End With
End Sub



This works fine for me right now, the only problem is opening and closing the instance of Word every time and letting Excel start a new instance instead of opening the Word document in the current instance.

Any help/pointers in the right direction is appreciated.

Thanks,

Lincoln

Bob Phillips
12-22-2006, 04:23 AM
Sub DocOpen()
Dim wdNew As Object
Dim File2Open As String

On Error Resume Next
Set wdNew = GetObject(, "Word.Application")
If wdNew Is Nothing Then
Set wdNew = CreateObject("Word.Application")
If wdNew Is Nothing Then
MsgBox "Error starting Word"
End Sub
End If
End If
on error Goto 0

With Application.FileSearch
.LookIn = "D:\Lincoln\Today's Work\" & Format(ActiveCell.Offset(0, -1).Value, "mmmm dd")
.Filename = ActiveCell.Value & "-"
.FileType = msoFileTypeWordDocuments
.Execute
If .FoundFiles.Count <> 0 Then
wdNew.Visible = True
i = 1
Do While i <= .FoundFiles.Count
File2Open = .LookIn & "\" & .Filename & i '& ".doc"
On Error Resume Next
wdNew.documents.Open File2Open
i = i + 1
Loop
Else
MsgBox "No files of this name could be found", vbExclamation, "...::: Lynx's Corner :::..."
End If
End With

Set wdNew = Nothing

End Sub

lynnnow
12-22-2006, 07:22 AM
Hey buddy this worked superbly. Thanks for the help.

Bob Phillips
12-22-2006, 08:23 AM
Hey buddy this worked superbly. Thanks for the help.
Hey buddy doesn't quite sound the sort of thing an Indian would say?

lynnnow
12-22-2006, 08:52 AM
Well, I don't quite know what you expected in return for the help you provided. My msg was spontaneous and needed to convey how I felt about the solution (even after looking at the online help but not able to see the light). If I've offended you in any way, well, I'm sorry. But trust me, I'm an Indian from Mumbai (formerly Bombay). I'm at work right now (9:30 p.m. Indian time) and waiting for it to be 10:30 so that I can pack off home. ciao :biggrin:

Bob Phillips
12-22-2006, 09:30 AM
Well, I don't quite know what you expected in return for the help you provided. My msg was spontaneous and needed to convey how I felt about the solution (even after looking at the online help but not able to see the light). If I've offended you in any way, well, I'm sorry. But trust me, I'm an Indian from Mumbai (formerly Bombay). I'm at work right now (9:30 p.m. Indian time) and waiting for it to be 10:30 so that I can pack off home. ciao :biggrin:

Not at all mate, I just know loads of Indians, and that is just not the sort of thing they say, most of my friends don't use that sort of idiom. I was thinking that you might be an American or Australian and working in India, and was (badly as it turned out) just asking about you. A smiley might have helped.

I know Mumbai was Bombay, as I know that Chennai was Madras (I am English, and that country still fascinates most of us).

That's a long day, and I have no idea if you have a braek at this time of year as we do, if you do, enjoy it.