Open Word document from VB?

jetyun
jetyun used Ask the Experts™
on
I m not sure which method is recommended to open a word document from VB? Pls advise.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Do like this.
'first goto-project-references-select microsoft word 9.0 object library-ok

dim oword as object
dim wrddoc as object
dim blnrunning as boolean

On Error Resume Next
Set oword = GetObject(, "word.application")

If Err.Number <> 0 Then
Set oword = CreateObject("word.application")
oword.Visible = True
blnrunning = False
Else
blnrunning = True
End If

AppActivate (oword)

Set WRDDOC = oword.Documents.Add
Commented:
another method is to use the shellexecute api.



Public 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 Const SW_SHOWNORMAL = 1




then you can launch word and open a specified file using the code below:

call ShellExecute(me.hWnd, "open", "C:\whatever\test.doc", vbNullString, vbNullString, SW_SHOWNORMAL)

Add reference to Microsoft Word Object Library 8.0
PAste the code in a button click event.


Dim Obj As New Word.Application
Obj.Documents.Open "Whole path of your document"
Obj.Visible = True
Can it be done without a refrence to Word?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial