Replace Bookmars looses formatting in Word 2010

Hello
i'm replacing bookmark in a word 2010 file but it loses formatting (like indentation, numbering, lists etc)
Maybe i'm doing wrong in my code.
This is the code of function i use to replace bookmark :
public static void ReplaceBookmarkText(WordprocessingDocument doc, string bookmarkName, string replacementText)
XDocument xDoc = doc.MainDocumentPart.GetXDocument();
XElement bookmark = xDoc.Descendants(W.bookmarkStart).FirstOrDefault(d => (string)d.Attribute(W.name) == bookmarkName);
if (bookmark == null)
throw new BookmarkReplacerException("Document doesn't contain bookmark.");
if (bookmark.Parent.Name.Namespace == M.m)
throw new BookmarkReplacerException("Replacing text in math formulas is not supported.");
if (OpenXmlPowerTools.RevisionAccepter.HasTrackedRevisions(doc))
throw new BookmarkReplacerException("Replacing bookmark text in documents that have tracked revisions is not supported.");
if (xDoc.Descendants(W.sdt).Any())
throw new BookmarkReplacerException("Replacing bookmark text in documents that have content controls is not supported.");
XElement newRoot = (XElement)FlattenParagraphsTransform(xDoc.Root);
XElement startBookmarkElement = newRoot.Descendants(W.bookmarkStart).Where(d => (string)d.Attribute(W.name) == bookmarkName).FirstOrDefault();
int bookmarkId = (int)startBookmarkElement.Attribute(W.id);
XElement endBookmarkElement = newRoot.Descendants(W.bookmarkEnd).Where(d => (int)d.Attribute(W.id) == bookmarkId).FirstOrDefault();
if (startBookmarkElement.Ancestors(W.hyperlink).Any() || endBookmarkElement.Ancestors(W.hyperlink).Any())
throw new BookmarkReplacerException("Bookmark is within a hyperlink. Can't replace text.");
if (startBookmarkElement.Ancestors(W.fldSimple).Any() || endBookmarkElement.Ancestors(W.fldSimple).Any())
throw new BookmarkReplacerException("Bookmark is within a simple field. Can't replace text.");
if (startBookmarkElement.Ancestors(W.smartTag).Any() || endBookmarkElement.Ancestors(W.smartTag).Any())
throw new BookmarkReplacerException("Bookmark is within a smart tag. Can't replace text.");
if (startBookmarkElement.Parent != endBookmarkElement.Parent)
throw new BookmarkReplacerException("Bookmark start and end not at same levels. Can't replace text.");
XElement parentElement = startBookmarkElement.Parent;
var elementsBetweenBookmarks = startBookmarkElement.ElementsAfterSelf().TakeWhile(e => e != endBookmarkElement);
var newElements = parentElement.Elements().TakeWhile(e => e != startBookmarkElement)
.Concat(new[]
startBookmarkElement,
new XElement(BookmarkReplacerCustomNamespace + "Insert",
elementsBetweenBookmarks
.Where(e => e.Name == W.r)
.Take(1)
.Elements(W.rPr)
.FirstOrDefault()),
.Concat(elementsBetweenBookmarks.Where(e => e.Name != W.p &&
e.Name != W.r && e.Name != W.tbl))
.Concat(new[]
endBookmarkElement
.Concat(endBookmarkElement.ElementsAfterSelf());
parentElement.ReplaceNodes(newElements);
newRoot = (XElement)UnflattenParagraphsTransform(newRoot);
newRoot = (XElement)ReplaceInsertElement(newRoot, replacementText);
newRoot = (XElement)DemoteRunChildrenOfBodyTransform(newRoot);
xDoc.Elements().First().ReplaceWith(newRoot);
doc.MainDocumentPart.PutXDocument();

HI
I am also stuck here with the same formatting problem. Has anybody found any answer for the same?
Regards
Kumar Vikas Singh

Similar Messages

  • When opening .doc from website link, FF appends .docx extention to file & does not open in Word 2010, with incorrect file format error. Pls Help

    I'm using Firefox v12 to open a hyperlink from a website that points to a filename.doc MS Word Document for download.
    We are then asked to open the file with MS Word (Default), which our installed version is MS Word 2010.
    Firefox then renames the file appending a .docx to the filename e.g filename.doc.docx. MS Word 2010 opens with error 'incorrect file format', which is correct as Firefox has renamed it.
    Is there a fix or workaround for this?

    Hello Jscher2000,
    I can save the file and it opens fine.
    It's when I open it directly, Firefox appends the 'docx' extenstion to the filename.
    If I continue to reopen from the web link, Firefox renames the file as so
    mydoc.doc-1.docx
    mydoc.doc-2.docx
    mydoc.doc-3.docx
    I can open a .doc from a Microsoft site ok.
    I have the feeling it's the way the web server is reporting the mime type to Firefox.
    The link in question works fine in Internet Explorer.
    best regards,
    Youngielinux.

  • The "portable document format" icon is a Word 2010 icon, so selecting Adobe Reader as action does not work to display the PDF.

    In the Applications tab -- Content Type -- the "portable document format" icon is a Word 2010 icon (not Adobe), so selecting Adobe Reader or preview in Firefox as action does not work to display the PDF. I can see this Applications difference because my other Firefox laptop has the Adobe icon, and I can view PDFs. Essentially, a PDF is coming in as a Word document -- and the computer tries to open it up as a Word file.
    How do I get the correct PDF icon in the Applications list -- the same way it displays (and works) on my other laptop.

    Adobe PDF Plug-In For Firefox and Netscape 10.1.7 ?
    I think you need to add this to the list of applications and the action to take on the type of content. This can also be managed in the about:permissions page. [[Permissions Manager - Give certain websites the ability to store passwords, set cookies and more]]

  • Word 2010 Send As Attachment does not send a HTML formatted mail

    Hi!
    I'm using Word 2010 to send a document by clicking File->Save & Send->Send Using E-mail->Send as Attachment.
    In the Outlook 2010  new message window I click the ribbon tab Format Text and then I select HTML as the message format.
    I continue to happily compose the HTML body and send it. On the recipient side (also Outlook 2010) the message body comes in Plain text format even tough the source email in the Sent folder is in HTML format.
    Is this a bug or am I missing something obvious?

    Hi,
    It's not a bug as I can tell, the recipient may have configured Outlook to Read all standard mail in plain text, please let the recipient check the following setting:
    Start Outlook 2010.
    Click the File tab in the Ribbon, and then click Options on the menu.
    Click Trust Center on the Options menu.
    Click the Trust Center Settings tab.
    Click E-mail Security.
    Under Read as Plain Text, check if the check box of Read all standard mail in plain text has been selected, untick it and click
    OK to save the setting.
    Send the email again to test, hopefully the recipient can receive the email as the original format.
    Good luck.
    Regards,
    Melon Chen
    TechNet Community Support

  • Word 2010 - I can only paste as plain text. Paste with source formatting is mising.

    I am using Word 2010 (version 14.0.7143.5000).
    I started writing a document with different formatting/styles.  If I copy formatted text from this document and try to do a paste special, I only have the option to paste as un-formatted plain text.  My different paste options are gone.  This
    used to work.
    I have performed Windows Update patches recently.  I do not have Skype or any Skype plug-ins installed that I am aware of.  I tried running Word in safe mode and without any web browsers open.
    Any ideas?

    I eventually figured out the problem.  The problem was not in Word or a setting within Word.  The problem was that I had other programs open that were modifying the data in the clipboard, specifically Windows Remote Desktop and Tight VNC viewer.
     They were converting the data in the clipboard to plain text without my knowledge.  So once I closed VNC and change Remote Desktop not to share the clipboard between machines, the data in the clipboard kept its formatting and the different Paste
    options in Word returned.  Hopefully this information will help others with this problem.

  • PDF conversion from Word 2010 incredibly slow and loses format

    Just moved onto new laptop with Windows 7 Word 2010
    and downloaded Acrobat 9 Pro trial.  Have used Acrobat 8 with Word 2007 and earlier versions with no problems. Now taking half an hour to convert a document and cutting out margins.  Just today I was talking with Adobe tech help and was advised Acrobat 9 is compatible with Word 10.  Any suggestions?

    You should be able to print to the Adobe PDF printer, but nothing else probably will work with OFFICE 2010 as Adobe told you. You probably can just wait until AA10. There are no current expectations of AA9 being updated to work with OFFICE 2010 (at least the PDF Maker part.).

  • Converting PDF formats to MS word 2010?

    hi,I've got a problem. I'm really sorry if someone have already discussed that problem before.
    I am trying to convert 1000's of pdf files to word 2010 that has not been labeled correctly so using the built in feature that acrobat has does not allow the tables and colums to convert without going all over the place. Is there anyway to do this?
    Hope to get answers as soon as possible.

    You'll want to ask this question in the Acrobat Windows forum.
    http://forums.adobe.com/community/acrobat/acrobat_windows
    Be well...

  • Adding a merge field to an email hyperlink in a word 2010 email mailmerge

    I am using word 2010, excel 2010, and outlook 2010 to send out the following email with email mailmerge to 50 + people.
    The highlighted names in the greeting are two merge fields - First_Name and Last_Name.
    The hyperlink is an email hyperlink to the following email message:
    Will you attend?   ___ Yes   ___  No
    The hyperlink is as follows:
    I would like to place the merge First_Name Last_Name in the hyperlink to sign the RSVP email back to me. I have tried many modifications of the hyperlink code and nothing works consistently. It will place one name as the signer but will not pick up
    the following names the merge fields.
    I would like the final email hyperlink to look like this:
    Will you attend?   ___ Yes   ___  No
    Jody Smith
    Can anyone tell me how to add the First_Name Last_Name to the hyperlink email so that each ricipient's name from the original email will appear in the hyperlink email as shown
    above?
    Thanks!
    Ben

    Hi All,
    To help other community members who may have similar problem, I quote Peter Jamieson's answer here:
    (Edited a little...)
    I can get this to work here, but there are some things you need to pay attention to when merging to HTML email (rather than to new documents etc.).
    The Hyperlink field code I start with is as follows:
    { HYPERLINK }
    Insert that using ctrl-F9 to insert the special field brace pair { }, insert the Word HYPERLINK, then select the field and update it using F9.
    View the field code results (press Alt-F9 if necessary). In the English language version of Word you should see 
    Error! Hyperlink reference not valid.
    Important point number 1:
    Do not update the result of this HYPERLINK field again (i.e. don't select it and press F9). If you do, every email will always show the same link text. As
    far as I can remember, this behaviour is different when you merge to a document rather than email, or perhaps merge to attachments. This can be a problem if you need to update other fields in your email, or if the email is being used by other people who might
    (for whatever reason) update the HYPERLINK field
    Now modify the display text.
    Use the Arrow keys to move the insertion point so it is between the "E" and the "r" in the Word "Error", and type
    Click here to RSVP!
    Then you can select and delete the "E", and the "rror! Hyperlink reference not valid."
    Select the text and apply the Hyperlink style to it (Home tab of the Ribbon->Styles).
    When you toggle field code display using alt-F9, you should see the original field
    { HYPERLINK }
    Now click after the K of HYPERLINK and insert the following fields and text (or copy them from somewhere else):
     "{ SET x 1 }mailto:*** Email address is removed for privacy ***?subject=July%20Wine%20and%20Spine%20&body=%20Will%20you%20attend?%20%20%20___%20Yes%20%20%20___%20No%0A%20%0a%2{
    MERGEFIELD First_Name%20{MERGEFIELD Last_Name }"
    so that your field now looks like
    { HYPERLINK "{ SET x 1 }mailto:*** Email address is removed for privacy ***?subject=July%20Wine%20and%20Spine%20&body=%20Will%20you%20attend?%20%20%20___%20Yes%20%20%20___%20No%0A%20%0a%2{
    MERGEFIELD First_Name%20{MERGEFIELD Last_Name }" }
    As usual, all the {} need to be the special field code brace pairs that you can enter using ctrl-F9 on WIndows Word.
    At this point, experienced field users will have to resist the temptation to select the fields and update them. Dont do it!
    Save the document.
    Test the email merge. At this point, I would hope to see the results you want.
    If not, I do not know why this works OK on my system but not yours.
    Important point number 2 You may be wondering why
    there is a { SET x 1 } field inside the HYPERLINK code. It's because when you are working with a .docx, if you save, close and reopen your document, you may find that Word has resolved all the merge fields nested inside your HYPERLINK field (i.e. replaced
    the field codes by their results). This behaviour can appear arbitrary, and the reason that (for some reason), Word does not resolve the nested fields if there is a Word bookmark inside the HYPERLINK field. Now if the insertion point or selection ws inside
    the HYPERLINK field when you save/close the document, there will be a bookmark in there, because Word inserts a bookmark called _GoBack, which enables the user to go back to the previous selection. But if you click outside the HYPERLINK field before you save,
    there wil be no such bookmark and Word will resolve your field codes. So we put the { SET x 1 } in there to ensure that there is a bookmark (in this case, called X) to prevent that from happening.
    Alternatively, if you save as .doc format (not recommended these days if you can avoid it) Word does not resolve these fields and you don't need the { SET } field.
    And that's it. Whether that will be enough for your Merge to work as you hope, I cannot be sure, but all I can say is that without doing those 2 things, it almost certainly
    won't.
    [[I originally had a third thing. I actually now think it's incorrect, but I'm going to leave it here just in case.
    Important point number 3: If it does not work, there is
    at least one other thing that could be wrong, although I think it is highly unlikely. There is a Windows registry setting called FieldCalcSecurityLevel, which was introduced about 12 years ago when Microsoft made some security-related changes to the behaviour
    of certain types of field. I'm not going to step through how to look for that, but for example, in my registry, for Word 2010, it is at
    HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Word\Options
    It may be elsewhere.
    If you cannot find that Option, or it is there and is set to the default value (1), then something else is wrong and I do not know what it is. However, if it is set to 0 (which
    makes Word use its original behaviour, you will have to set it to 1 to get this to work.
    Thanks for your understanding.
    Steve Fan
    TechNet Community Support

  • Can I be guaranteed Acrobat XI will convert Word 2010 accurately for print?

    Can I be guaranteed that Acrobat XI will convert a Microsoft Word 2010 documents which contain many tables 100% accurately and to a press quality "print ready" pdf without loosing any formatting etc?

    One aspect you may have to be careful about is the attached printer when you are editing the DOC file. Depending on a switch in WORD (in some versions of WORD it was turned off, but most have it checked), the formatting depends on the printer attached in the print menu (default system printer unless changed). WORD will reflow the document when you change printers or use PDF Maker if the printer was not the Adobe PDF printer. So ALWAYS edit with the Adobe PDF printer attached if you expect to get reliable results.
    This happens with most word processors (they are not layout packages and generally reflow to best print on the attached printer). The dependence on the printer has long been a major issue of DOC forms that end up printing differently than they were designed. (OK, a bit long winded.)

  • Can not open Word 2010 docx in Word 2013 from Outlook 2013 mail

    Hello,
    I got a mail on outlook with a word 2010 attachment in docx format.
    Open this attachment with double click will show only the welcome screen with the text Open in protected mode.
    With process monitor I could that word 2013 in an endless loop and tries to create the registry key
    "HKCU\Software\Microsoft\Office\15.0\Word\Options\Vprsu" in an loop.
    The result is always ACCESS DENIED.
    When I create this key manually there will be an loop to create the registry key
    "HKCU\Software\Policies\Microsoft\office\15.0\word\options\Assist".
    The result is always ACCESS DENIED.
    When I create this key manually. Word show the attachment immediatelly.
    Closing Word and open the attachment again will delete this registry key and the cycle starts again.
    Is there a hotfix available that will repair this?
    I think this is an error in word 2013 protected view.
    Disabling the protected view will help, but is not an good solution.
    Is there any other workarround?
    Screenshot from process monitor:
    Best Regards,
    Dieter

    This issue may occur if the default file associations for Word are damaged. You may repair Office 2013 to restore the file associations and check if that helps. Refer to the link below for more information. You may restart
    the computer once the repair is done.
    http://office.microsoft.com/en-us/project-help/repair-or-remove-office-HA010357402.aspx
    Note: Close all Office applications before you perform the repair.

  • Can not edit data labels in old graph chart objects using Word 2010

    In Word 2010, Insert object "Microsoft Graph Chart", go to Chart Options and check an item in Data labels, such as "Series Name", "Category Name" or "Value". Click OK, then double-click on a data label to edit its
    settings, or right-click on a data label and select "Format Data Labels..."
    This makes Word to crash. The only way I can get out of it is by using the Task Manager to kill Word.
    This happens in Windows 7 and Windows 8. This does NOT not happen in Word 2007 nor Word 2013, so it seems specific to Word 2010. All our clients have the same problem, this does not seem related to a specific workstation configuration.
    The Microsoft Graph Chart are the old charts used within .doc files. I have an application still generating those types of files and need to edit data label settings manually before processing the files with my application.
    Also note that anytime you edit a .doc file in Word 2010 and you see "Compatibility Mode" in the title bar, the Insert / Chart option from the ribbon inserts an old type chart (same as if you use Insert / Object / Microsoft Graph Chart), not the
    new Word 2010 charts. You then have the same problem editing the data labels.
    The underlying Activex seems to be the Chart.Exe file, the one I have installed is 14.0.7012.1000. You can see the version by double-clicking the chart and then look in the Help / About menu.
    Any help will be greatly appreciated.

    As this is a configuration issue and not a programming issue I' moving your question to a more appropriate forum where you're more likely to find assistance.
    Cindy Meister, VSTO/Word MVP,
    my blog

  • Can't Place a specific Word 2010 docx into InDesign CS4

    I'm trying to use the Place command to bring the text from a specific Word 2010 docx into InDesign CS4.
    I've successfully brought in several Word 2010 documents (docx extention) to InDesign CS4 documents using the place command.
    I have one specific 30 page document (which is no loarger than others which have been successful), and when I use the place command with this file, nothing happens (and no text gets loaded onto the cursor for placement.
    I've tried re-saving the word doc, even pasted its contents into a new Word doc and still can't get the Place command to work.
    THere's nothing particularly unique about the file - doesn't have any odd embedded content, just a 30 page text document writtin in word.
    Any ideas what could be going wrong or how to fix it?
    I've tried restarting both Word and InDesign without any change (and the rest of my word files can be Placed without problem, so I imagine it's something specific to that file.
    NOTE - re-saving in RTF format lets the Place command work but leaves a bunch of empty squares/boxes (not sure if these were for tabs or empty spaces or what really??)
    Can work around this for now, but would like to know how to get the place command working with decent size Word Docs( docx for word 2010 if that matters).
    Thanks for any help.

    It turned out that for at least one particular document the OP had used some blank paragraphs as spacers at the start of the text, and that the style assigned to the second paragraph used a combination of left and right paragraph indents that was more than the width of the document page, sending the entire document (except for the empty first paragraph) into overset. I can't say for sure, but I think the Word styles might have been mapped to ID styles because I found no trace of the indents in the Word file itself, only in the ID document, and I had no trouble placing that Word file in my own ID test document. I did have some trouble placing another .docx file supplied by the OP in CS4, but not in later versions, and conversion to .doc solved that problem.

  • A problem with VBA in MS Word 2010

    Hello
    I hope you can help me. I am working on developing a template in MS Word 2010. My problem is to I want to when I save a new document using the template file automatically provides stored in the path
    C: \ Users \ Stephen Christiansen \ OneDrive \ first Applications
    What is VBA code to do this.
    Sincerly 
    Steffen Christiansen

    Hi,
    Try something like this:
    Sub SaveMyDocument()
    Activedocument.SaveAs2 "C:\Users\Stephen Christiansen\OneDrive\first Applications\test.docx"
    End Sub
    You replace the filename with your own filename of course.
    Maurice
    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer. Thank You

  • Labview 2013 report generation toolkit word 2010 insert table problem

    Hi,
    I am currently evaluating LV2013 with respect to the report generation toolkit for use with word 2010. My Vi/program won't insert the correct table and gives me an error message. see below:-
    Exception occured in Microsoft Word: The requested member of the collection does not exist.Help Path is wdmain11.chm and context 25421 in NI_ReportGenerationToolkit.lvlib:Word_Insert_Table​.vi->NI_Word.lvclass:Append Text Table to Report.vi->Word Table save.vi
    I have since run the following word example.vi provided by NI within LV2013 office examples:- Growable table.vi. this gives me the same error!
    Exception occured in Microsoft Word: The requested member of the collection does not exist.Help Path is wdmain11.chm and context 25421 in NI_ReportGenerationToolkit.lvlib:Word_Insert_Table​.vi->NI_Word.lvclass:Append Text Table to Report.vi->Growable table.vi
    It would seem that regardless of my own Vi there is a fundamental problem in the office example vi.
    I have found examples of a similar problem in LV2011/2012 but the work around code doesn't seem to match the LV2013 vi's code for a direct replacement.
    Can anyone help/shed some light on this? 
    Thanks in advance,
    Dan.

    Because the upgrade of Word from 2007 to 2010 I needed to upgrade an existing application from LabVIEW 2009 to 2013. This didn't work because of the insert table error. I discovered that the problem is the vi called "Word_Insert_Table.vi" located in the ..Program Files (x86)\National Instruments\LabVIEW 2013\vi.lib\addons\_office\_wordsub.llb. There is a "Cut" command implemented, this command removes the created cell instead of clearing it. Therefore the table will be filled half and then returnes with an error. I have removed this command. I have posted the changed vi (LV 2013).
    Attachments:
    Word_Insert_Table.vi ‏30 KB

  • Word 2010 with Embedded Excel table becomes oversized for RDP users

    I originally asked this question on the SharePoint Online forums as this was the first time we've seen this issue. Original Post
    Summarized: we have a Word template with embedded Excel table created in SharePoint Online, and when a RDP user opens it, the document correctly opens in the local version of Word 2010 (not Word Online).   When the table is edited, it becomes
    larger to the point you can no longer view the entire table. 
    We have done some testing with Word 2010 outside of RDP and everything works correctly, the embedded Excel table doesn't become oversized and works as expected.  It seems to definitely be an issue with RDP and I've been looking for any assistance
    on resolving this issue.  I recently did some further testing and found that the only time the table becomes oversized and not view-able is when you copy and paste information from an existing 2003 Excel document.  I would think this is the issue,
    but it doesn't happen when working with Word and SharePoint outside of an RDP environment. 
    The template is a .docx file. All RDP users are in a 2008 R2 environment.

    I did some testing over the weekend and the simple answer to your question - Not always.
    I found 2 variables which seem to cause this to happen:
    1) We have 2 TS servers - one with IE 9, which remains to maintain support for a legacy app which will not work with IE 10 or 11, and one with IE 10.
    2) Screen resolution of the client computer - it seemed that systems with video configurations which could not exceed the maximum of 1280x1024 (the same screen resolution as our TS servers) didn't experience the issue.  There were some laptops
    and higher end systems with widescreen monitors, where this was an issue.  They typically didn't have a 1280x1024 option; yet even if they did they would experience the same issue, just not as pronounced.
    It also seems to only occur when a user copies and pastes information from Excel 2010 into the embedded Excel table in Word 2010.  Again, only within RDP.  On those client computers which had the problem, I could perform the same function outside
    of RDP without any problems.  However, this is not an ideal format for the chefs as they will frequently be moving between different systems during the day.

Maybe you are looking for

  • How to retrieve  Del. No. using MBLNR

    Hi Guyz.. One quick Qn ! I have MBLNR in my I_MSEG, now I want to retrieve Del. doc. no. using MBLNR. Now I tried to select VBELV as Del doc. using VBELN as subsequent doc. from  VBFA table. I am using FOR ALL ENTRIES in ITAB, The prob is the select

  • Where to download com.ms.security.PolicyEngine package for Internet Explore

    Hi, Where can i download com.ms.security.PolicyEngine package for Internet Explorer I need this to execute my applet.pls give information abt netscape security for executing applet Thanks naveen

  • OCCI Error -  ORA-32102: invalid OCI handle

    Hi, I'm completely new to OCCI and Linux and trying to run the simple OCCI program given in oracle site (http://www.oracle.com/technology/tech/oci/occi/occibasic.html) to connect a remote Oracle DB server. #include <iostream> #include <occi.h> using

  • Not able to See Application Component

    Hi, I logged into the BI 7.0 system. In the RSA1 transaction under the Data Sources option am not able to see Application Component folder is not getting displayed. Is there any configuration or settings are required for getting displayed of Applicat

  • Why cant i do my do my updates on my iphone 4 and ipad 2

    why cant i do my do my updates on my iphone 4 and ipad 2