+ Reply to Thread
Results 1 to 3 of 3

Opening a Microsoft Word document trough VBA code

Hybrid View

  1. #1
    Registered User
    Join Date
    01-13-2007
    Posts
    71

    Exclamation Opening a Microsoft Word document trough VBA code

    Hi all!

    Is it possible to open a MS Word documento trough a code in VBA??

    any help appreciated!

    thx

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Have a look at the help for CreateObject
    Martin

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Macrospaterson,

    Add a Standadrd Module to your VBA project and then copy and paste the code below into it. The macro "OpenFile" will open any file whose extension is registered with Windows. It takes 3 arugemnts: File Path (Directory where the file is located), File Name, and File Type (Extension - .doc for a Word file)The return value True if the file opened sucessfully or false if it did not. Error handling is built in.

    'Opens the File using the program associated with the extension.
     Private Declare Function ShellExecute _
      Lib "shell32.dll" _
       Alias "ShellExecuteA" _
          (ByVal hWnd As Long, _
           ByVal lpOperation As String, _
           ByVal lpFile As String, _
           ByVal lpParameters As String, _
           ByVal lpDirectory As String, _
           ByVal nShowCmd As Long) As Long
    
    Public Function OpenFile(File_Path As String, File_Name As String, File_Type As String) As Boolean
    
      Dim FTO As String
      Dim RetVal
      Dim ZeroStr As String
      
        ZeroStr = Chr$(0)
        FTO = File_Path & File_Name & File_Type
        
        'Open the File
         RetVal = ShellExecute(&H0, "open", FTO, ZeroStr, ZeroStr, vbNormalFocus)
    
          'Check if an error occurred while opening the file
           If RetVal <= 32 Then
               'There was an error
                 Select Case RetVal
                   Case ERROR_NO_RES
                      Msg = "System is out of Resources"
                   Case SE_ERR_FNF
                      Msg = "File not found -" _
                          & Chr$(34) & File_Name & File_Type & Chr$(34)
                   Case SE_ERR_PNF
                      Msg = "Path not found -" _
                          & Chr$(34) & File_Path & Chr$(34)
                   Case SE_ERR_ACCESSDENIED
                      Msg = "Access denied"
                   Case SE_ERR_OOM
                      Msg = "Out of memory"
                   Case SE_ERR_DLLNOTFOUND
                      Msg = "DLL not found"
                   Case SE_ERR_SHARE
                      Msg = "A sharing violation occurred"
                   Case SE_ERR_ASSOCINCOMPLETE
                      Msg = "Incomplete or invalid file association"
                   Case SE_ERR_DDETIMEOUT
                      Msg = "DDE Time out"
                   Case SE_ERR_DDEFAIL
                      Msg = "DDE transaction failed"
                   Case SE_ERR_DDEBUSY
                      Msg = "DDE busy"
                   Case SE_ERR_NOASSOC
                      Msg = "No program is associated with " _
                           & "the file extension " & Chr$(34) & File_Type & Chr$(34)
                   Case ERROR_BAD_FORMAT
                      Msg = "Invalid EXE file or error in EXE image"
                   Case Else
                      Msg = "Unknown error"
                 End Select
               MsgBox Msg, vbCritical + vbOKOnly, "Open File Error"
           Else
               OpenFile = True
           End If
    
    End Function
    Example:
    RetVal = OpenFile("C:\Documents and Settings\Jim\My Documents\", "Sales Report", ".doc")

    Sincerely,
    Leith Ross

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1