Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

Problem with MS Word Macro

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
HLR6S
Asked:
HLR6S
  • 3
  • 3
1 Solution
 
Chris BottomleyCommented:
Can you provide a sample file with anything sensitive removed?

Chris
0
 
HLR6SAuthor Commented:
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
 
Chris BottomleyCommented:
Without looking at the document initially but based off your comment ... unprotect and reprotect within the macro is one option

Chris
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HLR6SAuthor Commented:
That sounds like a good idea, do you know what code I would use to do this?
0
 
Chris BottomleyCommented:
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
 
HLR6SAuthor Commented:
That worked like a charm.  Thank you very much.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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