Word.Shape.Name最多18个字符

时间:2013-09-12 10:29:26

标签: c# .net

我正在使用Word.Interop库。如果我为'Word.Shape.Name'分配少于18个字符,那么它工作正常但当我分配超过18个字符时,'Word.Shape.Name'抛出异常。 e.g

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

抛出异常

  

“System.UnauthorizedAccessException:访问被拒绝。   (来自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;

    }

2 个答案:

答案 0 :(得分:1)

我无法在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"];

因此,无论如何都不需要写太长的名字。

答案 1 :(得分:1)

有关Word 2003的旧讨论here似乎很相似。

在该讨论中,它建议将名称存储在文档变量对象中(如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;
}