Open template word doc and replace values
-
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?
-
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?
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
-
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
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
-
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
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