我该如何分配给ActiveSheet.PageSetup.LeftHeaderPicture.FileName的嵌入图像的相对路径? [英] How do I assign a relative path of an embed image to a ActiveSheet.PageSetup.LeftHeaderPicture.FileName?

查看:1230
本文介绍了我该如何分配给ActiveSheet.PageSetup.LeftHeaderPicture.FileName的嵌入图像的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有2 SSCCEs此:

There are 2 SSCCEs for this:

我添加了一个图片为我的VSTO。文档级外接通过右键单击解决方案 - >添加 - >现有项目 - > myPic.jpg。

I have added a picture to my VSTO document-level add-in by right clicking the solution -> add -> existing item -> myPic.jpg.

现在我有一个Excel电子表格的工作,并希望将图片添加到顶部左侧头球。使用 PageSetup.LeftHeaderPicture.FileName 和提供的绝对的路径图片加载在调试就好了。

Now I am working with an Excel spreadsheet and want to add a picture to the top left header. Using PageSetup.LeftHeaderPicture.FileName and providing an absolute path to the picture it loads just fine while debugging.

当我试图改变路径不绝对和这样说。

When I am trying to change the path to not-absolute and say something like

ActiveSheet.PageSetup.LeftHeaderPicture.FileName = "\\Assets\\myPic.jpg"

我不断收到一个 HRESULT:0x800A03EC 例外。

I keep getting a HRESULT: 0x800A03EC exception.

我想我没有得到的访问 Assets\myPic.jpg

I think I am not getting the correct syntax for to access the Assets\myPic.jpg.

我添加了一个新的资源,选择现有项目并选择了myPic.jpg。我可以通过 Resource1.myPic 访问它,但的 ActiveSheet.PageSetup.LeftHeaderPicture 是的只读 ...

I have added a new resource, selected an existing item and selected the myPic.jpg. I can access it via Resource1.myPic but ActiveSheet.PageSetup.LeftHeaderPicture is read-only...

PageSetup.LeftHeaderPicture.FileName 需要一个字符串输入参数,我不知道如何检索我已经嵌入资源的路径...

The PageSetup.LeftHeaderPicture.FileName needs a string type parameter and I am not sure how to retrieve the path to my already embed resource...

我如何嵌入我的解决方案图片作为一种资源(或只是现有项目)能够与 PageSetup.LeftHeaderPicture.FileName使用

How do I embed a picture in my solution as a resource (or just an existing item) to be able to use it with PageSetup.LeftHeaderPicture.FileName?

推荐答案

好吧,我已经解决了它。

Right, I have solved it.

补充现有项目 myPic.jpg 来解决,并设置生成操作内容复制到输出目录复制始终,但我相信你可以设置它来复制,如果新的的)

Added an existing item myPic.jpg to the solution and set Build Action to Content and Copy to Output Directory to Copy Always (but I am sure you can set it to copy if newer)

请注意:有了这个设置你的文件总是被抄袭所公布的目录。

现在你在你的代码需要的是

Now all you need in your code is

ws.PageSetup.LeftHeaderPicture.Filename = 
                           AppDomain.CurrentDomain.BaseDirectory + "\\myPic.jpg";
ws.PageSetup.LeftHeader = "&G";



AppDomain.CurrentDomain.BaseDirectory 是非常好 这里

AppDomain.CurrentDomain.BaseDirectory is very well explained here

和最终的结果如预期

这篇关于我该如何分配给ActiveSheet.PageSetup.LeftHeaderPicture.FileName的嵌入图像的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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