Why I can not copy from a invisible document?

J

Jing You

hi, everyone

I am trying copy something from a hidden document to an opened document.
But I got nothing.

My code opening a hidden document like this:

'------------------------------------------------

Documents.Open FileName:=FileName, ConfirmConversions:=False, ReadOnly:=
_
False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto, XMLTransform:="", Visible:=False


afther the code, I do the copy operation like this:

Selection.WholeStory
Selection.Copy

But nothing was copied to clipboard. How can I get the correct way?

Regards,
Jing You
 
J

Jezebel

The problem is the use of the Selection object: you don't actually select
anything, which is just as well since your document is invisible. In any
case, you're better off avoiding the Selection object in VBA, and you rarely
need it except for interacting with the user. Try it like this:

Documents.Open (FileName:=FileName, ...).Content.Copy
 
J

Jing You

:) After some tests for the problem, I got the answer.

If a document is an invisible document, it should be initialized as an
inactive document. So we need call "document.active" method to active the
docuemnt, then all operations to this document are effective.
 
J

Jezebel

That will work, but it's not a good approach.



Jing You said:
:) After some tests for the problem, I got the answer.

If a document is an invisible document, it should be initialized as an
inactive document. So we need call "document.active" method to active the
docuemnt, then all operations to this document are effective.
 
H

Howard Kaikow

As Jezebel stated, get used to using the Range object instead of the
Selection object.
In general you will end up with faster and cleaner code.
Use Selection object only when there's no other way to do the deed, or in a
quickie piece of code.
 

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