none
how to open a word document from VBA code

    Question

  • Hi ,

    I am trying to open a word document from my VBA code.
    I have tried the below piece of code given in http://msdn.microsoft.com/en-us/library/aa220317(office.11).aspx link
    Documents.Open
     FileName
    
    But i am not getting "Documents" in my VBA intellisence.

    kindly help me how can i open a word document.


    Thanks in advance
    Sathya N
    Tuesday, August 11, 2009 3:25 PM

All replies

  • If you are running your vba code from within a word document or a template then use the following code

    Public Sub Test
    	Dim o as Application.Document
    	Set o = Application.Documents.Open FileName:="C:\MyFiles\MyDoc.doc", ReadOnly:=True
    End Sub


    otherwise, you need to create a word application before opening the document

    Public Sub Test
    	Dim app as Object
    	Dim o as object
    	Set app = getObject(,"Word.Application")
    	If app = Nothing Then
    		Set app = CreateObject("Word.Application")
    	End If
    	app.visible = True
    	set o = app.Documents.Open FileName:="C:\MyFiles\MyDoc.doc", ReadOnly:=True
    End Sub

    • Proposed as answer by vksampath Monday, September 21, 2009 3:36 AM
    Saturday, August 22, 2009 3:13 PM