0

我正在开发一个 vsto-word 插件,并将远程图像插入到图片内容控件中的 word 文档中。

一切正常,除了在登录用户连接到远程登录域的机器上。

我没有得到有关该错误的信息。它只是停止执行..图像和一个空的内容控件被插入到word文档中,但是作为两个对象,所以内容控件没有使用它的图像属性。这也是代码停止执行的地方:

public void resultImage(Result r, Dictionary<string, string> wizard)
{
      if (this.hasSelection())
      {
          PictureContentControl picture = getVSTODocument().Controls.AddPictureContentControl(getCCRange(), this.getRandomControlName());

          picture.LockContents = false;
          toggleActiveCCs(false);

          picture.Title = truncateCCTitle(r.title);
          picture.Tag = getWizardString(wizard, r.arrange);

                 try {
                     Image img = Fetcher.getImage(r.data)
                     picture.Image = img;
                 }
                 catch (Exception e) {
                     Log.alert(e.Message);
                 }


          afterInsert(picture.Range);
      }
    }

现在,我使用一个临时文件来存储图像,因为我想知道非管理员用户是否可能没有对内存的写权限......我也在使用一个临时文件(带有 html)来插入一个表,其中工作正常,也作为受限域访问用户......所以我想这也适用于图像!?

我尝试了很多东西,包括:

  • 使用 StreamReaders 和 Memory 流制作图像
  • Locked ContentControls 有类似的行为,只是停止工作,所以我确保它们都已解锁
  • 传输经过 base64 编码的图像,也带有内存流......但这里也一样......

我也在MSDN上问过这个问题

更新 X: 我确定了错误,即 hresult 0x80004005 (E_FAIL: Unspecified failure) 并没有多大帮助……该死。

堆栈跟踪:

Microsoft.Office.Tools.Word.PictureContentControlImpl 的 Microsoft.Office.Interop.Word.InlineShapes.AddPicture(String FileName, Object& LinkToFile, Object& SaveWithDocument, Object& Range) 在 Microsoft.Office.Tools.Word.PictureContentControlImpl XXX.ThisAddIn.resultImage 处的 .set_Image(图像值)(结果 r,Dictionary`2 向导)

这绝对是权限问题,如何检查/设置正确的权限.. ?!!

4

2 回答 2

1

我试图尽可能接近您在问题中使用的缺失代码部分。错误一定是这些缺失部分之一引起的(获取范围、获取图像甚至是插入后的部分)

因此,在隔离的 Office 插件中尽可能多地采用您的代码确实可以正常工作:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    var vstodocument = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.Documents.Add());

    vstodocument.Paragraphs[1].Range.InsertParagraphBefore();

    PictureContentControl picture = vstodocument.Controls.AddPictureContentControl(vstodocument.Paragraphs[1].Range, "pictureControl2");

    picture.LockContents = false;

    picture.Title = "Title";
    picture.Tag = "Tag";

    try
    {
        // Before running put picture.bmp in C:\Users\<user>\Pictures
        string imagePath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\picture.bmp";

        System.Drawing.Bitmap bitmap1 = new System.Drawing.Bitmap(imagePath, true);
        picture.Image = bitmap1;
     }
     catch (Exception ex)
     {
         System.Windows.Forms.MessageBox.Show(ex.Message);
     }
 }

因此,请尝试隔离各个部分,看看是否有任何部分返回了意想不到的东西......

于 2016-02-01T15:31:01.523 回答
1

我找到了一个简单的解决方案......它也适用于这个域登录机器......只需将一个 inlineshap 添加到一个范围,并使 contentcontrol 超出这个范围,例如:

Word.Range rng = getCCRange();

string tempPath = Fetcher.getImage(r.data);
rng.InlineShapes.AddPicture(tempPath);

PictureContentControl picture = getVSTODocument().Controls.AddPictureContentControl(rng, this.getRandomControlName());
于 2016-02-06T16:54:25.530 回答