• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

Create, protect and mail report as word document

This is what i want to do:

Create a report and e-mail it from Access - this i can do.  The problem is that this document must be protected as read only .

I have 2 options.

Option 1
Create and save the report in access and then open it through access with MS-word, set the protection by hand BUT then i need to mail the document from a stored location THIS I CAN'T DO !!!

Option 2
Create the report, set the protection property from access and then mail it.  CAN I SET THE PROTECTION FROM ACCESS?

Help urgently needed!

1 Solution
Hi Tryna,

After you created a Word document Object in Access, try:
WordObj.WritePassword = "MyPass"
WordObj.Document.WritePassword = "MyPass"

Where WordObj is the reference to you Word Object.

I never tried it before, so please let me know if it works.

Hope this helps,

To check if the method I suggested is working, you can test the value of WriteReserved:

Debug.print WordObj.WriteReserved  'Should display False
WordObj.WritePassword = "MyPass"
Debug.print WordObj.WriteReserved  'Should display True

Hope this helps,

I dont know if this will work for sure, but have you tried doing a mail merge?  I believe you could create the template and password protect it.
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Tryna,  I know this will work from Access 97 as I've used it before.  It sends the document as an outlook attachment.

Option Compare Database
Option Explicit

    Private objWord As Word.Application
    Const FILE_SHARE_READ = &H1

    Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Sub CreateInfractionDocument()
    On Error GoTo Proc_Err
    Dim strFileLoc  As String
    strFileLoc = "d:\lookat\temp\x.doc"
    Set objWord = New Word.Application
    objWord.ActiveDocument.SaveAs strFileLoc
    SetFileAttributes strFileLoc, FILE_SHARE_READ
    SendEMailMsg False, "baderms@state.gov", "Subject Line", "", strFileLoc
    Set objWord = Nothing
    Exit Sub

    MsgBox Err.Number & " - " & Err.Description
    Resume Proc_Exit
End Sub

Sub SendEMailMsg(DisplayMsg As Boolean, Receiver As String, Optional strSubject, Optional strCC, Optional AttachmentPath)
    On Error Resume Next
    Dim objOutlook          As Outlook.Application
    Dim objOutlookMsg       As Outlook.MailItem
    Dim objOutlookRecip     As Outlook.Recipient
    Dim objOutlookAttach    As Outlook.Attachment
    Dim CallDescription     As String
    ' Create the Outlook session.
    Set objOutlook = CreateObject("Outlook.Application")
    ' Create the message.
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    With objOutlookMsg
        ' Add the To recipient(s) to the message.
        Set objOutlookRecip = .Recipients.Add(Receiver)
        objOutlookRecip.Type = olTo
'        Add the CC recipient(s) to the message.
        If IsMissing(strCC) Then
            strCC = ""
        End If
        'If NoValue(strCC) = False Then
        '    Set objOutlookRecip = .Recipients.Add(strCC)
        '    objOutlookRecip.Type = olCC
        'End If
        ' Add the From recipient(s) to the message.
        'objOutlookMsg.SentOnBehalfOfName = GetSetting(App.EXEName, "Defaults", "EMailFrom")
        ' Set the Subject, Body, and Importance of the message.
        .Subject = strSubject
        .Importance = olImportanceHigh  'High importance
        'Request Deleivery Report
        'If GetSetting(App.EXEName, "Defaults", "EMailReceipt") = 1 Then
        '    .OriginatorDeliveryReportRequested = olTrackingDelivered
        'End If
        'Request Read Report
        'If GetSetting(App.EXEName, "Defaults", "EMailRead") = 1 Then
        '    .ReadReceiptRequested = olTrackingRead
        'End If
        ' Add attachments to the message.
        If IsMissing(AttachmentPath) Then
            AttachmentPath = ""
        End If
        'If NoValue(AttachmentPath) = False Then
            Set objOutlookAttach = .Attachments.Add(AttachmentPath)
        'End If
        ' Should we display the message before sending?
        If DisplayMsg Then
        End If
    End With

    Set objOutlook = Nothing
End Sub
OR instead of sending an rtf which is editable, you can send the report in snapshot format. The user must have the snapshot viewer installed to view the document, but I alway include a link to Microsoft's download site whenever I send a .snp file.

The sendObject command line looks like this

DoCmd.SendObject acSendReport, strdocname, "SnapShot Format", , , , , Chr(13) & "To view this document you must install SnapShot Viewer which can be downloaded from:" & Chr(13) & Chr(13) & "http://www.microsoft.com/accessdev/prodinfo/snapdl.htm"

The last line includes the site for download. This method is extremely EASY and once you install it, you're pretty much set.

Hope this helps you
TrynaAuthor Commented:

Your solution put me on the right track although it's not exactly what i need to do.  Your solution sets the property to read only which means that the mail recipient can still open and change the document but then must save it under a different name.

To clarify, the document that i'm trying to create and mail is a type of certifcate for laboratory results.  The recipient may under no circumstance change any of the information contained in the document.  In other words, i need to set the property that you find in MS word under:

Tools > Protect Document - The protect document form appears where you can protect it for Tracked Changes, Comments of Forms.  I need to do protection for Forms with a password.

As a first time user of any form of variables of type OBJECT, it took me a while to figure out that there is something like "References" to be set in the tools menu to be able to access the other Microsoft applications for Automation.  Luckily i'm a fast learner. Below is the code you provided followed by the code as i changed it.

   objWord.ActiveDocument.SaveAs strFileLoc
I changed it as follows :

   objWord.Documents.Open "c:\test\test.doc"
   objWord.ActiveDocument.Protect wdAllowOnlyFormFields

If there is another (easier) way or ideas on how to add a password to the document, i would like to know about it BUT for posting the closest thing to a solution (i.e. 99.9%) The points  - with compliments !!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now