none
problem setting Width for InlineShape RRS feed

  • Question

  • Hi all,

    I recently upgraded my machine to Windows 7 and Office 10.

    Since then, I cannot set the width of an inline shape I added using vba.

    The code worked for years...

    When trying to set the width, Office freezes and I get an error : The remote procedure call failed. (Exception from HRESULT: 0x800706BE)

    Here is my code:

    objWordInlineShape = objWordDoc.Sections(1).Headers(1).Range.InlineShapes.AddPicture(sFileName)
    
    objWordInlineShape.LockAspectRatio = -1
    
    objWordInlineShape.Width = 440
    objWordInlineShape.Height = 40
    

    TIA

    Guy


    TIA Guy Cohen
    • Moved by Cindy Meister MVPModerator Monday, February 7, 2011 5:53 PM VBA, not VSTO-related (From:Visual Studio Tools for Office)
    Sunday, February 6, 2011 7:45 AM

All replies

  • Hi Guy,

    If you press F8 to run your code step by step, you will find it work fine! I found it will get exception only at first time of running this code. If you success to run this code, the exception will never occur.

    I hope it helps.

    Have a great day,

    Tom Xu


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 8, 2011 7:36 AM
    Moderator
  • Hi Tom and thanks for your reply.

    It did not work with F8

    Any other idea please?

    Guy


    TIA Guy Cohen
    Tuesday, February 8, 2011 8:47 AM
  • Hi Guy,

    I show my snippet of the VBA, it works fine on my side. Please note the different between us.

    Sub TestInLineShape()
      Dim objWordDoc As Document
      Dim objWordInlineShape As InlineShape
      
      Set objWordDoc = Application.ActiveDocument
      
      If objWordDoc.Sections(1).Headers(1).Range.InlineShapes.Count >= 1 Then
        For i = 1 To objWordDoc.Sections(1).Headers(1).Range.InlineShapes.Count Step 1
          objWordDoc.Sections(1).Headers(1).Range.InlineShapes(i).Delete
        Next i
      End If
      
      Set objWordInlineShape = objWordDoc.Sections(1).Headers(1).Range.InlineShapes.AddPicture("C:\Users\v-yangxu\Pictures\Picture\mapi.JPG")
      
      objWordInlineShape.LockAspectRatio = -1
      objWordInlineShape.Width = 440
      objWordInlineShape.Height = 40
      
      Set objWordDoc = Nothing
    End Sub
    

    I hope it helps,

    Tom Xu


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 8, 2011 9:00 AM
    Moderator
  • Hi Guy,

    Did you solve you question? If you have solved it, would you please share your solutions or experience here? It will be very beneficial for other community members who have similar questions to see how you have dealt with your issue. I am writing to check the status of this thread. If you have any questions about it, you are able to unmark it.

    Have a great day,

    Tom Xu


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 15, 2011 8:22 AM
    Moderator
  • Hi there

    I am sorry for my late reply.

    Still it does not work

    My code is the same as yours.

     

    Guy


    TIA Guy Cohen
    Tuesday, February 22, 2011 3:15 AM
  • Had the same problem ... following line works fine on "Windows Server 2008, Office 2010" but throw error on "Windows7, Office 2010"

    InlineShape shape = wordApp.Selection.InlineShapes.AddPicture(item.FullName, ref falseObject, ref trueObject, ref missing);

    >>>> shape.Width = aValue; <<<< Exception -> "Command failed"

    Fixed it by:

                    Microsoft.Office.Interop.Word.Shape shape2 = shape.ConvertToShape();

                    shape2.Width = (float)aValue;

                    shape2.ConvertToInlineShape();

    regards



    Friday, April 27, 2012 8:19 AM
  • Hi Michael,

    with all due respect, this is not the answer to the question that was asked.

    Your code CONVERTS the INLINE SHAPE to a SHAPE.  This will not work.  I have the same problem and cannot convert to a shape -- must stay an inline shape.  How can the properties of the inline shape be modified?

    Could you possibly provide an answer to the question that was asked? Thank you


    Warm Regards, Crystal http://www.AccessMVP.com/strive4peace * (: have an awesome day :)

    Saturday, April 28, 2012 2:27 PM
  • Hi Guy

    I confirm the issue.

    But since you're locking the aspect ratio, you should be able to set only the Height property and the width will adjust automatically. Setting the Height does not cause an error.


    Cindy Meister, VSTO/Word MVP

    Sunday, April 29, 2012 6:43 AM
    Moderator
  • Hi Crystal

    If my suggestion to the OP doesn't work because you're not locking the aspect ratio, then you can use Michael's suggestion, just convert it to an InlineShape afterwards.

    Sub AddPicAndChangeWidth()
    Dim objWordInlineShape As Word.InlineShape
    Dim objWordShape As Word.Shape
    Dim objWordDoc As Word.Document
    Dim sFileName As String
    sFileName = "C:\Test\Zapotec.bmp"
    Set objWordDoc = ActiveDocument
    'Set objWordInlineShape = objWordDoc.Sections(1).Headers(1).Range.InlineShapes.AddPicture(sFileName)
    Set objWordShape = objWordDoc.Sections(1).Headers(1).Shapes.AddPicture(sFileName)
    objWordShape.LockAspectRatio = -1
    objWordShape.Width = 440 'causes error
    'objWordInlineShape.Height = 40
    Set objWordInlineShape = objWordShape.ConvertToInlineShape
    End Sub


    Cindy Meister, VSTO/Word MVP

    Sunday, April 29, 2012 6:49 AM
    Moderator
  • Hi all

    and thank you so much for re-trying.

    I decided to convert all my reports to HTML :)

    Its easier and MUCH faster (when you have LOTZ of rows...)

    And you can make the text blink and scroll :)

    Guy Cohen


    TIA Guy Cohen

    Sunday, April 29, 2012 6:53 AM
  • Hi Guy

    Thank you for letting us know you were able to work around the issue :-)


    Cindy Meister, VSTO/Word MVP

    Sunday, April 29, 2012 6:59 AM
    Moderator
  • Hi Cindy,

    thanks for jumping in and giving me what I needed to make the duct-tape work! Your site has so much valuable code, I have learned so much from you.  thank you! thank you!

    Warm Regards,
    Crystal

    Learn Access on YouTube
    http://www.youtube.com/user/LearnAccessByCrystal

     *    (: have an awesome day :)


    Warm Regards, Crystal http://www.AccessMVP.com/strive4peace * (: have an awesome day :)

    Tuesday, June 19, 2012 6:08 PM