Solved

Problem with MS Word Macro

Posted on 2011-03-25
6
472 Views
Last Modified: 2012-05-11
I have been trying to set up a Word document that automatically emails one page of the documents to various people in our organization.  Between Google searches and help from experts on this site, I had it working perfectly.  Then the page that I was supposed to have automatically email was changed to a form within Word.  The form has multiple checkboxes, drop down menus, and calendar selection boxes.  Now when I run the script the form is emailed blank no matter how it is filled out.  And if I fill the form out and save it before attempting to send, a blank page is emailed out.  Does anyone see what I should change in the code to fix this?
Sub SendDocAsMail()

Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

'Start Outlook if it isn't running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
    Set oOutlookApp = CreateObject("Outlook.Application")
End If

'Create a new message
Set oItem = oOutlookApp.CreateItem(olMailItem)

oItem.Subject = "New or Transferred User"
oItem.Recipients.Add "emailaddress@domain.com"
'oItem.Recipients.Add "AnotherAddressIfYouWant.domain.com"

'Allow the user to write a short intro and put it at the top of the body
'Dim msgIntro As String
'msgIntro = InputBox("Write a short intro to put above your default " & _
            "signature and current document." & vbCrLf & vbCrLf & _
            "Press Cancel to create the mail without intro and " & _
            "signature.", "Intro")

'Copies the first page
ThisDocument.GoTo(wdGoToPage, 4).Select
ThisDocument.Bookmarks("\page").Range.Copy

'Set the WordEditor
Dim objInsp As Outlook.Inspector
Dim wdEditor As Word.Document
Set objInsp = oItem.GetInspector
Set wdEditor = objInsp.WordEditor

'Write the intro if specified
Dim i As Integer
If msgIntro = IsNothing Then
    i = 1
    'Comment the next line to leave your default signature below the document
    wdEditor.Content.Delete
Else
    'Write the intro above the signature
    wdEditor.Characters(1).InsertBefore (msgIntro)
    i = wdEditor.Characters.Count
    wdEditor.Characters(i).InlineShapes.AddHorizontalLineStandard
    wdEditor.Characters(i + 1).InsertParagraph
    i = i + 2
End If

'Place the current document under the intro and signature
wdEditor.Characters(i).PasteAndFormat (wdFormatOriginalFormatting)

'Display the message
oItem.Display

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
Set objInsp = Nothing
Set wdEditor = Nothing

End Sub

Open in new window

0
Comment
Question by:HLR6S
  • 3
  • 3
6 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35222093
Can you provide a sample file with anything sensitive removed?

Chris
0
 

Author Comment

by:HLR6S
ID: 35233573
Here is the document as requested.  I had to remove the macro because experts exchange would not allow me to upload a macro enable document.  The macro that I use is saved in this post under the code section. The only change that would need to be made too the code I have here is to set it to copy page 1 instead of page 4, I had to delete the rest of the document to remove sensitive material.

 I think the problem may be that to allow the form to work correctly, I have been putting it in a protected mode that only allows editing to the form.  If I turn the protection mode off then any changes I make to the form are put into the email.  Any ideas on how to make it work in protected mode?
PAFnewhire-Experts-Exchange.docx
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35234175
Without looking at the document initially but based off your comment ... unprotect and reprotect within the macro is one option

Chris
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:HLR6S
ID: 35234296
That sounds like a good idea, do you know what code I would use to do this?
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 35234497
Two lines ... one to unprotect then one to protect:

ThisDocument.Unprotect Password:=""
wdEditor.Characters(i).PasteAndFormat (wdFormatOriginalFormatting)
ThisDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""

Chris
0
 

Author Comment

by:HLR6S
ID: 35234620
That worked like a charm.  Thank you very much.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question