Solved

Problem with MS Word Macro

Posted on 2011-03-25
6
462 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
Comment Utility
Can you provide a sample file with anything sensitive removed?

Chris
0
 

Author Comment

by:HLR6S
Comment Utility
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
Comment Utility
Without looking at the document initially but based off your comment ... unprotect and reprotect within the macro is one option

Chris
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:HLR6S
Comment Utility
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
Comment Utility
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
Comment Utility
That worked like a charm.  Thank you very much.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now