PDA

View Full Version : Word 2013 template document with command buttons and VBA code (NOT macros)



TimCook
08-22-2016, 03:10 PM
Hi All,

Issue is this. I have created a word 2013 template document (and saved it as a .dotm) which contains three command buttons with associated VBA code. This code performs as expected when the original document is opened but is not present in the Project(Document2)->Microsoft Word Objects->ThisDocument section of the VBA editor when a 'new document from template' is opened.

Am I being stupid (probably :) ) or is there a way to have the VBA code copied when a new document is opened from a template? Alternatively can the command buttons be edited to point to the corresponding code in the TemplateProject of the original file?

I would dearly love to stop bashing my head against a stone wall over this one...

Tim

gmaxey
08-22-2016, 07:15 PM
Option Explicit
'Note this code needs to be run from a template
Sub AutoNew()
Dim oDoc As Word.Document
Dim strCode As String
Set oDoc = ActiveDocument
strCode = ReadModule
With oDoc.VBProject.VBComponents("ThisDocument").CodeModule
.DeleteLines 1, .CountOfLines
.AddFromString strCode
End With
lbl_Exit:
Exit Sub
End Sub
Function ReadModule() As String
Dim VBProj As Object 'VBIDE.VBProject
Dim VBComp As Object 'VBIDE.VBComponent
Dim oTemplate As Document
Set oTemplate = ThisDocument
Set VBProj = oTemplate.VBProject
Set VBComp = VBProj.VBComponents("ThisDocument")
On Error Resume Next
ReadModule = VBComp.CodeModule.Lines(1, VBComp.CodeModule.CountOfLines)
lbl_Exit:
Set oTemplate = Nothing
Set VBProj = Nothing
Set VBComp = Nothing
Exit Function
End Function

gmaxey
08-23-2016, 12:17 PM
Alternatively, you can process the InlineShapes in the document:


Dim oILS As InlineShape
For Each oILS In ActiveDocument.InlineShapes
If oILS.OLEFormat.Object.Name = "CommandButtonPdf" Then
oILS.OLEFormat.Object.Enabled = False
End If
Next oILS

TimCook
08-24-2016, 08:35 AM
Thanks for the code example Gary, it transfers the VBA from the template into the new document. :thumb

But...

I got the following error message and the code won't compile-
'Member already exists in an object module from which this object module derives'


I searched for code to remove the template reference once the VBA has been copied into the current document with no luck so I modified your example very slightly, changing

ReadModule = VBComp.CodeModule.Lines(1, VBComp.CodeModule.CountOfLines)
to

ReadModule = VBComp.CodeModule.Lines(1, VBComp.CodeModule.CountOfLines-30)

Problem solved, I thought...

The command buttons now work however it seems that all VBA code contained in the template runs twice (???), both the on_click events from the buttons and the lines I have put in Document_New(). What's more the VBA is running from the template document rather than the doc created from it. :think:

I have zero experience of using the VBIDE model that your brilliant example is presumably(?) drawn from - I will have to search Google a lot more to get same background.

Tim



Option Explicit
'Note this code needs to be run from a template
Sub AutoNew()
Dim oDoc As Word.Document
Dim strCode As String
Set oDoc = ActiveDocument
strCode = ReadModule
With oDoc.VBProject.VBComponents("ThisDocument").CodeModule
.DeleteLines 1, .CountOfLines
.AddFromString strCode
End With
lbl_Exit:
Exit Sub
End Sub
Function ReadModule() As String
Dim VBProj As Object 'VBIDE.VBProject
Dim VBComp As Object 'VBIDE.VBComponent
Dim oTemplate As Document
Set oTemplate = ThisDocument
Set VBProj = oTemplate.VBProject
Set VBComp = VBProj.VBComponents("ThisDocument")
On Error Resume Next
ReadModule = VBComp.CodeModule.Lines(1, VBComp.CodeModule.CountOfLines)
lbl_Exit:
Set oTemplate = Nothing
Set VBProj = Nothing
Set VBComp = Nothing
Exit Function
End Function

gmaxey
08-24-2016, 07:11 PM
Tim,

Can you post your template and steps you are taking produce the problem?

TimCook
08-26-2016, 07:02 AM
Hi Gary,

Attached is a zip of the CA.dotm template file I am using.
16947


Tim,

Can you post your template and steps you are taking produce the problem?

gmaxey
08-26-2016, 05:29 PM
Firing twice? I don't see that. The Document_New fires once in the Template Project and once in the document itself because you copied it there.

Perhaps I don't understand the issue.