none
Help in selecting shape on specified page in a word document RRS feed

  • Question

  • Hi all,

    I have 1 picture (InlineShapes object) each on different page of the word document and there are 3 pages in the document. so there are 3 shapes in all. I want to select each shape, one at a time. I want to do it like "select a shape on 1st, 2nd page etc"

    Please help me in selecting a shape on specified page as if I select InlineShapes(1).Select, that shape may appear on the second page.


    http://learn2automate.wordpress.com Vijay Gupta

    Saturday, December 28, 2013 10:33 AM

Answers

  • Try this as starting point. You can add error handling and modify it to suit your needs.

    Sub SelectPicture()
        Dim lngPage As Long
        Dim rngPage As Range
        Dim shp As InlineShape
        lngPage = Val(InputBox("Please enter the page number"))
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=lngPage
        Set rngPage = ActiveDocument.Bookmarks("\page").Range
        Set shp = rngPage.InlineShapes(1)
        shp.Select
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by gupta vijay Wednesday, January 1, 2014 11:32 AM
    Saturday, December 28, 2013 11:41 AM

All replies

  • Try this as starting point. You can add error handling and modify it to suit your needs.

    Sub SelectPicture()
        Dim lngPage As Long
        Dim rngPage As Range
        Dim shp As InlineShape
        lngPage = Val(InputBox("Please enter the page number"))
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=lngPage
        Set rngPage = ActiveDocument.Bookmarks("\page").Range
        Set shp = rngPage.InlineShapes(1)
        shp.Select
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by gupta vijay Wednesday, January 1, 2014 11:32 AM
    Saturday, December 28, 2013 11:41 AM
  • Thanks Hans,

    I am using the following code:

    Set objDocument =GetObject(word file path)
    Set objWord=objDocument.Application
    wait(2)
    Set objInlineShapes = objDocument.InlineShapes
    Set objShapes = objDocument.Shapes
    Set PRange1=objWord.Selection.GoTo(wdGoToPage,wdGoToDirection,wdGoToFirst)                   
    Set PRange2=objWord.Selection.GoTo(wdGoToPage,wdGoToDirection,wdGoToSecond)             
    Set PRange3=objWord.Selection.GoTo(wdGoToPage,wdGoToDirection,wdGoToThird)      

    I added the following line:

    Set rngPage = ActiveDocument.Bookmarks("\page").Range

    msgbox rngPage.InlineShapes.Count

    But it is giving error. Am I doing something wrong?         



    http://learn2automate.wordpress.com Vijay Gupta

    Sunday, December 29, 2013 5:52 AM
  • It would have been helpful if you had told us what the error message said.

    The constant wdGoToFirst exists, but wdGoToSecond and wdGoToThird don't exist. For example, to go to the second page, use

    Set PRange2=objWord.Selection.GoTo(wdGoToPage, wdGoToAbsolute, 2)

    Since you're using Automation to control Word, you must make sure that all Word objects are fully qualified. ActiveDocument doesn't refer to any of your Word objects. Change the line

    Set rngPage = ActiveDocument.Bookmarks("\page").Range

    to

    Set rngPage = objDocument.Bookmarks("\page").Range


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, December 29, 2013 10:11 AM
  • Thanks hans. It works fine.

    http://learn2automate.wordpress.com Vijay Gupta

    Wednesday, January 1, 2014 11:31 AM