?
Solved

Problem with MS Word Macro

Posted on 2011-03-25
6
Medium Priority
?
483 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

770 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