none
retain formating of Word bookmarked text RRS feed

  • Question

  • Hi, I'm using vbs to copy the text from a source Word 2010 document into another Word 2010 document. The following code works fine BUT the copied text has lost the formatting (bold, underline, italic, etc.) of the original. How can it be retained?

    Const End_of_Story = 6
    Const Move_Selection = 0

    Set objWord1 = CreateObject("Word.Application")
    Set objDoc = objWord1.Documents.Open("C:Temp\source.docx")

    Set Life7 = objDoc.Bookmarks("Life7").Range
    Set Life8 = objDoc.Bookmarks("Life8").Range

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True

    Set objDoc = objWord.Documents.Add()
    Set objSelection = objWord.Selection
    objSelection.EndKey End_of_Story, Move_Selection

    objSelection.TypeText "" & Life7.Text
    objSelection.TypeParagraph()
    objSelection.TypeParagraph()

    Set objSelection = objWord.Selection
    objSelection.EndKey End_of_Story, Move_Selection

    objSelection.TypeText "" & Life8.Text
    objSelection.TypeParagraph()
    objSelection.TypeParagraph()

    objWord1.Quit
    objDoc.SaveAs("C:\Temp\output(test).docx")

    Many, many thanks...


    • Moved by Bill_Stewart Tuesday, February 18, 2014 10:28 PM Moving to more appropriate forum
    • Edited by Brian658 Tuesday, February 18, 2014 11:04 PM
    Tuesday, February 18, 2014 10:24 PM

Answers

  • Essentially, what you're doing is inserting the bookmarked range into another document as a text string (via .TypeText). To retain the formatting, you need to copy & paste. For example:

    Set objWord = CreateObject("Word.Application")
    Set objDocSrc = objWord.Documents.Open("C:Temp\source.docx")
    Set objDocTgt = objWord.Documents.Add()
    objDocSrc.Bookmarks("Life7").Range.Copy
    objDocTgt.Range.Characters.Last.PasteAndFormat 16 '16 = wdFormatOriginalFormatting
    objDocSrc.Bookmarks("Life8").Range.Copy
    objDocTgt.Range.Characters.Last.PasteAndFormat 16 '16 = wdFormatOriginalFormatting
    objDoc.SaveAs ("C:\Temp\output(test).docx")
    objWord.Visible = True


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by macropodMVP Wednesday, February 19, 2014 10:53 PM
    • Marked as answer by Brian658 Wednesday, February 19, 2014 11:19 PM
    Wednesday, February 19, 2014 1:53 AM
  • Hi Brian

    I prefer using

      TargetRange.FormattedText = SourceRange.FormattedText

    rather than copy/paste.

    Also, I recommend that you not use CreateObject twice in your code. There's no need to have a second instance? Declare a second object for the new document (Set newDoc = objWord1.Documents.Add). You can get a target Range object for the end of this document in the one instance something like this:

      Dim rngTarget as Word.Range
      Set rngTarget = newDoc.Content
      rngTarget = rngTarget.Collapse wdCollapseEnd
      rngTarget.FormattedText = Life7.FormattedText


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Brian658 Wednesday, February 19, 2014 11:19 PM
    Wednesday, February 19, 2014 6:28 AM
    Moderator
  • Fantastic Paul, worked first time.  Thanks again, Brian

    • Marked as answer by Brian658 Wednesday, February 19, 2014 10:53 PM
    Wednesday, February 19, 2014 10:52 PM

All replies

  • Essentially, what you're doing is inserting the bookmarked range into another document as a text string (via .TypeText). To retain the formatting, you need to copy & paste. For example:

    Set objWord = CreateObject("Word.Application")
    Set objDocSrc = objWord.Documents.Open("C:Temp\source.docx")
    Set objDocTgt = objWord.Documents.Add()
    objDocSrc.Bookmarks("Life7").Range.Copy
    objDocTgt.Range.Characters.Last.PasteAndFormat 16 '16 = wdFormatOriginalFormatting
    objDocSrc.Bookmarks("Life8").Range.Copy
    objDocTgt.Range.Characters.Last.PasteAndFormat 16 '16 = wdFormatOriginalFormatting
    objDoc.SaveAs ("C:\Temp\output(test).docx")
    objWord.Visible = True


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by macropodMVP Wednesday, February 19, 2014 10:53 PM
    • Marked as answer by Brian658 Wednesday, February 19, 2014 11:19 PM
    Wednesday, February 19, 2014 1:53 AM
  • Hi Brian

    I prefer using

      TargetRange.FormattedText = SourceRange.FormattedText

    rather than copy/paste.

    Also, I recommend that you not use CreateObject twice in your code. There's no need to have a second instance? Declare a second object for the new document (Set newDoc = objWord1.Documents.Add). You can get a target Range object for the end of this document in the one instance something like this:

      Dim rngTarget as Word.Range
      Set rngTarget = newDoc.Content
      rngTarget = rngTarget.Collapse wdCollapseEnd
      rngTarget.FormattedText = Life7.FormattedText


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Brian658 Wednesday, February 19, 2014 11:19 PM
    Wednesday, February 19, 2014 6:28 AM
    Moderator
  • Fantastic Paul, worked first time.  Thanks again, Brian

    • Marked as answer by Brian658 Wednesday, February 19, 2014 10:53 PM
    Wednesday, February 19, 2014 10:52 PM
  • Many thanks Cindy. The copy/past method suits my particular needs this time. I'll keep your method in reserve though and thank you for pointing out the duplicated CreateObject. I've rectified this. I'm now a happy bunny...
    Wednesday, February 19, 2014 10:56 PM