Consulting

Results 1 to 18 of 18

Thread: Solved: How to open a Specific file MsWord( example C:\MyW.doc) from MsEXcel...

  1. #1

    Question Solved: How to open a Specific file MsWord( example C:\MyW.doc) from MsEXcel...


  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    2 methods
    [VBA]Sub HLink()
    ActiveWorkbook.FollowHyperlink "C:\AAA\Test.doc"
    End Sub


    Sub WObject()
    Dim Wd As Object
    Dim Doc As Object
    Set Wd = CreateObject("Word.Application")
    Set Doc = Word.Documents.Open("C:\AAA\Test.doc")
    Wd.Visible = True
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    I inserted the code in the command but it shwoed this error debug object requierd at the this line Set Doc = Word.Documents.Open("C:\AAA\Test.doc")


    [vba]
    Private Sub CommandButton1_Click()
    WObject
    End Sub
    Sub WObject()
    Dim Wd As Object
    Dim Doc As Object
    Set Wd = CreateObject("Word.Application")
    Set Doc = Word.Documents.Open("C:\AAA\Test.doc")
    Wd.Visible = True
    End Sub

    [/vba]

  4. #4
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    [VBA]Set Doc = Wd.Documents.Open("C:\AAA\Test.doc")[/VBA]

    Artik

  5. #5
    Thank you for help it succeed with me this code by add Test.docx not doc becauseI'm using Ms2007
    Set Doc = Wd.Documents.Open("C:\AAA\Test.doc")

  6. #6

    How to open a Specific file MsWord

    Even better, as I bet you've found out, you can open .doc files created by Microsoft Word in Pages without a problem. Saving Pages docs in Word format, however, isn't quite as obvious as you may imagine, because it's not a "Save As" it's an "Export".

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Wd.Documents.Open
    Apologies for the typo.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    mdmackillop

    And so I'm lying and I think. When you create object variables that go beyond the application, at the end of the procedure should probably destroy them.
    Similarly like here:[vba]Sub WObject()
    Dim Wd As Object
    Dim Doc As Object

    Set Wd = CreateObject("Word.Application")
    Set Doc = Wd.Documents.Open("C:\AAA\Test.doc")
    Wd.Visible = True

    Set Wd = Nothing
    Set Doc = Nothing
    End Sub[/vba] Is there no such requirement?

    Artik

  9. #9
    The problem that I faced is that show me a message that the file is being used . and ask me if I want to open it only for reading so I clikc ok and it open it.

  10. #10
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    You probably have the file open in another instance of Word. Perhaps it is hidden. Kill all processes Word.exe in Task Manager.

    Artik

  11. #11
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    You probably did as Artik said and left an instance open in your testing.

    If someone else has it open, you can check for that if needed. http://www.vbaexpress.com/forum/showthread.php?t=3641

    As for not setting the object variables to Nothing, most would consider it good practice to do so. I saw a blog from a Microsoft employee once that said that it is not needed.

  12. #12
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    One more method:

    [VBA]Sub OpenFile()
    Dim wscrip As Object ' WshShell
    Set wscrip = CreateObject("WScript.Shell")
    wscrip.Run "path and filename"
    End Sub[/VBA]
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  13. #13
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    And,
    [VBA]Shell "cmd /c x:\msword\Letter1.doc", vbHide[/VBA]

  14. #14
    Quote Originally Posted by Artik
    mdmackillop

    And so I'm lying and I think. When you create object variables that go beyond the application, at the end of the procedure should probably destroy them.
    Similarly like here:[vba]Sub WObject()
    Dim Wd As Object
    Dim Doc As Object

    Set Wd = CreateObject("Word.Application")
    Set Doc = Wd.Documents.Open("C:\AAA\Test.doc")
    Wd.Visible = True

    Set Wd = Nothing
    Set Doc = Nothing
    End Sub[/vba] Is there no such requirement?

    Artik
    i tried this code work well now, but it show me the file minimize on the toolbar

  15. #15
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Try adding:
        Wd.Application.WindowState = 1
    ...after the Wd.Visible line.

  16. #16
    Quote Originally Posted by GTO
    Try adding:
        Wd.Application.WindowState = 1
    ...after the Wd.Visible line.
    I tried your line after Wd.Visible
    but still the same problem(Minimize..)

  17. #17
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I doubt that the MSWord Application is really minimized unless you already had it opened. What you want I suspect is to set focus or "Activate" it?

    In my example, you can delete or comment out the early binding method and uncomment the late binding method or set the reference if you want to use it. I included both methods as early binding makes intellisense work for you.

    [VBA]Sub OpenDoc()
    Dim s As String
    s = ThisWorkbook.Path & "\test.docx"
    OpenDoc2 s
    End Sub

    'Set Reference to Microsoft Word xx.x Object Libarary for Early Binding method.
    Sub OpenDoc2(sDoc As String)
    'Early binding method.
    Dim Wd As Word.Application
    Dim Doc As Word.Document

    'Late Binding method.
    'Dim Wd As Object
    'Dim Doc As Object

    If Dir(sDoc) = "" Then
    MsgBox "Error, file does not exist." & vbLf & sDoc, vbCritical, "File is Missing"
    Exit Sub
    End If

    On Error Resume Next
    Set Wd = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
    Set Wd = CreateObject("Word.Application")
    End If
    On Error GoTo errorHandler

    Set Doc = Wd.Documents.Open(sDoc)
    Wd.Visible = True
    'Wd.Activate

    errorExit:
    Set Doc = Nothing
    Set Wd = Nothing
    Exit Sub

    errorHandler:
    MsgBox "Unexpected error: " & Err.Number & vbLf & Err.Description
    Resume errorExit
    End Sub
    [/VBA]

  18. #18
    Thank you all for help!!!

Posting Permissions

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