selecting from clipboard

L

Larry

I would like to grab, from the Word clipboard, only InlineShapes,
dropping all other content. Any tips appreciated.
 
J

Jean-Guy Marcil

Larry said:
I would like to grab, from the Word clipboard, only InlineShapes,
dropping all other content. Any tips appreciated.

You could paste the clipboad content in a dummy document, check if the
InlineShapes collection is bigger than 0, if so, grab each item of the
collection.

What are you doing, over all?
Which Word version?
 
L

Larry

It's Word 2003 on WinXP.

Here's what I'm really trying to do:

I have users who need to copy content that includes MathType equations
from a source document into a target document. The target document is
format-protected, so any local formatting (italic, colour, etc.) of
the text from the source document will be lost when it gets pasted
into the target document, and this behaviour is exactly what we want.
EXCEPT... the sophisticated vertical alignment that is applied to
MathType equations is also lost, being seen as local formatting. I
need to keep that, even at the expense of losing the text content
outright, which my users have assured me is not terribly important.
But I would still like for them to be able to select a big bunch of
content with many equations, and at least let them paste the equations
from that content in one smooth move.

Currently I do have a macro working that just quietly unprotects the
document, does the paste, then reprotects. This leaves the nice
vertical alignment of the equations... and it leaves the local
formatting of the incoming text as well. I want the one but not the
other. :)

Perhaps I can convince VBA that the clipboard is a range and work with
the pieces of the range that way?

Merci d'avance, Jean-Guy. Pour le moment, je suis en train d'essayer
ta solution d'abord.

---larry
 
L

Larry

Oh! And I just realised that I can provide not just a specialised
Paste function, but also a specialised Copy function, because the
users will have access to the same modules in the source document as
they do in the target document. Does that make anything easier? Can I
more readily control what gets copied into the clipboard than what
gets pasted from it? For example, I could walk through the collection
of InlineShapes in the selection and only put them into the clipboard.
Except I don't know how to add to the clipboard piece-by-piece.

---larry
 
J

Jean-Guy Marcil

Larry said:
It's Word 2003 on WinXP.

Here's what I'm really trying to do:

I have users who need to copy content that includes MathType equations
from a source document into a target document. The target document is
format-protected, so any local formatting (italic, colour, etc.) of
the text from the source document will be lost when it gets pasted
into the target document, and this behaviour is exactly what we want.
EXCEPT... the sophisticated vertical alignment that is applied to
MathType equations is also lost, being seen as local formatting. I
need to keep that, even at the expense of losing the text content
outright, which my users have assured me is not terribly important.
But I would still like for them to be able to select a big bunch of
content with many equations, and at least let them paste the equations
from that content in one smooth move.

Currently I do have a macro working that just quietly unprotects the
document, does the paste, then reprotects. This leaves the nice
vertical alignment of the equations... and it leaves the local
formatting of the incoming text as well. I want the one but not the
other. :)

Perhaps I can convince VBA that the clipboard is a range and work with
the pieces of the range that way?

I am not sure I understand.

With my Word 2003, I created a dummy document with some text. I applied the
Heading 2 style to the whole document, plus added some manual changes.
Then, somewhere in the middle, I inserted a Microsoft Equation 3.0 object,
created some nonsensical equation using lots of differently aligned numbers.

Then,after leaving the inline shape that the equation is, I did CTRL-A and
applied the Normal style, my whole text reverted to Arial, but the euqation
stayed as Times because it was not affected by the formatting because it is
an inline shape.

I am not sure how you create your problem.

If you paste the content and work with it as a range, apply the normal style
and remove all manual formatting by doing a font reset and a pargraph reset.
This should not affect your equations.
Alternatively, without going through the clipboard, you can use something
like:

Dim docTarget As Document
Dim rgeSource As Range

Set rgeSource = Selection.Range
Set docTarget = Documents.Add

With docTarget.Range
.FormattedText = rgeSource.FormattedText
.Style = "Normal"
.Paragraphs.Reset
.Font.Reset
End With

What am I missing?
 
L

Larry

Merci beaucoup, Jean-Guy ---

Your method does work; I was able to get the desired behaviour by
using a temporary document as a buffer to hold a range, clearing out
undesirable formatting there (had to add colour, spacing, etc. etc. --
I wish Word had a "go back to the absolute font basics" function). But
then I encountered other side effects, such as inadvertently altering
the font style of adjacent existing text in the target document
(because some of the equations are inline); these side effects would
have occurred with any other approach as well.

So now I am just restricting the users to pasting one equation at a
time. The macro checks the length of the string to be posted and
rejects anything of length greater than zero. This will sometimes be
tedious, but it will avoid introducing errors in styling, which is
hugely important for us. Also, it means that the user can just use a
regular copy command, and only has to use the special paste command.
But I'll still keep an eye out for a way to allow the user to select a
bunch of equations at once and drop out all the intervening text.

Here is what the code looks like for now:

~~~~~~~~~~~~~

Dim myPass As String

myPass = ****** password algorithm here *******

On Error Resume Next

' Arrange to get the clipboard into a string variable so we can
check its size.
Dim MyData As DataObject
Dim strClip As String
Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText

If Len(strClip) = 0 Then ' Should only be true when the selection
is an inline shape by itself.
' Oddity here. If equations plus text characters are selected
together, Len(strClip) will report the length of the text, plus
' one for each equation selected. But if an equation is
selected all alone, then Len(strClip) reports zero.
ActiveDocument.Unprotect (myPass)
'Application.Run
MacroName:="MathTypeCommands.UIWrappers.EditPaste"
Selection.Paste
ActiveDocument.Protect Type:=wdNoProtection,
EnforceStyleLock:=True, Password:=myPass
Else
MsgBox "Select and copy only a single equation at a time.", ,
"INFO"
End If

~~~~~~~~~~~~~~~~~~~~~~~~~~

Merci encore tout la mème, Jean-Guy

--larry
 
L

Larry

Following up on the idea to capture all the inlineshapes within the
selection....

I can grab them all and put them into an array of inlineshapes. But I
don't see how to output them, one by one, in the target document. I'm
sure it's because there's a method I'm just not seeing... (I'm just
fundamentally uneasy with object oriented programming, I'm afraid). I
tried the .Type method and VBA sure didn't like that.
---larry
 
J

Jean-Guy Marcil

Larry said:
Merci beaucoup, Jean-Guy ---

Your method does work; I was able to get the desired behaviour by
using a temporary document as a buffer to hold a range, clearing out
undesirable formatting there (had to add colour, spacing, etc. etc. --
I wish Word had a "go back to the absolute font basics" function). But

If you use the Normal sytle as I suggested and the two "Reset" methods, it
should take care of all that.
then I encountered other side effects, such as inadvertently altering
the font style of adjacent existing text in the target document

You mean wheh pasting the text from the dummy document back into the target
location?
This should not happen.
Make sure you use the Range object.

Try not to use the clipboard, as I suggested, this way, the user clipboard
content is not altered.
 
L

Larry

If you use the Normal sytle as I suggested and the two "Reset" methods, it
should take care of all that.

It goes too far; the .Font.Reset loses the sophisticated vertical
alignment of the inline equations.
You mean wheh pasting the text from the dummy document back into the target
location?
This should not happen.

It happens when pasting a full paragraph into the middle of another
paragraph. Standard Word behaviour. Theoretically, the users should
expect this, but....
Make sure you use the Range object.

Try not to use the clipboard, as I suggested, this way, the user clipboard
content is not altered.

I do like the idea of avoiding the clipboard. I did use the Range
object to try to go directly from the source document to the target
document; I also tried going via a dummy document, which worked
better. But there are still enough variations in what happens
depending on just what the user does that I've decided this approach
overall is not worth the effort; generally our editors just want to
copy equations one at a time, very carefully. And they are in a hurry
just now -- I have to get a workable tool to them today. So I'll just
limit what they can copy, which sidesteps all the formatting issues,
and they've indicated that they're happy with that.

But Jean-Guy, thank you very much indeed for your help and interest;
I've learned more about Ranges from your examples and suggestions.

Cheers--
larry
 

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