How do I find a sentence in the body of the document?

H

Hollis D. Paul

I have this function that will open a word document, apply a template,
copy in the HTMLBody property of an Outlook message, and then save it
as an XML file.

I want to select the subject line in the document and construct a file
name out of it. Howver, every time I try to find the word subject with
a range object, it blows my function out of the water, and never saves
the file. Actually, when the MyDoc object loses scope the dialog box
pops up and asks if I want to save the changes.

Here is the function:
Function PrintDocs(o_item,strWordTemplate)

dim strSaveName
dim strIce9
Dim MyDoc
Dim MyRange
'Dim MySel

strSaveName = "TestDoc" & i+1 & ".XML"
'MsgBox strSaveName

'Open a new letter based on the selected template
appWord.Documents.Add strWordTemplate
appWord.Visible = True
Set MyDoc = appWord.ActiveDocument

MyDoc.Content.InsertAfter o_item.HTMLBody

Set MyRange = MyDoc.Content
'MyRange = MyDoc.Content.Collapse(1)
MsgBox "Test textbox Point1"

'MyRange.Find "Subject:"

strIce9 = "strIce9:" & MyRange.Text & "endstrIce9"

'MySel.InsertAfter strIce9


MsgBox "Test textbox Point2"

MyDoc.SaveAs strSaveName, 8

'MyDoc.close

'Write information to Word custom document properties
'Set prps = appWord.ActiveDocument.CustomDocumentProperties


' MsgBox "Do we see a document?"

Set MyRange = Nothing
Set MyDoc = Nothing

fLetterCreated = True

End Function

How do I find my Text Subject: , collect the rest of the line, delete
spaces and chop it off at 20 characters? Or just the first step? I
love a good flail at times, but this is not a good time.

The source text that is put in the document follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7036.0">
<TITLE></TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->

<P><FONT SIZE=2>From: &quot;kanth&quot;
&lt;[email protected]&gt;<BR>
Subject: How to set template to MySite.<BR>
Date: Sun, 14 Nov 2004 19:22:09 -0800<BR>
Newsgroups: microsoft.public.sharepoint.portalserver<BR>
<BR>
<BR>
Hi Everyone,<BR>
<BR>
I would like to set a template for mysite. Here, I want to create a<BR>
template with some organization webparts and by default when the
user<BR>
creates mysite, this template should apply.&nbsp;<BR>
<BR>
Any ideas would be appreciated.<BR>
<BR>
Thanks a lot.<BR>
- Ravi Kanth<BR>
<BR>
*********<BR>
<BR>
From: &quot;James Mueller&quot;
&lt;[email protected]&gt;<BR>
Subject: Re: How to set template to MySite.<BR>
Date: Sun, 14 Nov 2004 10:12:24 -0600<BR>
Newsgroups: microsoft.public.sharepoint.portalserver<BR>
<BR>
<BR>
You can edit the SPSMSITE default.aspx and public.aspx pages to
include<BR>
information, document libraries, and lists.<BR>
<BR>
<BR>
<BR>
</FONT>
</P>

</BODY>
</HTML>




Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access, Windows 2000 build 2600
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 
H

Helmut Weber

Hi Hollis,
I guess, the crucial point is, to avoid the use of
activedocument more than once. I'd create objects
instead, the reference of which is always clear.
like:
Dim DocOld As Document
Dim DocNew As Document
Set DocOld = ActiveDocument
Set DocNew = Documents.Add
Then create a new range and set it to newdoc.range.
Set r = DocNew.Range
---
The parsing, later on, is easy.
s = Right(s, Len(s) - Len("Subject: ")) ' cut off "subject "
s = Left(s, Len(s) - 5) ' cut off <BR> & paragraph mark
s = replace(s, " ", "")
s = Left(s, 20)
MsgBox "[" & s & "]"
---
Theoretically, one could search for "subject" until not "<".
But I couldn't get it to work. Happens quite often with wildcards.
---
I copied your text into a document, created an additional new one,
set it's range.text to the range.text of the original...
Just have a look.
And not a beginner's question, I'd say.
---
Sub Test446()
Dim s As String
Dim r As Range
Dim DocOld As Document
Dim DocNew As Document
Set DocOld = ActiveDocument
Set DocNew = Documents.Add
DocNew.Range.Text = DocOld.Range.Text
ResetSearch
Set r = DocNew.Range
With r.Find
.Text = "Subject: *^13"
.MatchWildcards = True
If .Execute Then
s = r.Text
End If
End With
ResetSearch
s = Right(s, Len(s) - Len("Subject: ")) ' cut off "subject "
s = Left(s, Len(s) - 5) ' cut off <BR> & paragraph mark
s = replace(s, " ", "")
s = Left(s, 20)
MsgBox "[" & s & "]"
DocNew.Close SaveChanges:=wdDoNotSaveChanges
End Sub
'---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/





I have this function that will open a word document, apply a template,
copy in the HTMLBody property of an Outlook message, and then save it
as an XML file.

I want to select the subject line in the document and construct a file
name out of it. Howver, every time I try to find the word subject with
a range object, it blows my function out of the water, and never saves
the file. Actually, when the MyDoc object loses scope the dialog box
pops up and asks if I want to save the changes.

Here is the function:
Function PrintDocs(o_item,strWordTemplate)

dim strSaveName
dim strIce9
Dim MyDoc
Dim MyRange
'Dim MySel

strSaveName = "TestDoc" & i+1 & ".XML"
'MsgBox strSaveName

'Open a new letter based on the selected template
appWord.Documents.Add strWordTemplate
appWord.Visible = True
Set MyDoc = appWord.ActiveDocument

MyDoc.Content.InsertAfter o_item.HTMLBody

Set MyRange = MyDoc.Content
'MyRange = MyDoc.Content.Collapse(1)
MsgBox "Test textbox Point1"

'MyRange.Find "Subject:"

strIce9 = "strIce9:" & MyRange.Text & "endstrIce9"

'MySel.InsertAfter strIce9


MsgBox "Test textbox Point2"

MyDoc.SaveAs strSaveName, 8

'MyDoc.close

'Write information to Word custom document properties
'Set prps = appWord.ActiveDocument.CustomDocumentProperties


' MsgBox "Do we see a document?"

Set MyRange = Nothing
Set MyDoc = Nothing

fLetterCreated = True

End Function

How do I find my Text Subject: , collect the rest of the line, delete
spaces and chop it off at 20 characters? Or just the first step? I
love a good flail at times, but this is not a good time.

The source text that is put in the document follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7036.0">
<TITLE></TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->

<P><FONT SIZE=2>From: &quot;kanth&quot;
&lt;[email protected]&gt;<BR>
Subject: How to set template to MySite.<BR>
Date: Sun, 14 Nov 2004 19:22:09 -0800<BR>
Newsgroups: microsoft.public.sharepoint.portalserver<BR>
<BR>
<BR>
Hi Everyone,<BR>
<BR>
I would like to set a template for mysite. Here, I want to create a<BR>
template with some organization webparts and by default when the
user<BR>
creates mysite, this template should apply.&nbsp;<BR>
<BR>
Any ideas would be appreciated.<BR>
<BR>
Thanks a lot.<BR>
- Ravi Kanth<BR>
<BR>
*********<BR>
<BR>
From: &quot;James Mueller&quot;
&lt;[email protected]&gt;<BR>
Subject: Re: How to set template to MySite.<BR>
Date: Sun, 14 Nov 2004 10:12:24 -0600<BR>
Newsgroups: microsoft.public.sharepoint.portalserver<BR>
<BR>
<BR>
You can edit the SPSMSITE default.aspx and public.aspx pages to
include<BR>
information, document libraries, and lists.<BR>
<BR>
<BR>
<BR>
</FONT>
</P>

</BODY>
</HTML>




Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access, Windows 2000 build 2600
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 
H

Helmut Weber

Theoretically, one could search for "subject" until not "<".
For "subject " until excluding "<" that is, of course.
Didn't work either.
Maybe Klaus Linke will know.
 
H

Hollis D. Paul

Helmut Weber said:
And not a beginner's question, I'd say.
Thanks, Helmut, for your suggestions. I guess that I had better
transfer this development project to VBA now, so I can take advantage
of Intellisense. I would eventually have had to do that anyway, but it
is a quicker start as VBScript.

Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access, Windows 2000 build 2600
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 
H

Hollis D. Paul

Helmut Weber said:
For "subject " until excluding "<" that is, of course.
Didn't work either.
Maybe Klaus Linke will know.
Talk about doing things the hard way, we are certainly guilty of doing
just so.

The code that I started from is from a code set by Helen Feddema, a
prolific writer whose many samples are available on-line. I had
downloaded a VBA sample of the same task, so I looked to see what Helen
had done. Simple and Elegant as could be. She got the Subject
property from the Outlook message! It's a named, standard Outlook
Property. No search required. Talk about no thinking out of the box.
The joke is certainly on me, and I am sorry I took up your time, even
if it was an interesting exercise.

Thanks, again.

Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access, Windows 2000 build 2600
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 
H

Hollis D. Paul

Helmut Weber said:
The parsing, later on, is easy.
s = Right(s, Len(s) - Len("Subject: ")) ' cut off "subject "
s = Left(s, Len(s) - 5) ' cut off <BR> & paragraph mark
s = replace(s, " ", "")
s = Left(s, 20)
MsgBox "[" & s & "]"
OK, I have been able to do this. I now have a string that I want to
put into the Title and Topic built-in properties. When I look up the
BuiltInProperties collection, it says they are read only. How do I set
these values into the BuiltIn Document Properties?

Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access, Windows 2000 build 2600
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 
H

Helmut Weber

Hi Hollis,
not all builtinproperties are readonly, therefore
With ActiveDocument.BuiltInDocumentProperties
.Item("Title").Value = "Test"
MsgBox .Item("Title").Value
.Item("Subject").Value = "Test, too"
MsgBox .Item("Subject").Value
End With
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
H

Hollis D. Paul

Helmut Weber said:
Hi Hollis,
not all builtinproperties are readonly, therefore
With ActiveDocument.BuiltInDocumentProperties
.Item("Title").Value = "Test"
MsgBox .Item("Title").Value
.Item("Subject").Value = "Test, too"
MsgBox .Item("Subject").Value
End With
Sad to say, this code is blowing me out of the water in VBScript. I
will try in VBA tomorrow.

Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access, Windows 2000 build 2600
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 
H

Hollis D. Paul

Helmut Weber said:
With ActiveDocument.BuiltInDocumentProperties
.Item("Title").Value = "Test"
MsgBox .Item("Title").Value
.Item("Subject").Value = "Test, too"
MsgBox .Item("Subject").Value
End With
What we overlooked is that an object is returned by

ActiveDocument.BuiltInDocumentProperties.Item("Title")

and we have to dimension and set the object that it is coming into.

Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access, Windows 2000 build 2600
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 
H

Helmut Weber

Hi Hollis,
hm..., was that a question?
The example was meant to show setting and reading
of (some) BuiltInDocumentProperties. It runs within word vba
without modifications and doesn't need a dim statement at all.
And I didn't use
ActiveDocument.BuiltInDocumentProperties.Item("Title")
but
ActiveDocument.BuiltInDocumentProperties.Item("Title").value
By the way,
..item() is required here, only in order to shorten code lines,
the way I prefer it.
ActiveDocument.BuiltInDocumentProperties("Title").value = "Test"
would do the job as well.
 
K

Klaus Linke

was a simple statement of the solution that I found, so
that other might find it when searching the newsgroup
later.
Not that many will ever do that [...]


Ha! You don't know how us MVPs find all those answers, then?

;-) Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top