A Microsoft Word forum. Microsoft Office Word Forum - WordBanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Microsoft Office Word Forum - WordBanter forum » Microsoft Word Newsgroups » Microsoft Word Help
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

How do I Select an Object with VBA?



 
 
Thread Tools Display Modes
  #1  
Old February 28th 09, 06:41 PM posted to microsoft.public.word.docmanagement
DerbyDad03
external usenet poster
 
Posts: 48
Default How do I Select an Object with VBA?

I am trying to learn how to Select an object in a Word doc via VBA.

Let me give you an example.

First - I know that I can insert this particular object as an Icon
linked to the file, but if someone can tell me why this conversion
code doesn't work, I'm hoping I'll learn something about selecting
objects via VBA. In other words, this question isn't about the best
way to convert an object, it's about how to select an object.

OK, so I have a new document open. I start the recorder and then
perform these steps:

1 - Go to my desktop
2 - Right click a PDF document and select Copy
3 - Go back into the document and use Edit...Paste
4 - Right-click the object and use the PDF Convert option to create a
PDF Icon
5 - Stop the recorder

This produces the following code:

Sub ConvertToIcon()
Application.WindowState = wdWindowStateNormal
Selection.Paste
ActiveDocument.Shapes("Object 2").Select
If Selection.Type wdSelectionShape Then
Selection.InlineShapes(1).ConvertToShape.Select
End If
Selection.ShapeRange(1).OLEFormat.ConvertTo ClassType:= _
"AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-
A90000000001}\PDFFile_8.ico" _
, IconIndex:=0, IconLabel:="Acrobat Document"
End Sub

Now here's the problem: If I copy another PDF and then run this code,
I get an error related to the object not being found, or it tries to
convert the same object over again, etc. Depending on what I've done
before I run the code, the error will change.

It's obvious that it is not selecting the most recently pasted object
because "Object 2" is hard coded. How do I get the code to select the
last object that was pasted without actually knowing what number that
object is?

Thanks.

Ads
  #2  
Old March 1st 09, 03:26 AM posted to microsoft.public.word.docmanagement
Greg
external usenet poster
 
Posts: 113
Default How do I Select an Object with VBA?

DD,

In this case you would probably just select the shape in the document and
then run:

Sub Test()
Dim oILShape As InlineShape
Set oILShape = Selection.InlineShapes(1)
oILShape.OLEFormat.ConvertTo ClassType:= _
"AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _

"C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-A90000000001}\PDFFile.ico",
IconIndex:=0, IconLabel:="Acrobat Document"
End Sub

The recorded often adds a lot of superfilous fluff. E.g., there is no need
to covert an InLineShape to a Shape before converting again.

Easier yet would probably be to simply use EditPaste Special and paste as
an icon.




"DerbyDad03" wrote:

I am trying to learn how to Select an object in a Word doc via VBA.

Let me give you an example.

First - I know that I can insert this particular object as an Icon
linked to the file, but if someone can tell me why this conversion
code doesn't work, I'm hoping I'll learn something about selecting
objects via VBA. In other words, this question isn't about the best
way to convert an object, it's about how to select an object.

OK, so I have a new document open. I start the recorder and then
perform these steps:

1 - Go to my desktop
2 - Right click a PDF document and select Copy
3 - Go back into the document and use Edit...Paste
4 - Right-click the object and use the PDF Convert option to create a
PDF Icon
5 - Stop the recorder

This produces the following code:

Sub ConvertToIcon()
Application.WindowState = wdWindowStateNormal
Selection.Paste
ActiveDocument.Shapes("Object 2").Select
If Selection.Type wdSelectionShape Then
Selection.InlineShapes(1).ConvertToShape.Select
End If
Selection.ShapeRange(1).OLEFormat.ConvertTo ClassType:= _
"AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-
A90000000001}\PDFFile_8.ico" _
, IconIndex:=0, IconLabel:="Acrobat Document"
End Sub

Now here's the problem: If I copy another PDF and then run this code,
I get an error related to the object not being found, or it tries to
convert the same object over again, etc. Depending on what I've done
before I run the code, the error will change.

It's obvious that it is not selecting the most recently pasted object
because "Object 2" is hard coded. How do I get the code to select the
last object that was pasted without actually knowing what number that
object is?

Thanks.


  #3  
Old March 1st 09, 08:00 AM posted to microsoft.public.word.docmanagement
Graham Mayor
external usenet poster
 
Posts: 19,312
Default How do I Select an Object with VBA?

Define a range eg as your code

Dim orng As Range
Set orng = Selection.Range 'define a range at the insertion point
orng.Paste 'paste into that range
orng.Select 'select the range
'now you can operate on the selection
If Selection.Type wdSelectionShape Then
Selection.InlineShapes(1).ConvertToShape.Select
End If
Selection.ShapeRange(1).OLEFormat.ConvertTo ClassType:= _
"AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-A90000000001}\PDFFile_8.ico"
_
, IconIndex:=0, IconLabel:="Acrobat Document"

or if the default paste mode is in-line

Dim orng As Range
Set orng = Selection.Range 'define a range at the insertion point
With orng
.Paste 'paste into that range
'and work with the range rather than the selection
.InlineShapes(1).OLEFormat.ConvertTo ClassType:= _
"AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-A90000000001}\PDFFile_8.ico"
_
, IconIndex:=0, IconLabel:="Acrobat Document"
.InlineShapes(1).ConvertToShape 'Are you sure?
End With

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org


--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org


DerbyDad03 wrote:
I am trying to learn how to Select an object in a Word doc via VBA.

Let me give you an example.

First - I know that I can insert this particular object as an Icon
linked to the file, but if someone can tell me why this conversion
code doesn't work, I'm hoping I'll learn something about selecting
objects via VBA. In other words, this question isn't about the best
way to convert an object, it's about how to select an object.

OK, so I have a new document open. I start the recorder and then
perform these steps:

1 - Go to my desktop
2 - Right click a PDF document and select Copy
3 - Go back into the document and use Edit...Paste
4 - Right-click the object and use the PDF Convert option to create a
PDF Icon
5 - Stop the recorder

This produces the following code:

Sub ConvertToIcon()
Application.WindowState = wdWindowStateNormal
Selection.Paste
ActiveDocument.Shapes("Object 2").Select
If Selection.Type wdSelectionShape Then
Selection.InlineShapes(1).ConvertToShape.Select
End If
Selection.ShapeRange(1).OLEFormat.ConvertTo ClassType:= _
"AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-
A90000000001}\PDFFile_8.ico" _
, IconIndex:=0, IconLabel:="Acrobat Document"
End Sub

Now here's the problem: If I copy another PDF and then run this code,
I get an error related to the object not being found, or it tries to
convert the same object over again, etc. Depending on what I've done
before I run the code, the error will change.

It's obvious that it is not selecting the most recently pasted object
because "Object 2" is hard coded. How do I get the code to select the
last object that was pasted without actually knowing what number that
object is?

Thanks.



  #4  
Old March 1st 09, 06:21 PM posted to microsoft.public.word.docmanagement
DerbyDad03
external usenet poster
 
Posts: 48
Default How do I Select an Object with VBA?

Graham,

Thanks for the rsponse, but I may be missing something.

I pasted both sets of code into the VBA editor, as is, and got the
same error message at the lines containing InlineShapes(1)

Run-time error '5941'

The requested member of the collection does not exist.

Is there something I should be editing?

I was also wondering if there is way to access the object while it is
still on the clipboard. That way there would be no way VBA would be
confused about what object I am trying to work on.

On Mar 1, 3:00*am, "Graham Mayor" wrote:
Define a range eg as your code

Dim orng As Range
Set orng = Selection.Range 'define a range at the insertion point
orng.Paste 'paste into that range
orng.Select 'select the range
'now you can operate on the selection
If Selection.Type wdSelectionShape Then
* * Selection.InlineShapes(1).ConvertToShape.Select
End If
Selection.ShapeRange(1).OLEFormat.ConvertTo ClassType:= _
* * "AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _
* * "C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-A90000000001}\PDFFile_8.ico"
_
* * , IconIndex:=0, IconLabel:="Acrobat Document"

or if the default paste mode is in-line

Dim orng As Range
Set orng = Selection.Range 'define a range at the insertion point
With orng
* * .Paste 'paste into that range
* * 'and work with the range rather than the selection
* * .InlineShapes(1).OLEFormat.ConvertTo ClassType:= _
* * "AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _
* * "C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-A90000000001}\PDFFile_8.ico"
_
* * , IconIndex:=0, IconLabel:="Acrobat Document"
* * .InlineShapes(1).ConvertToShape 'Are you sure?
End With

--

Graham Mayor - *Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org


--

Graham Mayor - *Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org


DerbyDad03 wrote:
I am trying to learn how to Select an object in a Word doc via VBA.


Let me give you an example.


First - I know that I can insert this particular object as an Icon
linked to the file, but if someone can tell me why this conversion
code doesn't work, I'm hoping I'll learn something about selecting
objects via VBA. In other words, this question isn't about the best
way to convert an object, it's about how to select an object.


OK, so I have a new document open. I start the recorder and then
perform these steps:


1 - Go to my desktop
2 - Right click a PDF document and select Copy
3 - Go back into the document and use Edit...Paste
4 - Right-click the object and use the PDF Convert option to create a
PDF Icon
5 - Stop the recorder


This produces the following code:


Sub ConvertToIcon()
* *Application.WindowState = wdWindowStateNormal
* *Selection.Paste
* *ActiveDocument.Shapes("Object 2").Select
* *If Selection.Type wdSelectionShape Then
* * * *Selection.InlineShapes(1).ConvertToShape.Select
* *End If
* *Selection.ShapeRange(1).OLEFormat.ConvertTo ClassType:= _
* * * *"AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _
* * * *"C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-
A90000000001}\PDFFile_8.ico" _
* * * *, IconIndex:=0, IconLabel:="Acrobat Document"
End Sub


Now here's the problem: *If I copy another PDF and then run this code,
I get an error related to the object not being found, or it tries to
convert the same object over again, etc. Depending on what I've done
before I run the code, the error will change.


It's obvious that it is not selecting the most recently pasted object
because "Object 2" is hard coded. How do I get the code to select the
last object that was pasted without actually knowing what number that
object is?


Thanks.


  #5  
Old March 1st 09, 06:58 PM posted to microsoft.public.word.docmanagement
Greg Maxey
external usenet poster
 
Posts: 264
Default How do I Select an Object with VBA?

I will try again since you have apparently not seen my reply on 28 Feb
that is showing up in the Office Online Discussion groups but not
here.

In this case you would probably just select the shape in the document
and then run:

Sub Test()
Dim oILShape As InlineShape
Set oILShape = Selection.InlineShapes(1)
oILShape.OLEFormat.ConvertTo ClassType:= _
"AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _

"C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-
A90000000001}\PDFFile.ico",
IconIndex:=0, IconLabel:="Acrobat Document"
End Sub

The recorded often adds a lot of superfilous fluff. E.g., there is no
need to covert an InLineShape to a Shape before converting to a icon.

Easier yet would probably be to simply use EditPaste Special and
paste as an icon.




On Feb 28, 1:41*pm, DerbyDad03 wrote:
I am trying to learn how to Select an object in a Word doc via VBA.

Let me give you an example.

First - I know that I can insert this particular object as an Icon
linked to the file, but if someone can tell me why this conversion
code doesn't work, I'm hoping I'll learn something about selecting
objects via VBA. In other words, this question isn't about the best
way to convert an object, it's about how to select an object.

OK, so I have a new document open. I start the recorder and then
perform these steps:

1 - Go to my desktop
2 - Right click a PDF document and select Copy
3 - Go back into the document and use Edit...Paste
4 - Right-click the object and use the PDF Convert option to create a
PDF Icon
5 - Stop the recorder

This produces the following code:

Sub ConvertToIcon()
* * Application.WindowState = wdWindowStateNormal
* * Selection.Paste
* * ActiveDocument.Shapes("Object 2").Select
* * If Selection.Type wdSelectionShape Then
* * * * Selection.InlineShapes(1).ConvertToShape.Select
* * End If
* * Selection.ShapeRange(1).OLEFormat.ConvertTo ClassType:= _
* * * * "AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _
* * * * "C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-
A90000000001}\PDFFile_8.ico" _
* * * * , IconIndex:=0, IconLabel:="Acrobat Document"
End Sub

Now here's the problem: *If I copy another PDF and then run this code,
I get an error related to the object not being found, or it tries to
convert the same object over again, etc. Depending on what I've done
before I run the code, the error will change.

It's obvious that it is not selecting the most recently pasted object
because "Object 2" is hard coded. How do I get the code to select the
last object that was pasted without actually knowing what number that
object is?

Thanks.


  #6  
Old March 1st 09, 09:28 PM posted to microsoft.public.word.docmanagement
DerbyDad03
external usenet poster
 
Posts: 48
Default How do I Select an Object with VBA?

Thank you for the response.

Actually, just recording the Paste-Special operation works and is
repeatable.

The only thing missing is that the IconLabel simply says Adobe Acrobat
Document. Is there any way to set the IconLabel to the original file
name?

On Mar 1, 1:58*pm, Greg Maxey wrote:
I will try again since you have apparently not seen my reply on 28 Feb
that is showing up in the Office Online Discussion groups but not
here.

In this case you would probably just select the shape in the document
and then run:

Sub Test()
Dim oILShape As InlineShape
Set oILShape = Selection.InlineShapes(1)
oILShape.OLEFormat.ConvertTo ClassType:= _
"AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _

"C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-
A90000000001}\PDFFile.ico",
IconIndex:=0, IconLabel:="Acrobat Document"
End Sub

The recorded often adds a lot of superfilous fluff. E.g., there is no
need to covert an InLineShape to a Shape before converting to a icon.

Easier yet would probably be to simply use EditPaste Special and
paste as an icon.

On Feb 28, 1:41*pm, DerbyDad03 wrote:

I am trying to learn how to Select an object in a Word doc via VBA.


Let me give you an example.


First - I know that I can insert this particular object as an Icon
linked to the file, but if someone can tell me why this conversion
code doesn't work, I'm hoping I'll learn something about selecting
objects via VBA. In other words, this question isn't about the best
way to convert an object, it's about how to select an object.


OK, so I have a new document open. I start the recorder and then
perform these steps:


1 - Go to my desktop
2 - Right click a PDF document and select Copy
3 - Go back into the document and use Edit...Paste
4 - Right-click the object and use the PDF Convert option to create a
PDF Icon
5 - Stop the recorder


This produces the following code:


Sub ConvertToIcon()
* * Application.WindowState = wdWindowStateNormal
* * Selection.Paste
* * ActiveDocument.Shapes("Object 2").Select
* * If Selection.Type wdSelectionShape Then
* * * * Selection.InlineShapes(1).ConvertToShape.Select
* * End If
* * Selection.ShapeRange(1).OLEFormat.ConvertTo ClassType:= _
* * * * "AcroExch.Document.7", DisplayAsIcon:=True, IconFileName:= _
* * * * "C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-
A90000000001}\PDFFile_8.ico" _
* * * * , IconIndex:=0, IconLabel:="Acrobat Document"
End Sub


Now here's the problem: *If I copy another PDF and then run this code,
I get an error related to the object not being found, or it tries to
convert the same object over again, etc. Depending on what I've done
before I run the code, the error will change.


It's obvious that it is not selecting the most recently pasted object
because "Object 2" is hard coded. How do I get the code to select the
last object that was pasted without actually knowing what number that
object is?


Thanks.

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
can not select an object to delete it from word Associates Microsoft Word Help 3 February 4th 09 04:42 AM
How do I select an object set behind a table? Chris O Tables 1 April 14th 08 04:11 AM
is there any short cut key to select work sheet object in word gkrupanand New Users 1 October 24th 06 06:43 PM
how to set select browser object default ayoung Microsoft Word Help 1 July 5th 06 11:10 PM
Need to select object on background layer in Word 2003. Mike Morrow Page Layout 2 June 30th 05 12:30 AM


All times are GMT +1. The time now is 11:43 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 2004-2014 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.