PDA

View Full Version : Auto Attatch Macro to New Document



Acefondu
02-04-2008, 07:58 AM
Most of this verbiage is new to me, so I'll be as descriptive as I can.

I have a document I want others to fill out, it is a protected document that I created. I have to have a macro on for people to be able to click on the HYPERLINKS I've set since it's protected. I've been able to create such a macro no problem.

The problem is I have to email it out, and when that happens the macro is no longer attached since it's a template doc.

What VBA code can I use to permanently attach the macro to the file?

fumei
02-04-2008, 10:07 AM
If the code is in the template, and you email out a document cloned from it, if the template itself is not available, then your code will not execute.

The code must be in the document itself if the template itself is not available.

burtburt
04-26-2008, 10:16 AM
If the code is in the template, and you email out a document cloned from it, if the template itself is not available, then your code will not execute.

The code must be in the document itself if the template itself is not available.

Is there a way to have a template generate a doc with code in the doc?

I've been searching for this for a few hours and can't seem to find the write keyword.

burtburt
04-26-2008, 10:19 AM
I need to put a button on the doc and have it open a form in the doc and then have the form pass the data to a sub within the doc.

fumei
04-28-2008, 05:01 AM
Again, if the template that created the document is not available, then...well, it is not available. Code can be executed in a document under two conditions.

1. the code is in the .dot file AND the .dot file is available.

2. the code is in the document itself.

This may be a case for not using a template, and simply put your stuff into a document for sending out.

OR....rethink things.

Yes, you can write actual code, using code. It is slow and incremental.

Sub AddCode_ByCode()
With Application.VBE.ActiveVBProject
' add a new module
With .VBComponents.Add(vbext_ct_StdModule)
' change the module name
.Name = "yadda"

' use the code module
With .CodeModule

' add a global variable declaration
.InsertLines 2, "Public strWhatever As String"

' add a procedure
.InsertLines 3, "Sub HoHum()"
.InsertLines 4, "Dim j As Long"
.InsertLines 5, "Dim oPara As Paragraph"
.InsertLines 6, "For Each oPara in ActiveDocument.Paragraphs"
.InsertLines 7, "j = j + 1"
.InsertLines 8, "Next"
.InsertLines 9, "Msgbox j"
.InsertLines 10, "End Sub"
End With
End With
End With
End Sub


And....there it is.