ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Hyperlink to open .dot with a macro (https://www.excelbanter.com/excel-discussion-misc-queries/55800-hyperlink-open-dot-macro.html)

scottEfraz

Hyperlink to open .dot with a macro
 
I have a word .dot that has a simple macro when opened, but when I try to
open the .dot from a hyperlink the macro doesn't autostart. Any ideas?

Dave Peterson

Hyperlink to open .dot with a macro
 
I opened word and created a new test2.dot template file.

I put this code under the ThisDocument module:

Option Explicit
Private Sub Document_New()
MsgBox "hi from new"
End Sub
Private Sub Document_Open()
MsgBox "hi from open"
End Sub

I put a hyperlink in a cell in an excel worksheet that pointed to that test2.dot
file.

The Document_Open() event fired--but the actual template (test2.dot) opened--a
new document based on that template did NOT get created.

I think you may want to change your technique if you want a new document based
on that template to be created (and the Document_New event to fire).

I'd put a button on the worksheet that has a macro assigned to it. It could
create a new document based on that template.

Option Explicit
Sub testme()

Dim myCell As Range
Dim WdApp As Object
Dim testStr As String

Set myCell = ActiveSheet.Range("d12") 'or whatever you want

testStr = ""
On Error Resume Next
testStr = Dir(myCell.Value)
On Error GoTo 0

If testStr = "" Then
MsgBox "Template file not found!"
Exit Sub
End If

On Error Resume Next
Set WdApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Err.Clear
Set WdApp = CreateObject("Word.Application")
End If

With WdApp
.Visible = True
.Documents.Add Template:=myCell.Value, _
NewTemplate:=False, DocumentType:=0
End With

Set WdApp = Nothing

End Sub

scottEfraz wrote:

I have a word .dot that has a simple macro when opened, but when I try to
open the .dot from a hyperlink the macro doesn't autostart. Any ideas?


--

Dave Peterson


All times are GMT +1. The time now is 03:20 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com