Consulting

Results 1 to 6 of 6

Thread: Solved: Opening a Word document from Excel in the running instance of Word

  1. #1
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location

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

    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:

    [VBA]
    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

    [/VBA]

    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

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    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
    [/vba]

  3. #3
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    Hey buddy this worked superbly. Thanks for the help.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by lynnnow
    Hey buddy this worked superbly. Thanks for the help.
    Hey buddy doesn't quite sound the sort of thing an Indian would say?

  5. #5
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    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

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by lynnnow
    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
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •