vb.net - Change version of word document programmatically? -
i dynamically generating word file, , clicking link opens file save dialog , says document a: microsoft word 97 - 2003 document.
what need programmatically generate 2007, 2010 word document.
code behind:
public sub page_load(byval sender object, byval e eventargs) handles me.load 'build content dynamic word document 'in html alongwith office specific style properties. dim strbody new system.text.stringbuilder("") strbody.append("<html " & _ "xmlns:o='urn:schemas-microsoft-com:office:office' " & _ "xmlns:w='urn:schemas-microsoft-com:office:word'" & _ "xmlns='http://www.w3.org/tr/rec-html40'>" & _ "<head><title></title>") 'the setting specifies document's view after downloaded print layout 'instead of default web layout. header & footer visible, 'this mode required. strbody.append("<!--[if gte mso 9]>" & _ "<xml>" & _ "<w:worddocument>" & _ "<w:view>print</w:view>" & _ "<w:zoom>90</w:zoom>" & _ "<w:donotoptimizeforbrowser/>" & _ "</w:worddocument>" & _ "</xml>" & _ "<![endif]-->") 'we can tweak msofooter class referenced footer, required strbody.append("<style>" & _ "<!-- /* style definitions */" & _ "p.msofooter, li.msofooter, div.msofooter" & _ "{margin:0in;" & _ "margin-bottom:.0001pt;" & _ "mso-pagination:widow-orphan;" & _ "tab-stops:center 3.0in right 6.0in;" & _ "font-size:12.0pt;}") 'word uses @page definition store document layout settings entire document. 'using @page sectionn, word applies page formatting individual html elements referenced 'through class attribute. 'mso-footer style attribute related footer 'refer topic "page layout , section breaks" & "headers , footers" in 'office xml & html reference detailed info. strbody.append("@page section1" & _ " {size:8.5in 11.0in; " & _ " margin:1.0in 1.25in 1.0in 1.25in ; " & _ " mso-header-margin:.5in; " & _ " mso-footer: f1;" & _ " mso-footer-margin:.5in; mso-paper-source:0;}" & _ " div.section1" & _ " {page:section1;}" & _ "-->" & _ "</style></head>") strbody.append("<body lang=en-us style='tab-interval:.5in'>" & _ "<div class=section1>" & _ "<h1>a dynamically generated document footer</h1>" & _ "<p style='color:red'><i> doc generated on " & _ datetime.now & "</i></p><hr>") 'we building big string here generated doc runs multiple pages. counter integer = 1 50 strbody.append("lorem ipsum dolor sit amet, consectetur adipisicing elit, " & _ "sed eiusmod tempor incididunt ut labore et dolore magna aliqua.") next strbody.append("</div>") 'word marks , stores information simple fields means of span element 'mso-field-code style. refer topic "fields" in office xml & html reference strbody.append("<div style='mso-element:footer' id=f1>" & _ " <p class=msofooter>" & _ " <span style='mso-tab-count:2'></span><span style='mso-field-code:"" page ""'></span>" & _ " </p></div>" & _ "</body></html>") 'force content downloaded 'as word document name of choice response.appendheader("content-type", "application/msword") response.appendheader("content-disposition", _ "attachment; filename=myword.doc") response.write(strbody) end sub
your document neither 97 - 2003 nor 2007 - 2010 document; it's word html document.
the save dialog says word 97 - 2003 document because you're sending incorrect extension.
changing extension (in content disposition header) .docx make save dialog word document, not make document more valid. (and make word more show warning)
you need generate openxml package, not word html.
can use microsoft's openxml sdk this.
note require complete rewrite of method.
Comments
Post a Comment