Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:16:49+00:00 2026-06-04T06:16:49+00:00

This article says that you need to use resizable MemoryStreams when working with the

  • 0

This article says that you need to use resizable MemoryStreams when working with the OpenXML SDK, and the sample code works fine.

However, when I translate the sample C# code into F#, the document remains unchanged:

open System.IO
open DocumentFormat.OpenXml.Packaging
open DocumentFormat.OpenXml.Wordprocessing

[<EntryPoint>]
let Main args =
    let byteArray = File.ReadAllBytes "Test.docx"

    use mem = new MemoryStream()
    mem.Write(byteArray, 0, (int)byteArray.Length)

    let para = new Paragraph()
    let run = new Run()
    let text = new Text("Newly inserted paragraph")
    run.InsertAt(text, 0) |> ignore
    para.InsertAt(run, 0) |> ignore

    use doc = WordprocessingDocument.Open(mem, true)
    doc.MainDocumentPart.Document.Body.InsertAt(para, 0) |> ignore

    // no change to the document
    use fs = new FileStream("Test2.docx", System.IO.FileMode.Create)
    mem.WriteTo(fs)

    0

It works fine when I use WordprocessingDocument.Open("Test1.docx", true), but I want to use a MemoryStream. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:16:51+00:00Added an answer on June 4, 2026 at 6:16 am

    Changes you’re making to doc are not reflected in MemoryStream mem until you close doc. Placing doc.Close() as below

    ...
    doc.MainDocumentPart.Document.Body.InsertAt(para, 0) |> ignore 
    doc.Close()
    ...
    

    fixes the problem and you’ll get text Newly inserted paragraph at the top of your Test2.docx.

    Also your snippet is missing one required reference:

    open DocumentFormat.OpenXml.Packaging 
    

    from WindowsBase.dll.

    EDIT: as ildjarn pointed out the more F#-idiomatic would be the following refactoring:

    open System.IO
    open System.IO.Packaging
    open DocumentFormat.OpenXml.Packaging 
    open DocumentFormat.OpenXml.Wordprocessing 
    
    [<EntryPoint>] 
    let Main args = 
        let byteArray = File.ReadAllBytes "Test.docx" 
    
        use mem = new MemoryStream() 
        mem.Write(byteArray, 0, (int)byteArray.Length) 
    
        do
            use doc = WordprocessingDocument.Open(mem, true) 
            let para = new Paragraph() 
            let run = new Run() 
            let text = new Text("Newly inserted paragraph") 
            run.InsertAt(text, 0) |> ignore     
            para.InsertAt(run, 0) |> ignore
            doc.MainDocumentPart.Document.Body.InsertAt(para, 0) |> ignore 
    
        use fs = new FileStream("Test2.docx", FileMode.Create) 
        mem.WriteTo(fs) 
    
        0 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This article on MSDN states that you can use as many try catch blocks
I just read this nice article that taught me how to use inheritance (Table-per-hirarchy).
i'm checking this link: http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html It says that in order tu ouse C2DM, you
http://www.oracle.com/technetwork/articles/javase/index-140168.html#binsch - This article says JAXB requires that the XML document you want to
This article says: Every prime number can be expressed as 30k±1 , 30k±7 ,
I have read from this article http://codahale.com/how-to-safely-store-a-password/ and it says using a salt isn't
As per this MSDN article: W3C Extended Log File Format (IIS 6.0) It says
This article here suggests to use -XX:+UseParNewGC To enable a parallel young generation GC
this article suggests you can use Color c = Color.decode(FF0096); however this understandably throws
I need to use SQLCLR to make a stored procedure that uses stuff in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.