PDA

View Full Version : Losing template during email



davidboutche
12-18-2009, 08:51 AM
I'm using the following code to email the active document to another user but when it does this it seems to lose it's code and turns it into a normal word document, not a template. It show the missing refererence to the original file (.doc).


Sub eMailActiveDocument()
Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document


Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(0)
Set Doc = ActiveDocument
Doc.Save

With EmailItem
.Subject = "A Caution Certificate has been issued"
.Body = "A " & ActiveDocument.Bookmarks("appbook").Range.Text & " has been issued to:" & vbCrLf & _
"Surname: " & ActiveDocument.Bookmarks("surnamebook").Range.Text & vbCrLf & _
"First Names: " & ActiveDocument.Bookmarks("forenamebook").Range.Text & vbCrLf & _
"Address: " & ActiveDocument.Bookmarks("addressbook").Range.Text & vbCrLf & _
"Post Code: " & ActiveDocument.Bookmarks("postcodebook").Range.Text & vbCrLf & _
"DOB: " & ActiveDocument.Bookmarks("dobbook").Range.Text & vbCrLf & _
"Sex: " & ActiveDocument.Bookmarks("sexbook").Range.Text & vbCrLf & _
"Age: " & ActiveDocument.Bookmarks("agebook").Range.Text & vbCrLf & _
"Ethnicity: " & ActiveDocument.Bookmarks("ethnicitybook").Range.Text & vbCrLf & _
"Place of Birth: " & ActiveDocument.Bookmarks("pobbook").Range.Text & vbCrLf & _
"Occupation: " & ActiveDocument.Bookmarks("occupationbook").Range.Text & vbCrLf & _
"Custody Number: " & ActiveDocument.Bookmarks("custodybook").Range.Text & vbCrLf & vbCrLf & _
"Offence Details" & vbCrLf & vbCrLf & _
"Offence: " & ActiveDocument.Bookmarks("offencebook").Range.Text & vbCrLf & _
"Time,Date and Location of Offence: " & ActiveDocument.Bookmarks("datebook").Range.Text & _
vbCrLf & "Crime Number: " & ActiveDocument.Bookmarks("crimebook").Range.Text & vbCrLf & _
"Additional Offence: " & ActiveDocument.Bookmarks("secondoffencebook").Range.Text & vbCrLf & _
"Time,Date and Location of second offence: " & _
ActiveDocument.Bookmarks("seconddatebook").Range.Text & vbCrLf & _
"Second Crime Number: " & ActiveDocument.Bookmarks("secondcrimebook").Range.Text & _
vbCrLf & vbCrLf & "Rank: " & ActiveDocument.Bookmarks("adminrankbook").Range.Text & vbCrLf & _
"Collar Number: " & ActiveDocument.Bookmarks("admincollarbook").Range.Text & vbCrLf & _
"Station Code: " & ActiveDocument.Bookmarks("adminstationbook").Range.Text & vbCrLf & _
"Date Administered: " & ActiveDocument.Bookmarks("admindatebook").Range.Text
.To = ".Non Court Disposals"
.Importance = 1 'Or olImprotanceHigh Or olImprotanceLow
' .Attachments.Add Doc.FullName
' .Send
.Display
End With

Application.ScreenUpdating = True
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing

MsgBox ("Non Court Disposals have been notified of the caution being issued. " & _
"If you are asked if you want to allow a program to send messages " & _
"on your behalf click 'yes'")
End Sub



Is there anyway I can adapt this code so it keeps it's template properties? I need to keep the code to ensure the buttons work when the next user gets it.

Thanks

Edited 21-Dec-09 by geekgirlau. Reason: insert line breaks

lucas
12-18-2009, 09:30 AM
David, is the activedocument a template .dot or a regular .doc file?

When you create a document from a template or .dot file the code does not get copied to the cloned document, only a reference to the template.

The best way to do this in my opinion is to not use a template and just make a copy of the .doc file each time.

Sharper marbles will probably give better background on template useage but that is the way I have been handling it when I want the code to go with the document.

lucas
12-18-2009, 09:46 AM
David, attached is a sample of a document that I use for this.

You run the macro when it opens from a menu at the top....next to help.

When you have your copy made you can either leave the module or delete it as you desire. You would also need to delete the menu item.

I sometimes run it from document open(commented out in the example) so that I don't have to mess with the menu and then delete the code from the thisdocument module and the function that is in the module.

Hope this helps. Maybe somone has a better solution but I have been here before and it seems that making a copy of the doc file is the best way to do it if you want your code to follow.....

see attached.

fumei
12-21-2009, 01:06 PM
Accessible VBA code is in three possible places.

1. Normal.dot (or any other global .DOT file) directly accessible to the document.

2. The template .DOT file used to create the document. This must be accessible to the document.

3. In a code module in the document itself.

If code is in the template, and the document is emailed without the template (.DOT), then that code is not available.

Steve's (Lucas) example has the code in the document itself. This is really the only way to ensure the code will be accessible when the document is emailed.

BTW: are these real bookmarks, or are they formfields. I suspect they are actually formfields. If this is correct, it is better to use formfields as the objects. Use .result, rather than .Range.Text of the bookmark. Like this:
Dim DocFF As FormFields
Set DocFF = ActiveDocument.FormFields
With EmailItem
.Subject = "A Caution Certificate has been issued"
.Body = "A " & DocFF("appbook").Result & " has been issued to:" & _
vbCrLf & _
"Surname: " & DocFF("surnamebook").Result & _
vbCrLf & _
"First Names: " & DocFF("forenamebook").Result & vbCrLf & _
"Address: " & DocFF("addressbook").Result & vbCrLf & _
"Post Code: " & DocFF("postcodebook").Result & vbCrLf & _
"DOB: " & DocFF("dobbook").Result & vbCrLf & _
"Sex: " & DocFF("sexbook").Result & vbCrLf & _
"Age: " & DocFF("agebook").Result & vbCrLf & _
"Ethnicity: " & DocFF("ethnicitybook").Result & vbCrLf & _
"Place of Birth: " & DocFF("pobbook").Result & vbCrLf & _
"Occupation: " & DocFF("occupationbook").Result & vbCrLf & _
"Custody Number: " & DocFF("custodybook").Result & vbCrLf & vbCrLf & _
"Offence Details" & vbCrLf & vbCrLf & _
"Offence: " & DocFF("offencebook").Result & vbCrLf & _
"Time,Date and Location of Offence: " & DocFF("datebook").Result & _
vbCrLf & "Crime Number: " & DocFF("crimebook").Result & vbCrLf & _
"Additional Offence: " & DocFF("secondoffencebook").Result & vbCrLf & _
"Time,Date and Location of second offence: " & _
DocFF("seconddatebook").Result & vbCrLf & _
"Second Crime Number: " & DocFF("secondcrimebook").Result & _
vbCrLf & vbCrLf & "Rank: " & DocFF("adminrankbook").Result & vbCrLf & _
"Collar Number: " & DocFF("admincollarbook").Result & vbCrLf & _
"Station Code: " & DocFF("adminstationbook").Result & vbCrLf & _
"Date Administered: " & DocFF("admindatebook").Result
.To = ".Non Court Disposals"
.Importance = 1 'Or olImprotanceHigh Or olImprotanceLow
' .Attachments.Add Doc.FullName
' .Send
.Display
End With

davidboutche
01-12-2010, 03:45 AM
OK thanks for the helpings. What if I left the orginial template in a folder that all network users had access to? Would it still reference properly then?