Open Word template hyperlink as a new Word document (.docx), not as the original template (.dotx)

SaraWitch

Active Member
Joined
Sep 29, 2015
Messages
377
Office Version
  1. 365
Platform
  1. Windows
Hello peeps,

I have various buttons that I have hyperlinked to Word templates. When opening the Word templates from File Explorer (or other Word hyperlinks), these open as a new document ("Document 1"), as they should. However, from the Excel hyperlinks, they open as the original template. I want to preserve the integrity of the original template (GDPR risk!).

I found a solution of creating a shortcut of the template and hyperlinking that, but this no longer works - it still opens the template. I've also found a couple of VBA codes for links within a cell (which don't work for me), but not for a button.

Any help would be greatly appreciated, thank you :)
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
When you say buttons, do you mean Shapes? If so, see if this approach works for you - try it on one of the shapes.

1. Remove the link from the shape (see note below).

2. Edit the shape and set its Alt Text to the full name of the Word template file, including the ".dotx" extension. The folder path is optional - if omitted, the code below expects the template to be in the same folder as the workbook, but the code could be changed to look for the template in any folder.

3. Paste the following code into a standard module in the Excel workbook (.xlsm file):

VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As LongPtr) As LongPtr
#Else
    Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
#End If


Public Const wdNewBlankDocument = 0


Public Sub Open_Word_Doc_From_Template()

    Dim WordApp As Object 'Word.Application
    Dim WordDoc As Object 'Word.Document
    Dim templateFile As String
    
    templateFile = ActiveSheet.Shapes(Application.Caller).AlternativeText
    
    Set WordApp = Nothing
    On Error Resume Next
    Set WordApp = GetObject(, "Word.Application")
    On Error GoTo 0
    If WordApp Is Nothing Then
        Set WordApp = CreateObject("Word.Application")
    End If
    WordApp.Visible = True
    
    If InStr(templateFile, "\") = 0 Then templateFile = ThisWorkbook.Path & "\" & templateFile
    
    Set WordDoc = WordApp.Documents.Add(template:=templateFile, NewTemplate:=False, DocumentType:=wdNewBlankDocument)
    SetForegroundWindow WordDoc.Windows(1).hwnd
    
    Set WordApp = Nothing
    
End Sub

4. Assign the Open_Word_Doc_From_Template macro to the shape.

Note - You have to remove the link (Step 1) because it takes precedence over the assigned macro (Step 4), stopping it from being run. Removing the link means you lose the screentip when you hover over the shape.
 
Upvote 0
Solution
I am continually amazed and grateful at the expertise and skill on this forum!

This worked perfectly (and for all my 'quick access buttons' (yes, shapes)) - thank you so very much, @John_w. This is a game-changer! :biggrin:
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,153
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top