Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

Cut & Paste help!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ivan Debono - 27 Dec 2004 15:31 GMT
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
Helmut Weber - 27 Dec 2004 20:10 GMT
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/
Ivan Debono - 27 Dec 2004 20:24 GMT
> 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?

That's the only way I know of at the moment.

>> in a bookmark in another part of the document.
>
> 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
Helmut Weber - 27 Dec 2004 20:36 GMT
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/
Ivan Debono - 28 Dec 2004 08:02 GMT
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

> Hi Ivan,
>
[quoted text clipped - 11 lines]
> Word XP, Win 98
> http://word.mvps.org/
Helmut Weber - 28 Dec 2004 09:40 GMT
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/
Ivan Debono - 29 Dec 2004 05:30 GMT
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

> Hi Ivan,
> hmm... looks all very strange to me.
[quoted text clipped - 22 lines]
> Word XP, Win 98
> http://word.mvps.org/
Ivan Debono - 29 Dec 2004 06:25 GMT
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

> Hi Ivan,
> hmm... looks all very strange to me.
[quoted text clipped - 22 lines]
> Word XP, Win 98
> http://word.mvps.org/
Helmut Weber - 29 Dec 2004 10:45 GMT
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/
Ivan Debono - 30 Dec 2004 07:21 GMT
Solved the problem!

Thanks,
Ivan

> Hi Ivan,
> I still don't understand all, but this might get us
[quoted text clipped - 32 lines]
> Word XP, Win 98
> http://word.mvps.org/

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.