Consulting

Results 1 to 6 of 6

Thread: Solved: Opening Path error?

  1. #1
    VBAX Regular
    Joined
    Jun 2006
    Posts
    20
    Location

    Solved: Opening Path error?

    From within an Outlook VBAProject I need to Open a Word Document. The following code is fine;

    strInvoice = "C:\Invoice1.doc"
    RetVal = Shell("C:\Program Files\Microsoft Office\Office12\winword.exe" & strInvoice, 1)

    But if;

    strInvoice = "C:\Documents and Settings\Peter R Hawkes\Invoice1.doc"

    Then Word opens but message states cannot find c:\documents.doc

    There has to be an easy answer but what is it!! I can see it is the spaces in the path but these cannot be changed?

    Peter H

  2. #2
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    Try changing it to this:
    [vba]
    strInvoice = "Invoice1.doc"
    RetVal = Shell("C:\Program Files\Microsoft Office\Office12\winword.exe\" & strInvoice, 1)
    [/vba]

    I had similar problems with paths and never could figure it out, so maybe that won't work, but I think it might.
    Office 2010, Windows 7
    goal: to learn the most efficient way

  3. #3
    VBAX Regular
    Joined
    Jun 2006
    Posts
    20
    Location
    Unfortunately that will not find the file as there is no path information?

    Peter H

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I don't understand why you are using Shell to do this. If you want to open a Word document from Outlook, make an instance of Word, and open the document.

    Shell is going to open winword.exe anyway, so why not just explicitly use an instance of Word??

  5. #5
    VBAX Regular
    Joined
    Jun 2006
    Posts
    20
    Location

    Correct!

    You are quite correct and a lapse on my part as already using Word object to writ eto the invoice! After some help from Sue Mosher and a KB article I have settled on this:
    [vba]
    Sub OpenInvoice(strOpenThis)

    ' http://support.microsoft.com/kb/177097/en-us
    Dim wObj As Word.Application

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

    wObj.Documents.Open (strOpenThis)
    wObj.Visible = True
    Set wObj = Nothing

    End Sub
    [/vba]

    Thank you.

    Peter H

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    And there you go. Although make sure you quit and close the instance properly.....

Posting Permissions

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