VBA Word - 切换Shape.RelativeVerticalPosition的值物理移动形状 [英] VBA Word - Switching the value of Shape.RelativeVerticalPosition physically moves a shape

查看:145
本文介绍了VBA Word - 切换Shape.RelativeVerticalPosition的值物理移动形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我一直在努力解决问题,找不到任何相关内容......


'p>当我改变(在  VBA) 所述Shape.RelativeVerticalPosition,它 将改变的形状的物理位置 它发生我已经在包含的范围内进行的Range.Find.Execute后才形状。但是,如果我通过布局选项在Word中手动更改它
,则形状将不会移动。


我正在使用VS2010 C#office 2010.以下是一些示例代码...它需要一个位于同一文件夹上的空文档TestWord.docx,然后打开文档,添加一个文本框,然后执行"查找"...在"查找"之前和之后都有一个测试,
在"查找"之后,文本框将根据RelativeVerticalPosition设置上下跳转。


任何提示都将不胜感激!


 

使用System.Collections.Generic;

使用System.IO;

使用System.Windows.Forms;

使用Microsoft.Office.Interop.Word;


命名空间TestWord

{

   公共静态类TestWord2

    {

        public static void Start()

        {

            object oReplaceAll = WdReplace.wdReplaceAll;

            object oNullValue = System.Reflection.Missing.Value;

            object oTrueValue = true;


           对象的docx = Path.Combine(System.Windows.Forms.Application.StartupPath," TestWord.docx");

        &NBSP ;   ApplicationClass WordAppl;


            WordAppl = new ApplicationClass();

            WordAppl.Visible = true;


           文档DOC = WordAppl.Documents.Open(参照DOCX,REF oNullValue,REF oTrueValue,参考oNullValue,

          &NBSP ;     REF oNullValue,REF oNullValue,REF oNullValue,REF oNullValue,REF oNullValue,参考oNullValue,

       &NBSP ;        REF oNullValue,REF oNullValue,REF oNullValue,REF oNullValue,REF oNullValue,REF oNullValue);


  &NBSP ;        对象oStory = doc.StoryRanges [WdStoryType.wdMainTextStory];


            doc.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizo​​ntal,

                 100,100,100,50F,参考oStory);


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;列表与LT;形状> ReferenceShape =新列表< Shape>();


            foreach(doc.StoryRanges中的范围r)

            {

                foreach(r.ShapeRange中的形状)

                {

                    ReferenceShape.Add(s);

                }


               如果(r.StoryType == WdStoryType.wdTextFrameStory)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

                    ChangePos([否ISSUE - 之前找到故事" + r.StoryType,ReferenceShape);


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;对象oFind,oReplaceValue,oReplaceType;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; oFind = QUOT;东西英寸;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; oReplaceValue = QUOT;英寸;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; oReplaceType = oReplaceAll;


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; r.Find.Execute(参考oFind,REF oNullValue,REF oNullValue,参考oNullValue,

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; REF oNullValue,REF oNullValue,REF oTrueValue,REF oNullValue,参考oNullValue,

&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; REF oReplaceValue,REF oReplaceType,REF oNullValue, REF oNullValue,REF oNullValue,REF oNullValue);


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP; ChangePos("形状MOVED - 寻找故事后" + r.StoryType,ReferenceShape);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP; }
            }
        }


        private static void ChangePos(string itr,List< Shape> ReferenceShape)

        {

            foreach(参考形状中的形状)

            {

               而(MessageBox.Show(QUOT;按YES保持切换RelativeVerticalPosition\r\\\
\r\\\
" + ITR,"继续] ;, MessageBoxButtons.YesNo,MessageBoxIcon.Question)
= = DialogResult.Yes)

                {


                    s.RelativeVerticalPosition =

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; (s.RelativeVerticalPosition == WdRelativeVerticalPosition.wdRelativeVerticalPositionPage)

&NBSP;?&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; WdRelativeVerticalPosition.wdRelativeVerticalPositionParagraph:

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }


            }
        }
    }¥b $ b}

解决方案

你好,


谢谢你的支持参加这个论坛。我试图让一些高级工程师参与这个问题,这需要一些时间。非常感谢您的耐心等待。


很抱歉给您带来不便,祝您有个愉快的一天!


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;
                }

            }
        }
    }
}

解决方案

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!


这篇关于VBA Word - 切换Shape.RelativeVerticalPosition的值物理移动形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆