Cut & Paste help!

I

Ivan Debono

Hi everybody,

Through code I insert text at the end of a document. I'd like to cut that
inserted text in a bookmark in another part of the document.

What are the right properties/functions (and in which order) to use to
achieve this using the word object model?

Thanks,
Ivan
 
H

Helmut Weber

Hi Ivan,
Through code I insert text at the end of a document.
I'd like to cut that inserted text

You insert text and would like to cut it out afterwards?
in a bookmark in another part of the document.

activedocument.bookmarks("Mark1").range text = "..."

Note, that the bookmark will have vanished after inserting.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
I

Ivan Debono

Helmut Weber said:
Hi Ivan,


You insert text and would like to cut it out afterwards?

That's the only way I know of at the moment.
activedocument.bookmarks("Mark1").range text = "..."

Note, that the bookmark will have vanished after inserting.

But how do I select the start and end of a range or selection to delete?

Greetz from the Black Forest, Germany!
Ivan
 
H

Helmut Weber

Hi Ivan,

1st, show us your code.
2nd, the text, you insert must be defined somehow.
Either it is in a variable, or in a range, or in the selection,
which is a range, too, or ....
Then, this text would not be inserted at all,
but go straight to the bookmark.
hm...

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
I

Ivan Debono

Here's what I'm doing:

At the beginning I do:

lStart = oDocument.Characters.Last.End

I have a loop to insert autotext entries:

Do While Not tVIEW.EOF
With oDocument.Characters.Last
.Collapse wdCollapseEnd
.Text = oAuto.Name
.InsertAutoText

'Set bookmark values
For Each oField In tVIEW.Fields
.Bookmarks(oField.Name).Range.Text =
oField.Value
Next oField
End With

tVIEW.MoveNext
Loop

Then at the end I do:

With oDocument.Characters.Last
.SetRange lStart, .End
.Cut
.Bookmarks(oAuto.Name).Range.Paste
End With

Which should cut what I inserted at the end and pastes it in another place
of the document.

Ivan
 
H

Helmut Weber

Hi Ivan,
hmm... looks all very strange to me.

This one inserts all autotextentries at the end of the document
and moves them to the beginning of the document, where they
could have been inserted in the first place anyway.

Dim oAut As AutoTextEntry
Dim rDcm As Range ' document range
Dim rTmp As Range ' temporary range
Set rDcm = ActiveDocument.Range
Set rTmp = Selection.Range
rTmp.Start = rDcm.End
For Each oAut In NormalTemplate.AutoTextEntries
rDcm.InsertAfter oAut
Next
rTmp.End = rDcm.End
' MsgBox rTmp.Text
rDcm.InsertBefore rTmp.Text
rTmp.Delete

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
I

Ivan Debono

Your code adds all autotext entries, inserts a copy at the beginning and
deletes everything.

It's nearly what I need. Is there a way to add autotext entries to the
clipboard or to some other temporary place before placing them in the
document itself?

Ivan
 
I

Ivan Debono

The following code inserts an autotext (1 line) with 3 bookmarks, like this:
ItemQuantity ItemName ItemDescription

With oDocument.Characters.Last
.Collapse wdCollapseEnd
.Text = oAuto.Name
.InsertAutoText

'Set bookmark values
For Each oField In tVIEW.Fields
If .Bookmarks.Exists(oField.Name) Then
.Bookmarks(oField.Name).Range.Text =
oField.Value
End If
Next oField
End With

But what happens is after each record is updated, the whole autotext is
deleted. Has it something to do with collapsing?

Ivan
 
H

Helmut Weber

Hi Ivan,
I still don't understand all, but this might get us
a bit closer towards a solution:

Sub test012()
' Selection.Collapse ' just in case for testing
Dim rTmp As Range ' a temporary range
Dim rDcm As Range ' the documents range
Set rTmp = Selection.Range ' initialize rtmp
Set rDcm = ActiveDocument.Range
rTmp.Start = rDcm.End
With rTmp
.InsertAfter vbCr & "autotest" ' oAuto.Name
.Start = .Start + 1 ' rtmp.start = autotext start
.End = rDcm.End ' rtmp.end = doc.end
' .Select ' for testing only
.InsertAutoText
rTmp.End = rDcm.End
' rTmp.Select ' for testing only
' rtmp is now the inserted autotext plus the end of doc
' autotest text does not contain a paragraph mark
MsgBox rTmp.Bookmarks.Count ' test
MsgBox rTmp.Bookmarks(1).Range.Text ' test
' rDcm.FormFields(1).Result = rTmp.Bookmarks(1).Range.Text 'Test
' rtmp contains the 3 bookmarks in question
' rTmp.Delete or move or whatever
End With
End Sub

HTH

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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