how to move text around in a word doc without copy and paste?

O

Omatase

I have an application I'm writing that needs to move text from one
place in a word doc to another. A simple enough process but it's
proving to be difficult to get just right. Right now I have it
*working* using cut and paste. The only problem with this is that, as
their names imply, these methods actually use the Windows clipboard.
This won't work for me as I don't want people to have to be aware of
the fact that while this utility is running they cannot use the
clipboard for anything or the application will mess up their
documents.

In my application I find and select a range of text. Then on the range
I say Cut(). I know that on the same range object I have a property
named "Text". The problem I have with using this is that the specific
text I am moving has merge fields in it. They look something like this
"{merge field}". When I try to read the text property of a range that
has a merge field in it, instead of getting the above text, I get
boxes where the "{" and "}" characters are. So, using the Text
property to move the text around actually changes the text to
something unusable. Does anyone know a fix for this?

Thanks much
 
S

Shauna Kelly

Hi Omatose

Use .FormattedText with something like:

Dim rngSource as Word.Range
Dim rngTarget as Word.Range

'set the rngSource and rngTarget in some appropriate way

'"Copy" the text from one range to the other
rngTarget.FormattedText = rngSource.FormattedText


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
D

Doug Robbins - Word MVP

You may have to use code to re-create the mergefields.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

Hi Shauna,

In my testing, the use of .FormattedText did not preserve the functionality
of the merge fields.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
O

Omatase

Shauna,

Thanks for the reply, it is still printing the boxes when I try to do
it that way though.

Doug,

Can you show a quick example of how to recreate a merge field?

Thanks
 
D

Doug Robbins - Word MVP

Leave the mergefields without the field codes being displayed, so that they
appear like:

«fieldname»

and then run the following macro

Dim mfield As Range
Dim mfrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="«[A-z]{1,}»", Forward:=True,
Wrap:=wdFindStop) = True
Set mfrange = Selection.Range
If mfrange.Fields.Count = 0 Then
Set mfield = mfrange.Duplicate
mfield.start = mfield.start + 1
mfield.End = mfield.End - 1
ActiveDocument.Fields.Add Range:=mfrange, Type:=wdFieldEmpty,
Text:="MERGEFIELD " & Chr(34) & mfield.Text & Chr(34),
PreserveFormatting:=False
End If
Selection.Collapse wdCollapseEnd
Loop
End With

The above assumes that the merge field names are simply Alphabetical. If
they are not, you will need to amend the string that is been searched for
"«[A-z]{1,}»", so that it does find the fields.

For more on that, see the article "Finding and replacing characters using
wildcards" at:

http://www.word.mvps.org/FAQs/General/UsingWildcards.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
O

Omatase

This is a really big help thank you thus far!

Can you help me understand how that code would look different if I
already had a Range object that represents the area I want to search
inside for those merge fields?

Here is what I tried, but the search is failing (mergeFields is a
Range object that is already set to the correct range for my merge
fields) (this is of course not VBA, but responding in VBA is perfectly
fine with me)

object findText = @"«[A-z]{1,}»";

while (mergeFields.Find.Execute(ref findText, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue,
ref trueValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue, ref
missingValue, ref missingValue) == true)
{
}

The search returns false on it's first execution.

Here are what the merge fields look like that I am trying to move:

«LetterDate»

«AddressName»
«Position»
«Company»
«Address1»
«Address2»
«Address3»
«City», «State» «Zip»

I don't know if those throw off the search string or not. I'm not very
familiar with the inner workings of Word searches.
 
O

Omatase

Nevermind my last post please. I was able to get the search working
after trial and error between 3 or 4 parameters in the Execute method.
I am still having problems though getting it just right. Here's my
code now:

object findText = @"«[A-z]{1,}»";

Range newRange = null;

while (mergeFields.Find.Execute(ref
findText, ref missingValue, ref missingValue, ref trueValue, ref
missingValue, ref missingValue, ref trueValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue) == true)
{

if (mergeFields.Fields.Count == 0)
{
newRange = mergeFields.Duplicate;
newRange.Start = cell.Range.End;
newRange.End = cell.Range.End;

object fieldText = "MERGEFIELD " +
(char)34 + newRange.Text + (char)34;
object type =
WdFieldType.wdFieldEmpty;

aDoc.Fields.Add(mergeFields, ref
type, ref fieldText, ref falseValue);
}
}

When I execute the aDoc.Fields.Add method I get an error that shows up
in the page where my merge field was "No Bookmark Name Found!". In
looking at your code I can't tell exactly how the Word object model is
supposed to be treating all of your commands. Can you tell me if my
attempt and copying your code has some improper logic in it?
 
O

Omatase

I should probably clarify too that the "cell" object is a reference to
a cell in the table. I am trying to add these merge fields to that
table cell one at a time. They need to end up in the same order too

continued thanks
 
O

Omatase

Thanks for all of your help. I think I've got it figured.

Here is the code I ended up with that works (mergeFields is a Range
object set to the boundaries of my mergefields):

foreach (Field myMergeField in mergeFields.Fields)
{
if (myMergeField.Type ==
WdFieldType.wdFieldMergeField)
{
Range cellRange =
aDoc.Tables.Item(1).Cell(1, 1).Range;

Range rngFieldCode =
myMergeField.Code;
object fieldText =
rngFieldCode.Text;
object type =
WdFieldType.wdFieldEmpty;

object collapseDirection =
WdCollapseDirection.wdCollapseStart;
cellRange.Collapse(ref
collapseDirection);

cellRange.Fields.Add(cellRange,
ref type, ref fieldText, ref falseValue);
}
}
 
S

Shauna Kelly

Hi Doug

How odd. I tried the following: I created a new document based on
normal.dot, pressed Enter a few times to create some paragraphs, and put a
field in the first paragraph that looked like { MERGEFIELD "Last" }.

I could then do:
With ActiveDocument.Paragraphs
.Last.Range.FormattedText = .First.Range.FormattedText
end with

And it worked fine. I've used .FormattedText to "copy" ranges containing all
kinds of other fields in the past (SEQ fields in particular, and I tested it
out a bit today and couldn't find a kind of field it didn't work with).

But if I have set a document as the Main Document for a merge, then
..FormattedText no longer works to "copy" text containing a MERGEFIELD. But
it still works for other fields, including an ADDRESSBLOCK field.

Odd!

Shauna

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
D

Doug Robbins - Word MVP

Odder and Odder<g>

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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