使用Office.Interop.Word打开.bin文件

时间:2013-06-07 15:20:46

标签: c# ms-word office-interop

我有一个存储为.bin文件的Word 8.0文档,我正在尝试使用C#和Microsoft.Office.Interop在MS Word中重新打开。我可以打开文档并显示内容,但它不适用于文档的任何样式(如字体大小,下划线等)。

我的代码如下:

object pathToBin = @"\\server\folder\123.bin";
object missingType = Type.Missing;
object readOnly = true;
object isVisible = false;
object documentFormat = WdOpenFormat.wdOpenFormatDocument97;

Application applicationclass = new Application();
applicationclass.Documents.Open(ref pathToBin,
                                ref readOnly,
                                ref missingType, ref missingType, ref missingType,
                                ref missingType, ref missingType, ref missingType,
                                ref missingType, ref missingType, ref isVisible,
                                ref missingType, ref missingType, ref missingType,
                                ref missingType, ref missingType);
applicationclass.Visible = true;
Document document = applicationclass.ActiveDocument;

当文档本身打开时,它似乎在字面上显示如下样式:

Heading 1
Heading 1
Heading 2
Heading 2
Heading 3
Heading 3
Hyperlink
Hyperlink

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我看到你设置了documentFormat对象,但没有看到你将它用作对Documents.Open的调用的参数。

鉴于文件扩展名为.bin,可能是Word难以找出缺少此参数的文档格式吗?

答案 1 :(得分:1)

注意到@Chris Spicer,因为你设置documentFormat,你应该在致电applicationclass.Documents.Open时设置它......

...但确保在正确的位置设置正确的参数,因为它们实际上已移位(您为IsVisible参数传递了Encoding。)。< / p>

Documents.Open方法

Document Open(
[In] ref object FileName,                              PathToBin OK
[In, Optional] ref object ConfirmConversions,          here you have readOnly!
[In, Optional] ref object ReadOnly,                    Missing
[In, Optional] ref object AddToRecentFiles,            Missing
[In, Optional] ref object PasswordDocument,            Missing
[In, Optional] ref object PasswordTemplate,            Missing
[In, Optional] ref object Revert,                      Missing
[In, Optional] ref object WritePasswordDocument,       Missing
[In, Optional] ref object WritePasswordTemplate,       Missing
[In, Optional] ref object Format,                      Missing
[In, Optional] ref object Encoding,                    here you have IsVisible!
[In, Optional] ref object Visible,                     Missing
[In, Optional] ref object OpenAndRepair,               Missing
[In, Optional] ref object DocumentDirection,           Missing
[In, Optional] ref object NoEncodingDialog,            Missing
[In, Optional] ref object XMLTransform                 Missing

所以这段代码:

applicationclass.Documents.Open(ref pathToBin,
                            ref readOnly,
                            ref missingType, ref missingType, ref missingType,
                            ref missingType, ref missingType, ref missingType,
                            ref missingType, ref missingType, ref isVisible,
                            ref missingType, ref missingType, ref missingType,
                            ref missingType, ref missingType);

应该是:

applicationclass.Documents.Open(ref pathToBin,
                            ref missingType,
                            ref readOnly, ref missingType, ref missingType,
                            ref missingType, ref missingType, ref missingType,
                            ref missingType, ref documentFormat, ref missingType,
                            ref isVisible, ref missingType, ref missingType,
                            ref missingType, ref missingType);