Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening a word document using Excel VB 3

Status
Not open for further replies.
Aug 29, 2005
144
Anyone know if this is possible? I want to be able to open a word proforma document by clicking a button in an excel spreadsheet, but after reading the "help" files, I'm a little confused as to whether it is possible to do. Any help would be appriciated!
Cheers
James
 
Replies continue below

Recommended for you

James,
There are several ways to do this.
One method is to select a cell, right click and
select HYPERLINK. Browse to your word document and click OK. Works like any other hyperlink. Word will open the document.

You can also add an Autoshape to your spreadsheet and assign a hyperlink via the right click as well.
 
Thanks WMO! Didn't think of that! I'd still be interested to know if there is a way to open a word document (or any other document for that matter) from a VB Macro.
 
FYI I found some code to open word docs from VB

Sub OpenWord()
fNameAndPath = "C:\***PATH***\file.doc"
Set wordApp = CreateObject("Word.Application")
wordApp.Documents.Open (fNameAndPath)
wordApp.Visible = True
End Sub

Opening other files is a little more difficult.
 
You can readily open any document that is associated with an application by using the FileProtocol handler. I use this function:
Code:
Public Function OpenDocument(DocumentWithPath As String) As Long
    OpenDocument = Shell("RUNDLL32.EXE URL.DLL,FileProtocolHandler " & DocumentWithPath, vbNormalFocus)
End Function
From anywhere in your code:
Code:
OpenDocument "c:\test\d1.vsd"
On my machine .vsd files are associated with Visio, so running the above code opens the file in a new instance of Visio

Good Luck
johnwm
________________________________________________________
To get the best from these forums read faq731-376 before posting

Steam Engine enthusiasts:
 
Not only is it possible to open any Microsoft Office product from a VB Macro, you can also control it too. My first VB macro opened Access, searched a table for matching records, wrote to the table if there was no duplicate record, then opened Word and performed a mail merge and allowed the user to print and save the mail merge before closing Word again and terminating the macro.

I have no idea how I did it, it was back in early 1999. I remember using the VB Help and ObjectBrowser a lot, though. (Particularly their sample code.)
 
I did something like this a while back in order to launch a new document from a word template, though it's probably not too elegant to someone who really knows what they are doing.

I had a few problems with getting it to launch a new document from the template rather than just opening the template and, in this case, the template must be located in the same directory as the spreadsheet. However here is what I ended up with:



Private mobjWordApp As Word.Application, wbXL As excel.Workbook

Private Sub CommandButton1_Click()

Dim templName As String
Dim tPath As String

tPath = ThisWorkbook.Path
templName = "filename.dot"

Set mobjWordApp = New Word.Application

With mobjWordApp
.Visible = True
.WindowState = wdWindowStateMaximize
.Documents.Add Template:=(tPath & Application.PathSeparator _
& templName), NewTemplate:=False, DocumentType:=0

End With

End Sub


Hope this is not too late to be of some use,

G
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor