Solved

Problem with MS Word Macro

Posted on 2011-03-25
6
467 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

803 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