Circle a specific piece of text

C

Cameron Piper

This is my first post to the Microsoft word user's group,
so please pardon my newbie status. I am working on
setting up a microsoft acces project that will export a
group of fields to a templated Microsoft word document.


Exporting the information to specific bookmarks is not a
problem for me but I was curious if Word was able to
conditionaly format information much like an access
report.

What I would really like to accomplish is having a
specific choice on my form circled. Because I am working
on a form that is standardized throughout my company, I
am not able to enter one piece of text or another. I
will have circle one of the two choices.

I was thinking of using a border property around a
specific named piece of text, but I am not sure how to
accomplish naming text.

Questions:

1. Does Word support conditional formating?
2. Is it possible to use an If...Then...ElseIf statement
in Word?
3. What is the best way to circle a piece of text? using
border properties or another method?
4. How can I name a line of text so that I can call it in
VBA and format its border?


I hope that someone can point me in the right direction.
Thanks in advance for your help.

Cameron Piper
(e-mail address removed)(dot com)
 
J

Jonathan West

Hi Cameron


Cameron Piper said:
This is my first post to the Microsoft word user's group,
so please pardon my newbie status. I am working on
setting up a microsoft acces project that will export a
group of fields to a templated Microsoft word document.


Exporting the information to specific bookmarks is not a
problem for me but I was curious if Word was able to
conditionaly format information much like an access
report.

What I would really like to accomplish is having a
specific choice on my form circled. Because I am working
on a form that is standardized throughout my company, I
am not able to enter one piece of text or another. I
will have circle one of the two choices.

I was thinking of using a border property around a
specific named piece of text, but I am not sure how to
accomplish naming text.

Questions:

1. Does Word support conditional formating?

Not directly, though there are a variety of workarounds you can use.
2. Is it possible to use an If...Then...ElseIf statement
in Word?

Word VBA does support that construction
3. What is the best way to circle a piece of text? using
border properties or another method?

Border properties seem a pretty good way to me
4. How can I name a line of text so that I can call it in
VBA and format its border?

Every bit of text on a Word document is a Range object. You can apply border
to any arbitrary range. For instance, if you want to apply a border round
the text marked by a bookmark called myBookmark, you would use the following
code

With ActiveDocument.Bookmarks("myBookmark").Range.Font
With .Borders(1)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
End With
 
C

Cameron

Jonathan,

Thank you so much for your prompt and helpful reply. Now
that I know that it has the capability my next quest is
to accomplish the task with word.

I was hoping that you could walk me through a couple
examples briefly.

1. If I had a row of text like:
This is text in a row and I want to circle some of it
And that text was on the fifth line, how would I name the
portion "in a row" as Text1 so that I could call it in
code.

2. Using a boolean value from Access a "true/false" field
help me with the syntax of the following.

If Boolean1 = True Then

With ActiveDocument.Bookmarks("Text1").Range.Font
With .Borders(1)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
End With
End If

Thanks for your help

Cameron
 
J

Jonathan West

Hi Cameron
Cameron said:
Jonathan,

Thank you so much for your prompt and helpful reply. Now
that I know that it has the capability my next quest is
to accomplish the task with word.

I was hoping that you could walk me through a couple
examples briefly.

1. If I had a row of text like:
This is text in a row and I want to circle some of it
And that text was on the fifth line, how would I name the
portion "in a row" as Text1 so that I could call it in
code.

Think about it this way. There must be some totally unambiguous way for you
to describe which bit of text is to be circled. If that unambiguous
description exists, then you can write VBA code to identify it.

There are various possibilities, including but not limited to the following

1. Starting at character x of the paragraph and ending at character y, where
x & y are known in advance. The example below finds characters 15 to 18 of
the 4th paragraph od the document

Dim myRange as Range
Set myRange = ActiveDocument.Paragraphs(4).Range.Characters(15)
myRange.MoveEnd Unit:=wdCharacter, Count:=3

2. Starting at word x of the paragraph and ending at word y, where x & y are
known in advance. The following code example identifies the 15th to 18th
words of the 4th paragraph

Dim myRange as Range
Set myRange = ActiveDocument.Paragraphs(4).Range.Words(15)
myRange.MoveEnd Unit:=wdWord, Count:=3

3. The content of the text is known. i.e. you know that "this is the answer"
is *somewhere* within the paragraph, but you don't know where. For this, you
use the Find object to locate the relevant text. The following code
identifies that text within the 4th paragraph of the document (if present at
all), and indicates whether the text was found.

Dim myRange as Range
Set myRange = ActiveDocument.Paragraphs(4).Range
With myRange.Find
.Text = "this is the answer"
.Format = False
.Wrap = wdFindStop
If .Execute Then
MsgBox "Text was found"
Else
MsgBox "Test was not found"
End If
End With


2. Using a boolean value from Access a "true/false" field
help me with the syntax of the following.

If Boolean1 = True Then

With ActiveDocument.Bookmarks("Text1").Range.Font
With .Borders(1)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
End With
End If

If the document is predefined, then you can predefine bookmarks where the
border might go, in which case the code above can be used. Of course, you
don't need to use a string literal for the bookmark name, you can store the
bookmark name in a string variable (and previously in a string field in your
database) so that the same code can be used to set multiple bookmarks.

If you are identifying the appropriate range on the fly using one of the
techniques I describe earlier in this post, then you don't need to set a
bookmark, you can use the Range object directly. Given that the examples
above use an object variable called myRange, you could modify the code above
to this

With myRange.Borders(1)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
 
C

Cameron

Jonathan,

I have been running some tests on the functions to see if
I can get a handle on them. I have been able to open the
document and insert the first and last names. However,
when I added the part of circle the text I get a compile
error:

Method or data member not found.

When I click debug it opens the visual basic editor and
highlights ".MoveEnd". Can you help me through this
problem. I have included my code below.

Dim objWord As Object
Dim myRange As Range

Set objWord = CreateObject("Word.Application")
With objWord
.Visible = True
.Documents.Add ("C:\Documents and
Settings\CSPiper\Application
Data\Microsoft\Templates\Purchase Agreement1.dot")

End With

If objWord.ActiveDocument.Bookmarks.Exists
("FirstName") = True Then
objWord.ActiveDocument.Bookmarks
("FirstName").Range.Text = Me!Firstname
End If

If objWord.ActiveDocument.Bookmarks.Exists
("LastName") = True Then
objWord.ActiveDocument.Bookmarks
("LastName").Range.Text = Me!Lastname
End If

Set myRange = ActiveDocument.Paragraphs
(1).Range.Characters(15)
myRange.MoveEnd Unit:=wdCharacter, Count:=3

If Me.HeadOfHouse = True Then
With myRange.Borders(1)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
End If

Thank you so very much for all of your help

Cameron
 
C

Cameron

Jonathan or anyone,

As a followup to my most recent post. I solved the
compile error by adding a reference to the word object
library. However, I am now getting to the bottom of the
code. The document is open the fields are filled in and
I am getting error 4248 telling me "this command is not
available becuase there is no document open." Thanks
again for all of your help.

Cameron
 
J

Jonathan West

Cameron said:
Jonathan,

I have been running some tests on the functions to see if
I can get a handle on them. I have been able to open the
document and insert the first and last names. However,
when I added the part of circle the text I get a compile
error:

Method or data member not found.

Ah, if you are controlling this from outside Word and using late binding,
then your code will not know the value of wdCharacter or any of the other
constants. Replace them with the following values

wdCharacter 1
wdLineStyleSingle 1
wdLineWidth050pt 4
wdColorAutomatic -16777216
 
C

Cameron Piper

Jonathan,

Thank you again!! I was able to fix this problem with a
reference to the MS Word Boject Library, but I have run
into some other problems.

I am now getting to the bottom of the code. The document
is open the fields are filled in, and I am getting error
4248 telling me "this command is not available becuase
there is no document open." Hope that all of this makes
sense. Thanks again for all of your help.

Cameron
 
J

Jonathan West

Cameron Piper said:
Jonathan,

Thank you again!! I was able to fix this problem with a
reference to the MS Word Boject Library, but I have run
into some other problems.

I am now getting to the bottom of the code. The document
is open the fields are filled in, and I am getting error
4248 telling me "this command is not available becuase
there is no document open." Hope that all of this makes
sense. Thanks again for all of your help.

Which specific line of code is it failing on?
 
C

Cameron

I am not entirely certain, but I do know that it
completes everything except for the border around the
text.

Cameron
 
J

Jonathan West

Cameron said:
I am not entirely certain, but I do know that it
completes everything except for the border around the
text.

It must be possible to find out. If you run the code from within the VB6
IDE, which line is highlighted in the code window when the error is
displayed?
 

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