PDA

View Full Version : Emailing book mark information



davidboutche
07-08-2009, 05:36 AM
I have a word form and most of the information is entered through a makro inserting the contents into bookmarks.

What I want to do is using a button, copy those bookmarks into an email and have it sent to a designated address.

Can anyone suggest some code for this please

David

lucas
07-08-2009, 07:08 AM
You could email directly from the userform but this should get you started with your request:

Option Explicit
Sub eMailActiveDocument()
Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set Doc = ActiveDocument
Doc.Save
With EmailItem
.Subject = "Insert Subject Here"
.Body = ActiveDocument.Bookmarks("bodytext").Range.Text & vbCrLf & ActiveDocument.Bookmarks("uuu").Range.Text
.To = "User@Domain.Com"
.Importance = olImportanceNormal 'Or olImprotanceHigh Or olImprotanceLow
' .Attachments.Add Doc.FullName
' .Send
.Display
End With

Application.ScreenUpdating = True
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
End Sub

You don't say what version but it runs in 2003 ok.

fumei
07-08-2009, 01:14 PM
As it appears you want to get all bookmarks...

"copy those bookmarks into an email "

I would amend lucas' code, and extract the bookmark contents into a string first, THEN use it.

Dim strBookmarks As String
Dim oBM as Boomark
' this builds all the bookmark content
' into ONE string (with breaks)
For Each oBM In ActiveDocument.Bookmarks
strBookmarks = strBookmarks & _
oBM.Range.Text & vbCrLf
Next

' now you can use that string

' the other stuff for the MailItem
.Body = strBookmarks
' the other stuff



Steve.....underscore character please.

fumei
07-08-2009, 01:53 PM
From the OP: "I have a word form "

From lucas: "You could email directly from the userform "

We do NOT know if this is a userform. I suspect there is not a userform.

davidboutche
07-09-2009, 04:19 AM
Thanks,
The document is being designed in 2003 but most terminals that will use it run 2000.

A user form is used which captures most of the data and puts it into the final document. However, some of the information that I wish to be emailed will not be put into the form until a later date, hence I need a button on the document.

I've tried the original example you included but when I run the macro, it reports it can't find the object or library refering to (olMailItem).

Any ideas?

lucas
07-09-2009, 08:44 AM
In the visual basic editor you need to go to tools-references and check the box next to the microsoft outlook xx.0 object library.

mine is 2003 so the version is 11.0 instead of xx.

fumei
07-09-2009, 09:25 AM
"However, some of the information that I wish to be emailed will not be put into the form until a later date, hence I need a button on the document."

Could you explain that? Why do you need a button on the document?

davidboutche
07-10-2009, 03:26 AM
Changing the reference worked ok on the example thanks.

When I put the code into my own document, it doesn't select the text that has been put into the bookmarks.

I notice that on the example if I 'goto' bookmark it highlights a selection of your text, If I do it on my document it only puts the cursor at the start of the text.

The user form puts the text into the bookmarks with the code below:

ActiveDocument.Bookmarks("crimebook").Select
Selection.TypeText Text:=UserForm1.crimebox.Text


It doesn't seem to be selecting the text I want to email? any ideas?

davidboutche
07-10-2009, 04:25 AM
I think I've managed to solve that myself.

I removed :
.range.text from the code and it now seems to pick it up ok. should I have replaced that with another constraint?

The only unusual thing is it seems to want to save the document when executing the macro, but that's really not a problem.

What I would now like to do though is put additional text on to the email in between the bookmarked text. Is this possible?

Thanks for all the help. The template is coming along well now.

davidboutche
07-10-2009, 04:45 AM
i made a mistake, that did not work.. advice needed please. I tried lol.

fumei
07-10-2009, 11:02 AM
OK, let's start at the beginning. Please state clearly, and EXACTLY, what you want to do.

davidboutche
07-13-2009, 07:56 AM
Thanks for the help. I got there in the end. The code I was originally using was placing the text after a book mark. I have since learnt there are two type of book marks. I needed to used the closed book mark, which for the benefit of others reading is :
Dim bmRange As Range
Set bmRange = ActiveDocument.Bookmarks("urnbook").Range
bmRange.Text = UserForm1.urnbox.Text
ActiveDocument.Bookmarks.Add _
Name:="urnbook", _
Range:=bmRange


The project was formed of several stages.

The first was to capure information, insert it into a document which could then be worked on by the 1st level user.

It would then be emailed to the supervisor who would complete more of the document.

The supervisor would then open another form via a button and complete more details authorising the next stage of the process. This would then email a custom body of an email to a predfined address, made up of bookmarks.

Thanks for your help.

fumei
07-13-2009, 12:14 PM
"I have since learnt there are two type of book marks."

Indeed? What two types are there? AFAIK, there is only one type of bookmark.

davidboutche
07-14-2009, 02:09 AM
Maybe it was my poor understanding of the following quote



The most important thing you need to know when working with bookmarks in Word is that there are two “types” of bookmarks – “placeholder” bookmarks and “enclosing” bookmarks.
Before we proceed, and whenever you work with bookmarks, you should turn on display of bookmarks by going to Tools | Options | View and selecting “Bookmarks”. This makes it easier to see what's actually happening.

(1) Placeholder Bookmarks
If you click somewhere in the document and insert a bookmark it will look like a beam I – this is a “placeholder” bookmark. (2) Enclosing Bookmarks
Now, if you select some text and insert a bookmark it will look like the selected text is enclosed in square brackets ie: [selected text] – this is an “enclosing” bookmark.

fumei
07-14-2009, 10:09 AM
They are the same thing. You do not state where you got that quote, but IMO that is poor information.

A "placeholder" is a bookmark with a Range.Start = .Range.End.

That is, the start and the end of the bookmark are the same number .

An "enclosing" is a bookmark with .Range.End > .Range.Start.

That is, the end of the bookmark is AFTER the start, therefore there is some content between Start and End.

However, the are both exactly the same thing...a bookmark. If you put content into a "placeholder" (Start = End) bookmark...voila, the End is moved to hold the content, and .End is > .Start. Thus is "becomes" an "enclosing" bookmark.

It also works the other way. If you remove content from an "enclosing" bookmark...then Start = End, and it "becomes" a "placeholder.

A bookmark is a bookmark is a bookmark. There is only one type.

Bookmarks are named ranges.

If you want to just point to a SINGLE character - i.e. for location purposes, then all you need is one number. Thus, .Start = .End. You do not need anymore.

If you want to point to more than one character, then - obviously - the .End has to be greater than the .Start.

They are still the same object, regardless of the values of Start and End. A bookmark is a named range. Period.

The quoted text is true - but gives IMO an inaccurate representation of what is a bookmark. It stems from the overly (and poorly) use and concentration on using Selection.

"Now, if you select some text and insert a bookmark it will look like the selected text is enclosed in square brackets ie: [selected text] – this is an “enclosing” bookmark."

My bolding.

If you want to use VBA it makes things MUCH better to not use Selection.

From VBA's perspective, there are NO different - or "type" - of bookmarks. There is ONE type. It is a Range object with a name. Sure the values of Start and End may change, but that is the whole point.

davidboutche
07-14-2009, 03:36 PM
Well thanks for that. I'm actually very new to vb so this sort of advice is really helpful.

I'll have to run my eyes over your explanation a couple more times before it sinks in.

Here's the url to the site that quoted that:
http://word.mvps.org/faqs/MacrosVBA/WorkWithBookmarks.htm

ThANKS again

David:thumb

fumei
07-15-2009, 11:42 AM
I am wont to disagree with MVP, but IMO this is an issue of concept v.s. definition.

A bookmark used for the purpose of pointing to a single character location - THERE! - is a concept of "place".

A bookmark used for the purpose of pointing to a chunk of text, is a concept of "enclosing".

Just looking at the GUI - what you see on screen - yes, I can see that one may consider the two as differerent. However, from the real perspective of VBA (the Object Model) they are not different in any way. They both use ONE object, the same object....a bookmark.

fumei
07-15-2009, 11:45 AM
BTW: if you are going to work with VBA and bookmarks, make sure you also check out the "Insert text at bookmark without deleting the bookmark" link at the MVP site.