Open a Word Document

Hello i need to find out how to open a word document from my access database any one know a good solution.

i have an ole object and you can drag and drop the file onto it but i need a way to have it load up in Word so it can be read/written/printed via word 2003.




Answer this question

Open a Word Document

  • seanklindt

    You can use Word objects to open a word document

    Make sure that you include the Word 9.0 Object Library and then add the following code

    Dim oWd as Word.application

    Dim oDoc as Word.Document

    Set oWd= New Word.application

    Set oDoc = oWd.open(C:\MyDoc.doc")

    '--- Now you get all the Word objects and you can print and play with them

    ' -- Finally destroy your objects

    Set oDoc = nothing

    Set oWd = nothing

    Shasur



  • Yen-Heng Chen

    Thanks all both solutions seem to be perfect i have experience using the open file command though so i'll most likely end up using that 1.

    thanks again for the help



  • ZuesBigglo

    The following code actually opens a Word document in Microsoft Office Word 2003 from a ASP.NET 2005 /Framework 2.0 Webform.

    Imports System

    Imports Microsoft.Office.Interop.Word

    Imports System.Windows.Forms

    Partial Class Test_Area_OpenDoc

    Inherits System.Web.UI.Page

    Public Shared Sub DocUp()

    Dim oWd As Microsoft.Office.Interop.Word.Application

    Dim oDoc As Microsoft.Office.Interop.Word.Document

    oWd = New Microsoft.Office.Interop.Word.Application

    oDoc = oWd.Documents.Open("E:\MONSTER\Monster FTP SPECS.doc")

    '--- Now you get all the Word objects and you can print and play with them

    ' -- Finally destroy your objects

    oDoc = Nothing

    oWd = Nothing

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    DocUp()

    End Sub

    End Class

    There are some caveats with this however The is a problem I’m having is when I want to Save the form from Word I receive the following choices on a pop-up. I would just like to save the file.

    The pop-up.

    Open a Read Only Document
    Open a Local Copy and Merge your changes later
    Receive Notification when the original copy is available

    I choose “Open a Local Copy and Merge your changes later”
    So when I save the document I receive this pop-up”
    “The original copy of Monster FTP SPECS is still locked. Click OK to save to a different location. When the original copy is available, click Compare and Merge Documents on the Tools Menu to Merge your changes into the original.”

    I click OK and it saves the doc file as Monster FTP SPECS - for merge1.doc Not the original file name if you noticed.

    Then when I attempt to close the Office Word Window I receive this pop-up:

    “Changes have been made that effect the global template, Normal.doc. Do you want to save those changes

    I click Cancel and then I can close the Office Word Application and continue working in the ASP.NET word application..

    This will not be a pleasant user experience.

    So I’m working catching or avoiding this behavior. I’m sure that in the hierarchical ownership construct the file is Owned by the ASP.NET web app that originally made the call to the Microsoft.Office.Interop.Word.Application class.

    I’ll u[pdate this if there is a solution.


  • MysTiger

    There's a better way to open a word document using the Windows API.

    Copy this code to the top of your form/module under Option Compare Database...

    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

    Copy this as a function in your form/module....

    Private Sub OpenFile(File As String)
    On Error GoTo cmdOpenFile_Err
    'open the document in whatever application it requires
    ShellExecute 0, "open", File, vbNullString, vbNullString, vbNormalFocus
    Exit Sub
    cmdOpenFile_Err:
    MsgBox "An unexpected error has occurred.", vbOKOnly + vbCritical, "Error Conditon in Opening File"
    End Sub

    Then call....

    OpenFile("C:\MyDocuments\MyWordDoc.doc")

    This will cause word to open and the file to be displayed just as though someone double clicked the file. You don't need to know where word is stored as basically your asking windows to open the file for you.

    This will work for images, pdf, any document at all.

    They will all open in their default application.



  • Open a Word Document