Results 1 to 4 of 4
  1. #1
    cg1465 is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    8

    If Then CreateObject

    I have a command button opening a word doc with the following:

    Dim oApp As Object
    Set oApp = CreateObject("Word.Application")
    oApp.Visible = True
    AppActivate oApp
    oApp.Documents.Open FileName:=FilLoc & "C:\FirstUSA\Prospects\" &


    Forms![FirstUSA]![Phone1] & ".doc"
    Set oApp = Nothing

    The above path may also have a .pdf INSTEAD of a .doc extention. So I need the code to open the file in either format depending on which document type exists. I'm not sure of the IF Then procedure.

    Thanks in advance!

    Clint

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    PDFs don't open in word do they?

    if not, just use a conditional after you check the extension chars. shell the PDF if that's what it is:
    Code:
    'decs
    Dim myShell As Object
    
       if right(forms!firstUSA!Phone1, 4) = ".pdf" then
          Set myShell = createobject("Wscript.shell")
             myShell.Run(forms!firstUSA!Phone1 & ".pdf")
       else
          'continue with opening word doc
       end if

  3. #3
    cg1465 is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    8
    Thanks Adam.

    The path to the file I need to open will either be a .pdf OR a .doc file. The path will be the same except for the extension and the application to open each file (Word or Adobe Reader). For example, the If/Then needs to point to either opening one of these paths:

    oApp.Documents.Open FileName:=FilLoc & "C:\FirstUSA\Prospects\" & Forms![FirstUSA]![Phone1] & ".doc"

    OR OPEN

    oApp.Documents.Open FileName:=FilLoc & "C:\FirstUSA\Prospects\" & Forms![FirstUSA]![Phone1] & ".pdf]"

    Using Word or Adobe Reader respectively to open each file. Make sense?

    Thanks again!

    Clint

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Clint,

    I have already explained how to solve this. Read the previous post carefully...out of the [code] tags, what I am saying is that you don't do anything with the word application objects until you identify the type of extension. Does that make sense?

    The shell command that I provided you should work just fine...windows default programs are almost always set appropriately in this scenario.

    e.g. - if you open a DOC, word opens it by default, if you open a PDF, reader or acrobat opens it, depending on what you've got installed.

    If it still doesn't make sense, let me know. All the information you need to solve this is already here...

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How do I specify a version of Word using CreateObject?
    By TrevorOlding in forum Programming
    Replies: 1
    Last Post: 03-17-2006, 12:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums