Word.Shape.Name 最多需要 18 个字符

本文关键字:字符 Shape Name Word | 更新日期: 2023-09-27 18:30:59

我正在使用Word.Interop库。如果我为"Word.Shape.Name"分配少于 18 个字符,那么它可以完美工作,但是当我分配超过 18 个字符时,"Word.Shape.Name"会引发异常。例如

Word.Shape.Name = "This is a test value to assign";

抛出异常

"系统.未经授权访问异常:访问被拒绝。 (HRESULT的例外情况:0x80070005(E_ACCESSDENIED))"。

我应该怎么做才能解决这个问题?整个方法是

 objTargetDocument, ref Scripting.Dictionary dicMarkers, ref Alias.Document objSourceDocument)
    {
        bool blnRetVal = false;
        string strModel = ndModel.Name;
        int lngModel = 0;
        int lngNextModel = 0;
        int lngStart;
        int lngEnd;
        Alias.Document objTemp;
        Microsoft.Office.Interop.Word.Range objRange;
        Shape objShape;
        Object[] astr;
        int n;
        bool bLastModel;
        /*'--------------------------------------------------------------------------------------------------
        '   1. Find model's model marker and the next marker (if any)
        '--------------------------------------------------------------------------------------------------*/
        astr = dicMarkers.Keys();
        for (n = astr.GetLowerBound(0); n <= astr.GetUpperBound(0); n++)
        {
            if (string.Compare(astr[n].ToString(), strModel, true) == 0)
            {
                lngModel = (int)dicMarkers.get_Item(astr[n]);   //PNDC  //dicMarkers.Item(astr(n))
                if (n < astr.GetUpperBound(0))
                {
                    if (string.Compare(astr[n + 1].ToString(), "#end", true) == 0)
                    {
                        lngNextModel = 0;
                        bLastModel = true;
                    }
                    else
                    {
                        lngNextModel = (int)dicMarkers.get_Item(astr[n + 1]);
                        bLastModel = false;
                    }
                }
                else
                {
                    lngNextModel = 0;
                }
                break;
            }
        }
        /*'--------------------------------------------------------------------------------------------------
        '   2. Copy model from original document to new document
        '--------------------------------------------------------------------------------------------------*/
        if (lngModel > 0)
        {
            lngStart = objSourceDocument.Sections[lngModel].Range.Start;
            if (lngNextModel == 0)
            {
                var key = "#end";
                var value = dicMarkers.get_Item(key);
                lngEnd = value;
            }
            else
                lngEnd = objSourceDocument.Sections[lngNextModel].Range.Start; //objSourceDocument.Sections.Last.Index;
            //--------------------------------------------------------------------------------------------------
            //copy original
            objSourceDocument.ActiveWindow.Selection.SetRange(lngStart, lngEnd);
            objSourceDocument.ActiveWindow.Selection.Copy();
            bool bInsertSection = false;
            //paste (append) copied model to the document
            if (objTargetDocument.Sections.First.Index == objTargetDocument.Sections.Last.Index)
            {
                //Target document only has 1 (default) section
                bInsertSection = true;
            }
            else
            {
                if (objTargetDocument.Sections.Last.PageSetup.SectionStart == WdSectionStart.wdSectionNewPage)
                {
                    //Last section is a nextpage section
                    if ((objTargetDocument.Sections.Last.Range.End - (objTargetDocument.Sections.Last.Range.Start) <= 1))
                        //Empty section
                        bInsertSection = false;
                    else
                        bInsertSection = true;
                }
                else
                {
                    //Last section isn't a nextpage
                    bInsertSection = true;
                }
            }
            objTargetDocument.ActiveWindow.Selection.Start = objTargetDocument.Range().End;
            if (bInsertSection)
            {
                objTargetDocument.ActiveWindow.Selection.InsertBreak(WdBreakType.wdSectionBreakNextPage);
                objTargetDocument.ActiveWindow.Selection.Start = objTargetDocument.Range().End;
            }
            objTargetDocument.ActiveWindow.Selection.Collapse();
            objRange = objTargetDocument.ActiveWindow.Selection.Range.Duplicate; //remember range for model marker anchor
            objTargetDocument.ActiveWindow.Selection.Paste();
            objTargetDocument.Variables.Add(m_strModelMarker + strModel);

            // .TextFrame.ContainingRange
            //place model marker (so that we can find our model again)
            objShape = objTargetDocument.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationUpward, 0, 0, 0, 0, objRange);
            objShape.Name = m_strModelMarker + strModel; // This Line Trowing Exception
            objShape.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
            UpdateFields(ref objTargetDocument, ref ndModel);
            blnRetVal = true;
        }
        else
            new Modules.Globals().MsgBoxEx("Kan het bestaande model '" + strModel + "' niet kopieren.", MessageBoxButtons.OK);
        return blnRetVal;
    }

Word.Shape.Name 最多需要 18 个字符

我无法在 Name 属性中找到对 18 个字符限制的任何引用。此外,经过快速测试,它似乎工作正常(即使输入更长的长度):

Word.Shape oShape = oDoc.Shapes.AddLine(0, 0, 0, 0);
oShape.Name = "This is a test value to assign - This is a test value to assign";

因此,您提到的错误很可能是由代码的不同部分引起的。

无论如何,我不确定您是否理解Name属性的确切含义。这根本不会在 Word 文档中看到;这是出于"内部参考目的"的东西。Shapes数组实际上是一个Dictionary,可以通过键入给定形状的索引或名称来访问;也就是说,如果如上所述oShape是文档中的第一个形状,我可以使用以下任何选项访问它:

Word.Shape oShape2 = oDoc.Shapes[1];
Word.Shape oShape2 = oDoc.Shapes["This is a test value to assign - This is a test value to assign"];

出于这个原因,无论如何都没有必要写太长的名字。

这里有一个关于Word 2003的旧讨论,它似乎很相似。

在该讨论中,它建议将名称存储在文档变量对象中(如Document.Variables)。

像这样:

Variables vars = Doc.Variables;
var = vars.Add("myShapeName", "This is a test value to assign");

然后以某种方式将var.Value分配给Word.Shape.Name

编辑

在重新阅读该链接讨论时,无法直接将Doc.Variable分配给Name属性。可以将形状的Name设置为短的唯一标识符,并使用它来从Variables中检索长字符串,只要您需要它。

void SetLongName(Shape shape, string uniqueId, string longName)
{
    shape.Name = uniqueId;
    Variables vars = Doc.Variables;
    var = vars.Add(uniqueId, longName);
}
string GetLongNameOfShape(Shape shape)
{
    return GetLongNameById(shape.Name);
}
string GetLongNameById(string uniqueId)
{
    Variables vars = Doc.Variables;
    return vars.get_Item(uniqueId).Value;
}