Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word VBA: Copy range to variable for later use?

Status
Not open for further replies.

rocket5556

IS-IT--Management
Aug 9, 2007
5
US
Hi,

I am trying to find a way to use VBA to copy some information (including text, tables, bookmarks, etc.) out of a Word document and then use it several times in the same document without relying on it remaining in the clipboard the whole time.

Currently what I am doing is cutting the range out of the document, pasting in back in, changing all the bookmark names, and then pasting the range in again (this preserves the original bookmark names for later copying and pasting). As you might imagine, this is not a very quick process when it has to be done over and over again and there are quite a few bookmarks.

Is there some type of a variable that I could use to store this information and then insert it when I need it? I have tried a Range variable, but if I use a range variable for the original and then delete the original, the range variable essentially holds nothing. I need a way to keep all that information in storage.

An additional note. I found that I could paste the contents into an access object frame and this could be later used to retrieve the information, but this isn't a very elegant solution, and I don't want to have to rely on this. Isn't there a comparable data type that could store this stuff?

Thoughts?

Thanks!
 
Just a thought: why not using a temporary word document ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hey,

Thanks for the idea. I had thought of that as well and may try it out. I just thought it seemed silly to have a create a whole document just to store some temporary stuff. I was hoping their would be another way to do it, but maybe there isn't...

 
>but if I use a range variable for the original and then delete the original, the range variable essentially holds nothing

Unless you use the original's Duplicate method ...
 
I had thought of the duplicate thing, but that doesn't seem to work. Here is some sample code:

Function test()


'Variable declaration
Dim m_objWord As word.Application
Dim m_objDoc As word.Document
Dim m_objDoc2 As word.Document
Dim myRange As Range

'Setup 2 documents, a template and a second document to try to paste a range into
Set m_objWord = New word.Application
Set m_objDoc = m_objWord.Documents.Add("C:\access\template.dot")
Set m_objDoc2 = m_objWord.Documents.Add

'Set the range as the duplicae of the desired range
Set myRange = m_objDoc.Bookmarks("EngagementPart").Range.Duplicate

'Copy something else into the clipboard to be sure it is not the range anymore
m_objDoc.Bookmarks("Client").Select
m_objWord.Selection.Copy

'Delete the information in the original range
m_objDoc.Bookmarks("EngagementPart").Select
m_objWord.Selection.Delete

'Copy the information from the "duplicated" range into the second document
myRange.Copy
m_objDoc2.Activate
m_objWord.Selection.Paste

'Make the documents visible to see results
m_objWord.Visible = True

End Function
 
Ok, well I didn't mean to post that yet, but the code above works if I comment out the two lines below "'Delete the information in the original range".

If those lines run I get an error message on the "myRange.copy" line:
"This method or property is not available because no text is selected.", which makes me think the range is still pointing to the spot in the deleted spot in the document.
 
Sorry, but I am a little confused.

For one thing, you can not change a bookmark name.

Also, you have the code:
Code:
    'Delete the information in the original range
    m_objDoc.Bookmarks("EngagementPart").Select
    m_objWord.Selection.Delete
I am unsure of your intention here.


If you want to remove the bookmarks AND the contents, then you can use:
Code:
    'Delete the information in the original range
    m_objDoc.Bookmarks("EngagementPart").Range.Delete
There is no need to select anything. This may help to speed things up.

As I said, I am unsure of exactly what you are trying to do, but if what you are attempting is to copy the contents of a bookmark, here is a possible suggestion.

Make a Range object.

Make its value the range of the bookmark.

Delete the bookmark - but ONLY the bookmark, NOT the contents. The contents are still selected.

Make an AutoText of the Selection.

REMAKE the bookmark - still using the same Selection.

Result? The original bookmark remains (recreated), with the contents made into an AutoText. Now you can use the AutoText as much as you like, where ever you like.

Note you can NOT do this by making an AutoText of the bookmark Range. The bookmark will be duplicated, and (I think) this is not what you want to do. Sample code:
Code:
Dim r As Range
[COLOR=red] ' select bookmark range[/color red]
Set r = ActiveDocument.Bookmarks("test").Range
r.Select
[COLOR=red] ' delete [b]bookmark[/b] ONLY[/color red]
ActiveDocument.Bookmarks("test").Delete
[COLOR=red] ' make AutoText of selection[/color red]
    NormalTemplate.AutoTextEntries.Add Name:="tg", Range:=r
[COLOR=red] ' recreate bookmark using same name
' note: you could get original bookmark name and 
' use it as a variable[/color red]
    ActiveDocument.Bookmarks.Add Name:="test", Range:=r
[COLOR=red] ' test by going to end of doc and
' inserting new AutoText[/color red]   
    Selection.EndKey unit:=wdStory

    NormalTemplate.AutoTextEntries("tg").Insert _
        Where:=Selection.Range, _
        RichText:=True

faq219-2884

Gerry
My paintings and sculpture
 
The above would be easy to adjust to delete the original bookmark contents, if that is what you want to do.
'Delete the information in the original range
m_objDoc.Bookmarks("EngagementPart").Select
m_objWord.Selection.Delete

faq219-2884

Gerry
My paintings and sculpture
 
Thanks for all comments.

Fumei- The point of the code I posted was just to show that when the data inside the bookmark is deleted the duplicate range also ends up containing no data. The example didn't really have anything to do with deleting the bookmark or not.

I know you can't technically change the name of a bookmark, but you can make a bookmark with an identical range and delete the original, which is what I actually meant. I just consider it a way of renaming the bookmarks since that is the end effect.

I am definitely going to try the autotext idea. I think that may work pretty well.

Just to clarify, the point is this: I have a template that I am filling in from a database. In this template there is a section that is reused several times. Inside that section there is a subsection that is also reused several times.

So the code will copy the large section and paste it in, and enter some data into it. Then it will copy the subsection, and paste and fill it in one or more times.

Now if I need another copy of the big section I need to have an unaltered copy of it somewhere. A range variable won't work since it would have been altered or deleted. Storing it in a separate document would work, but I would prefer not to have an extra scratch document open. What I was doing before works, which is keeping an unaltered version in the document, cutting it out, pasting it in, replacing all the internal bookmarks with differently named bookmarks so they wouldn't conflict with the original, and then pasting the original back in.

I think the autotext will work, so I will definitely try that.

Thanks again.
 
The point of the code I posted was just to show that when the data inside the bookmark is deleted the duplicate range also ends up containing no data. The example didn't really have anything to do with deleting the bookmark or not.

I know you can't technically change the name of a bookmark, but you can make a bookmark with an identical range and delete the original, which is what I actually meant. I just consider it a way of renaming the bookmarks since that is the end effect.[/quote[Just to clarify.


1. The duplicate range is a pointer. If the original range is changed (resized, or contents deleted...it does not matter), then the duplicate is changed as well.

2. You can not change a bookmark name, period. If you .Add a bookmark with the same name, whatever range is used becomes the bookmark. It does not have to be the identical range, it can be any range. So "make a bookmark with an identical range and delete the original" is not correct. There is NO deletion action. The defined range of is assigned. VBA does not care one bit what the range is.

If you have paragraph 1 assigned as a bookmark ("One"), then select paragraphs 2 to 167 and .Add bookmark named "One", then THAT is the bookmark. The bookmark is not deleted. The range parameters are reassigned.
In this template there is a section that is reused several times. Inside that section there is a subsection that is also reused several times.
I would be interested in hearing how you go about this, and what is the process/logic for determining "several".


Does the user do this? Is this a real template (.dot)? What is the structure? When you say "section", does you mean a real Section? Depending on these, you may want to consider an reductive process, rather than an additive one. With working with this kind of stuff, I have found it easier to remove chunks NOT needed (but present in the .dot file). If these are bookmarks, then it is one instruction to remove them.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top