我试图嵌入一个.dotx文件,得到Type Mismatch错误,无论我尝试什么,tStream都保持为null。 [英] I am trying to embed a .dotx file, getting Type Mismatch error and tStream remains null no matter what I try.
问题描述
Assembly _assembly = Assembly.GetExecutingAssembly();
Stream tStream = _assembly.GetManifestResourceStream(typeof(Document),SSS.dotx);
object oTemplate = tStream;
oDoc = oWord.Documents.Add(ref oTemplate,ref oMissing,ref oMissing,ref oMissing);
//我将.dotx添加为现有项目并将其构建操作设置为Embedded。
Assembly _assembly = Assembly.GetExecutingAssembly();
Stream tStream = _assembly.GetManifestResourceStream(typeof(Document), "SSS.dotx");
object oTemplate = tStream;
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
// I added the .dotx as an existing item and set it's build action to Embedded.
推荐答案
1)
- 阅读: http://support2.microsoft.com/kb/319292 [ ^ ]
- 在解决方案资源管理器中,选择Resources文件夹中的文件。设置它的构建操作到嵌入式资源(你在写的时候做了这个)
- 检查_assembly.GetManifestResourceNames();
名称的结果。资源名称使用默认名称空间和.resources进行限定,例如WindowsFormsApplication1.Resources.SSS.dotx
- 请注意,该资源你没有类型Document,因为你嵌入了文件表示。
2)你不能从这样的模板创建文档。仅限文件,例如此处 [ ^ ]:
1)
- Read this: http://support2.microsoft.com/kb/319292[^]
- In Solution Explorer select the file in Resources folder. Set it's Build Action to Embedded Resource (you made this as you wrote)
- Check_assembly.GetManifestResourceNames();
result for the names. The resource name is qualified with default namespace and ".resources", likeWindowsFormsApplication1.Resources.SSS.dotx
- Please note, that the resource you have is not of type Document, as you embedded file representation.
2) You can't create document from such a template. Only from file, like here[^]:
oWord.Documents.Add(@"C:\Test\SampleTemplate.dotx");
所以:从没有类型的资源中获取嵌入文件,将其保存到临时文件并传递该文件的路径为Add方法。
这篇关于我试图嵌入一个.dotx文件,得到Type Mismatch错误,无论我尝试什么,tStream都保持为null。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!