none
VBA Word - Switching the value of Shape.RelativeVerticalPosition physically moves a shape RRS feed

  • Question

  • Hi,

    I have been struggling with an issue for a while and couldn't find anything relevant...

    When I change (in VBA ) the Shape.RelativeVerticalPosition, it would change the physical position of a shape. It happens only after I have performed a Range.Find.Execute on the range containing the shape. However if I then change it manually in Word through the Layout Option the shape will not move.

    I am using VS2010 C# office 2010. Below are some sample code... it requires an empty document TestWord.docx located on the same folder, then it open the doc, add a textbox, and execute a "find"... there is a test before and after the "find", where after the "find", the textbox will jump up and down base on the RelativeVerticalPosition setting.

    Any hint would be greatly appreciated!

     

    using System.Collections.Generic;
    using System.IO;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Word;

    namespace TestWord
    {
        public static class TestWord2
        {
            public static void Start()
            {
                object oReplaceAll = WdReplace.wdReplaceAll;
                object oNullValue = System.Reflection.Missing.Value;
                object oTrueValue = true;

                object docx = Path.Combine(System.Windows.Forms.Application.StartupPath, "TestWord.docx");
                ApplicationClass WordAppl;

                WordAppl = new ApplicationClass();
                WordAppl.Visible = true;

                Document doc = WordAppl.Documents.Open(ref docx, ref oNullValue, ref oTrueValue, ref oNullValue,
                    ref oNullValue, ref oNullValue, ref oNullValue, ref oNullValue, ref oNullValue, ref oNullValue,
                    ref oNullValue, ref oNullValue, ref oNullValue, ref oNullValue, ref oNullValue, ref oNullValue);

                object oStory = doc.StoryRanges[WdStoryType.wdMainTextStory];

                doc.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
                    100, 100, 100, 50f, ref oStory);

                List<Shape> ReferenceShape = new List<Shape>();

                foreach (Range r in doc.StoryRanges)
                {
                    foreach (Shape s in r.ShapeRange)
                    {
                        ReferenceShape.Add(s);
                    }

                    if (r.StoryType == WdStoryType.wdTextFrameStory)
                    {
                        ChangePos("NO ISSUE - Before find for story " + r.StoryType, ReferenceShape);

                        object oFind, oReplaceValue, oReplaceType;
                        oFind = "Something";
                        oReplaceValue = "";
                        oReplaceType = oReplaceAll;

                        r.Find.Execute(ref oFind, ref oNullValue, ref oNullValue, ref oNullValue,
                            ref oNullValue, ref oNullValue, ref oTrueValue, ref oNullValue, ref oNullValue,
                            ref oReplaceValue, ref oReplaceType, ref oNullValue, ref oNullValue, ref oNullValue, ref oNullValue);

                        ChangePos("Shape MOVED - after find for story " + r.StoryType, ReferenceShape);
                    }
                }
            }

            private static void ChangePos(string itr, List<Shape> ReferenceShape)
            {
                foreach (Shape s in ReferenceShape)
                {
                    while (MessageBox.Show("Press YES to keep switching RelativeVerticalPosition\r\n\r\n" + itr, "Continue?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {

                        s.RelativeVerticalPosition =
                            (s.RelativeVerticalPosition == WdRelativeVerticalPosition.wdRelativeVerticalPositionPage) ?
                            WdRelativeVerticalPosition.wdRelativeVerticalPositionParagraph :
                            WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
                    }

                }
            }
        }
    }

    Thursday, May 16, 2013 4:31 PM

All replies

  • Hello,

    Thanks for your participation in this forum. I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.

    Sorry for any inconvenience and have a nice day!


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 20, 2013 12:58 PM
    Moderator
  • Hi Puyo,

    I went through the code and tried to reproduce the issue. I could not repro the issue reported [physical movement of shape] in either cases when my word application is "Maximized". I was trying to reproduce the issue with Office 2010 Sp1. [Word 14.0.6129.5000 and MSO 14.0.6129.5000. ] if you are using an older version please update to latest version of Office 2010.

    If the issue still reproduce, we probably need to troubleshoot deeper. Because of its complexity your question falls into the paid support category which requires a more in-depth level of support.  If the support engineer determines that the issue is the result of a bug the service request will be a no-charge case and you won't be charged. Please visit the below link to see the various paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone


    Sangeeth,MSFT

    Tuesday, May 21, 2013 4:04 PM
  • Hi Sangeeth,

    I believe we have same version of Word (see below). The shape still moves no matter the window state is Maximum or Restore, or changing the state through the code or by clicking the button. Were you implying that you can repro the issue when it is not "Maximized"?

    In our application we always hide the process (application.visible = false).

    We have been struggling with this since Word 2007. And now in Word 2013, the shape moves even before I execute the Find. We have been having this issue with Windows 7 + Word 2007 + .NET 3.5, Windows 7 + Word 2010 + .NET 3.5/4.0, Windows 8 (upgraded) + Word 2010 + .NET 3.5/4.0, Windows 8 (fresh install) + Word 2013 + .NET 4.0. Our development machines always apply update on the Word / Windows OS.

    Tuesday, May 21, 2013 4:59 PM