How can I embed any file type into Microsoft Word using OpenXml 2.0

前端 未结 5 2078
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 06:09

I spent a lot of time trying to figure out a good way to embed any file into Microsoft Word using OpenXml 2.0; Office documents are fairly easy but what about other file typ

5条回答
  •  北海茫月
    2020-11-30 06:49

    This is a great answer and it helped me a lot, but the potential bug that user bic mentioned also exists

    in

    OpenXmlEmbeddedObject(FileInfo fileInfo, bool displayAsIcon)
    

    at line 242,

    _filePathAndName = fileInfo.ToString();
    

    in

    SetupOleFileInformation()
    

    at line 264,

     object objectFilename = _fileInfo.ToString();
    

    line 289, and

    Image image = Image.FromFile(_fileInfo.ToString());
    

    line 293

    wordDocument.InlineShapes.AddPicture(_fileInfo.toString(), ref _objectMissing, ref _objectTrue, ref _objectMissing);
    

    All of these need to be "FullName" instead of "ToString()" if the code should work with relative paths as well. Hope this helps anyone who wants to use D Lyonnais's code!

提交回复
热议问题