PDA

View Full Version : Solved: Multiple tasks for one command button



tonyam
10-17-2008, 07:49 AM
Is it possible to call a macro and open a word doc from the same click of a command button? If so, how do you code it?

Thanks in advance

Kenneth Hobs
10-17-2008, 08:05 AM
You mean call some other sub in that sub? Yes, it is just a sub itself.

You can use this to open a DOC file.
Shell "CMD /c c:\MyDocFile.doc", vbNormalFocus

Bob Phillips
10-17-2008, 08:06 AM
You code it to start a Word session, or grab the existing session. The code to do the Word stuff is a macro.


Sub TestViaExcel()
Dim wdApp As Object
Dim wdDoc As Object

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Set wdApp = CreateObject("Word.Application")
If wdApp Is Nothing Then

MsgBox "Error"
Exit Sub
End If
End If
On Error GoTo 0

wdApp.Visible = True
wdApp.documents.Open ("C:\tes\myDoc.doc")

'do your stuff

Set wdDoc = Nothing
wdApp.Quit

Set wdApp = Nothing

End Sub

tonyam
10-17-2008, 08:12 AM
This is what i have entered and it is saying unexpected end sub

Private Sub CommandButton6_Click()
Sub TestViaExcel()
Dim wdApp As Object
Dim wdDoc As Object

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Set wdApp = CreateObject("Word.Application")
If wdApp Is Nothing Then

MsgBox "Error"
Exit Sub
End If
End If
On Error GoTo 0

wdApp.Visible = True
wdApp.documents.Open ("C:\Mail Merge D & N letter v2 2000.doc")

Call savetonewworkbook

Set wdDoc = Nothing
wdApp.Quit

Set wdApp = Nothing

End Sub

Kenneth Hobs
10-17-2008, 08:24 AM
You defined two subs in the first 2 lines. Delete the word Sub if you wanted to call TestViaExcel.