Hi all!
Is it possible to open a MS Word documento trough a code in VBA??
any help appreciated!
thx
Hi all!
Is it possible to open a MS Word documento trough a code in VBA??
any help appreciated!
thx
Have a look at the help for CreateObject
Martin
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.
Example:![]()
'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
RetVal = OpenFile("C:\Documents and Settings\Jim\My Documents\", "Sales Report", ".doc")
Sincerely,
Leith Ross
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks