Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Open template word doc and replace values

Open template word doc and replace values

Scheduled Pinned Locked Moved ASP.NET
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    byka
    wrote on last edited by
    #1

    I have a web application where I have a template (word) which I need to open/save and replace some values at run time. How do I do this?

    C Richard DeemingR 2 Replies Last reply
    0
    • B byka

      I have a web application where I have a template (word) which I need to open/save and replace some values at run time. How do I do this?

      C Offline
      C Offline
      CHill60
      wrote on last edited by
      #2

      How to get an answer to your question - ASP.NET Discussion Boards[^]

      1 Reply Last reply
      0
      • B byka

        I have a web application where I have a template (word) which I need to open/save and replace some values at run time. How do I do this?

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        If it's a new-format file (.docx), then you'll need to use something like DocX[^] or the Open XML SDK 2.5 for Office[^]. If it's an old-format file (.doc), then you'll struggle to find a non-commercial library to manipulate it. NB: Don't be tempted to try Office Interop; aside from needing an Office license for your server, it's not supported in ASP.NET:

        Considerations for server-side Automation of Office[^]

        Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        B 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          If it's a new-format file (.docx), then you'll need to use something like DocX[^] or the Open XML SDK 2.5 for Office[^]. If it's an old-format file (.doc), then you'll struggle to find a non-commercial library to manipulate it. NB: Don't be tempted to try Office Interop; aside from needing an Office license for your server, it's not supported in ASP.NET:

          Considerations for server-side Automation of Office[^]

          Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          B Offline
          B Offline
          byka
          wrote on last edited by
          #4

          Below is my code. I can't figure out how to find specific text and replace it

          Dim byteArray As Byte()
          Dim myDataTable As DataTable = objDB.GetTemplates(plan.YearCode, "Templates", plan.State)

                  If Not myDataTable Is Nothing AndAlso myDataTable.Rows.Count > 0 Then
                      byteArray = DirectCast(myDataTable.Rows(0).Item("Template"), Byte())
                  Else
                      Throw New Exception("Unable to retrieve the specified template")
                  End If
          
          
                  Using mem As MemoryStream = New MemoryStream
                      mem.Write(byteArray, 0, CInt(byteArray.Length))
                      Using doc As WordprocessingDocument = WordprocessingDocument.Open(mem, True)
          
                      ??? How do I find specific word and replace it
                      
                     Helper.DownloadStream("Schedule" &" .docx", mem, Helper.WORD\_CONTENT\_TYPE)
                      End Using
                  End Using
          
          Richard DeemingR 1 Reply Last reply
          0
          • B byka

            Below is my code. I can't figure out how to find specific text and replace it

            Dim byteArray As Byte()
            Dim myDataTable As DataTable = objDB.GetTemplates(plan.YearCode, "Templates", plan.State)

                    If Not myDataTable Is Nothing AndAlso myDataTable.Rows.Count > 0 Then
                        byteArray = DirectCast(myDataTable.Rows(0).Item("Template"), Byte())
                    Else
                        Throw New Exception("Unable to retrieve the specified template")
                    End If
            
            
                    Using mem As MemoryStream = New MemoryStream
                        mem.Write(byteArray, 0, CInt(byteArray.Length))
                        Using doc As WordprocessingDocument = WordprocessingDocument.Open(mem, True)
            
                        ??? How do I find specific word and replace it
                        
                       Helper.DownloadStream("Schedule" &" .docx", mem, Helper.WORD\_CONTENT\_TYPE)
                        End Using
                    End Using
            
            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            MSDN have plenty of examples: How to: Search and replace text in a document part (Open XML SDK)[^] Alternatively, DocX[^] looks significantly easier to use: Cathals Corner: DocX - A .NET library for manipulating Word 2007 files - String replacement[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups