c# - Retrieving Building Blocks from user generated Word document -
i've searched far , wide, , haven't found proper solution yet.
first off, winforms application using .net 4.0 , devexpress.
i've been attempting retrieve building blocks (watermarks, cover pages etc) word documents (.docx), or @ least user generated template files (.dotx), user uploads application, saved database.
i must able retrieve building blocks used in file.
i have tried lot of different ways of retrieving them, can retrieve them built-in building blocks.dotx file, located in:
c:\users\<username>\appdata\roaming\microsoft\document building blocks\1033\14\built-in building blocks.dotx
i haven't figured out how extract them user generated file.
here work in progress code i've been using (many iterations can debug it):
private void savebuildingblock(string savedfile) { try { microsoft.office.interop.word.applicationclass wordapplication = null; wordapplication = new microsoft.office.interop.word.applicationclass(); microsoft.office.interop.word.document worddocument = wordapplication.documents.opennorepairdialog(savedfile); object missing = system.type.missing; database.toolbaritemswordinsertfiledata.filedata = filehandler.filebytelocked(savedfile); database.toolbaritemswordinsertfiledata.id = 0; database.toolbaritemswordinsertfiledata.toolbaritemid = toolbarid; database.toolbaritemswordinsertfiledata.save(); listbuildingblocks(wordapplication, wordpage); } catch (exception err) { logger.log(err); } }
next method:
private void listbuildingblocks(microsoft.office.interop.word.application wordapplication, wordpages wordpage) { microsoft.office.interop.word.wdbuildingblocktypes type = wordpage == wordpages.coverpage ? type = microsoft.office.interop.word.wdbuildingblocktypes.wdtypecoverpage : type = microsoft.office.interop.word.wdbuildingblocktypes.wdtypewatermarks; (int = 1; <= wordapplication.templates.count; i++) { try { if (wordapplication.templates[i] != null) { microsoft.office.interop.word.categories categories = wordapplication.templates[i].buildingblocktypes.item(type).categories; (int c = 1; c <= categories.count; c++) { try { //category cat = categories.application[0]; microsoft.office.interop.word.buildingblocks buildingblocks = wordapplication.templates[i].buildingblocktypes.item(type).categories.item(categories.item(c).name).buildingblocks; (int b = 1; b <= buildingblocks.count; b++) { try { microsoft.office.interop.word.buildingblock buildingblock = buildingblocks.item(b); if (buildingblock != null) { //saving database occurs here } } catch (exception err) { messagebox.show(err.message); } } } catch (exception err) { messagebox.show(err.message); } } } } catch (exception err) { messagebox.show(err.message); } } }
the wordpages parameter [wordpage] used in application specify if it's watermark or cover page (it's property in user control).
you need add current list of add-ins, can open template working building blocks. apart changing attached template , loading way, don't see other way of doing this:
dim app = globals.thisaddin.application 'load file containing building blocks app.addins.add("\\path file.dotx") 'get object loaded template dim template = app.templates(app.addins.count) 'go through each building block in file = 1 template.buildingblockentries.count dim bb = template.buildingblockentries.item(i) next
if need go down xml route, building blocks stored inside package in word\glossary\document.xml easier if doing server side wont need work word object model @ all, use open xml sdk, it's pretty useful things this!
Comments
Post a Comment