none
Using Selection.FormattedText and preserve the original document formatting RRS feed

  • Question

  • Hi,

    I am using VB.NET, and wanting to copy a source documents whole story into a target document while preserving the source documents formatting.

    I don't want to use copy, paste as PreserveOriginalFormat works, but use the selection.FormattedText to avoid using the clipboard.

    It appears that the source document that I am copying into the target document is inheriting the targets formatting settings.

    IS there a way around this. The source document has various object types.

    Regards

    Thursday, August 24, 2017 8:29 AM

All replies

  • Hi KG Inoz,

    What code do you use? I used below code and it works to copy from source document and preserve it’s formatting.  

            Dim SourcePath As String
    
            Dim sourceDoc As Word.Document
    
            Dim currentRng As Word.Range
    
            Dim wdApp As Word.Application
    
            SourcePath = "C:\Users\v-guaxu\Desktop\Source.docx"
    
            wdApp = Globals.ThisAddIn.Application
    
            currentRng = wdApp.Selection.Range
    
            sourceDoc = wdApp.Documents.Open(SourcePath)
    
            currentRng.FormattedText = sourceDoc.Range

    This is source document


    This is target document and current selection location


    Here is the result


    Best Regards,

    Terry

    Friday, August 25, 2017 10:00 AM
  • Thanks Terry,

    This is fine for objects, but if text is involved such as heading styles, the heading styles on the source document get changed to the targets heading style when copied.

    Monday, August 28, 2017 12:48 AM
  • Hi KG Inoz,
    Could you please share some screenshots(sourceFile, targetFile,result) to explain how source docuemnt get changed? You could share screenshots via One Drive and put link here.
    Besides, you could also  try to use InsertFile to insert a document to another doucment.
    Here is the example.
     currentRng = wdApp.Selection.Range
     currentRng.InsertFile(SourcePath)
    Best Regards,
    Terry
    Wednesday, August 30, 2017 7:32 AM
  • Hi Terry,

    Here is a link for you to look at that has four documents in it SourceDocuments

    The TAS_ProjectTemplate.dotx is the basis of the final Detailed target test pdf.

    WA_SWMS-003 when imported "copied" using formattedText gets changed from the source to how it appears in the target pdf file (page 52 to 58). AS you can see the SWMS-003 heading style has changed from the source file to that of the target files style, and also the arrows (wingdings characters) have been corrupted some how.

    Likewise with WA_SWI-010. the heading style and positioning of "SWI-010" is different from source to target.

    I can't use the insert file methodology, as I need to open each source file and populate bookmarks with data before copying into the target document, but if there is a way of doing that I am open to suggestions.

    There are many source documents being used in order to create the target document and subsequent pdf document.

    Regards

    Wednesday, August 30, 2017 11:37 PM
  • Hi KG Inoz,

    ->AS you can see the SWMS-003 heading style has changed from the source file to that of the target files style, and also the arrows (wingdings characters) have been corrupted some how.

    In my test, heading style does not changed and arrow has not been corrupted. 

    What's your current code? Why do you refuse to use copy and paste as PreserveOriginalFormat?

    Best Regards,

    Terry

    Tuesday, September 5, 2017 2:57 AM
  • Hi Terry,

    I used copy and paste with the PreserveOriginalFormat which works fine, but that clobbered the clipboard for other application usage. Not preferred method.

    Heres part of the code in VB.NET.

    wDoc.Bookmarks("dgDocHome").Select()
    
                Dim bAddSectionBreak As Boolean
                If wDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape Then
                    ' Here we can set a flag to notify of pending page orientation change required
                    bAddSectionBreak = True
                End If
    
                ' Select the Document to Copy
                wDoc.Application.Selection.WholeStory()
    
                Dim oSourceRange As Word.Range
    
                oSourceRange = wDoc.Application.Selection.FormattedText
    
                ' Select the Active Document 
                oBaseDoc.Select()
    
                ' Create a Home Bookmark to return to
                iBMCount += 1
                oBaseDoc.Bookmarks.Add(String.Format("dgDocHome{0}", CStr(iBMCount)))
    
                WordApp.Selection.EndKey(Word.WdUnits.wdStory)
                WordApp.Selection.EndKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove)
                If bAddSectionBreak Then
                    WordApp.Selection.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
                    Dim tr As Word.Range
                    tr = WordApp.Selection.Range
    
                    tr.FormattedText = oSourceRange.FormattedText
                    SetPageOrientation(WordApp, Word.WdOrientation.wdOrientLandscape)
                    Dim HdFt As Word.HeaderFooter
                    With WordApp.ActiveDocument.Sections.Last
                        For Each HdFt In .Headers
                            HdFt.LinkToPrevious = False
                        Next
                        For Each HdFt In .Footers
                            HdFt.LinkToPrevious = False
                        Next
                    End With
                Else
                    Dim tr As Word.Range
                    tr = WordApp.Selection.Range
    
                    tr.FormattedText = oSourceRange.FormattedText
                End If
    
                oBaseDoc.Bookmarks(String.Format("dgDocHome{0}", CStr(iBMCount))).Select()
    
                ' Seek the Main Document
                WordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument
    
                WordApp.Selection.EndKey(Word.WdUnits.wdStory)
                WordApp.Selection.EndKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove)
    
                oBaseDoc.Bookmarks(String.Format("dgDocHome{0}", CStr(iBMCount))).Delete()
    
                ' Specific document changes
                If oFile.ToString.Contains("3_6Form02") Then
                    With WordApp.Selection.PageSetup
                        .LeftMargin = WordApp.CentimetersToPoints(1.9)
                        .RightMargin = WordApp.CentimetersToPoints(1.95)
                    End With
    
                    ' Seek the Main Document
                    WordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument
                    WordApp.Selection.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
    
                    With WordApp.Selection.PageSetup
                        .LeftMargin = WordApp.CentimetersToPoints(2.54)
                        .RightMargin = WordApp.CentimetersToPoints(2.54)
                    End With
                Else
                    ' Seek the Main Document
                    WordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument
                    WordApp.Selection.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
                End If
    
                If WordApp.Selection.PageSetup.Orientation <> Word.WdOrientation.wdOrientPortrait Then
                    SetPageOrientation(WordApp, Word.WdOrientation.wdOrientPortrait)
                    Dim HdFt As Word.HeaderFooter
                    With WordApp.ActiveDocument.Sections.Last
                        For Each HdFt In .Headers
                            HdFt.LinkToPrevious = False
                        Next
                        For Each HdFt In .Footers
                            HdFt.LinkToPrevious = False
                        Next
                    End With
                End If
                wDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
                wDoc = Nothing

    Tuesday, September 5, 2017 3:14 AM
  • Hi KG,

    I checked the downloaded link, do you mean you want to copy WA_SWMS-003.Duct Erection.docx or WA_SWI-010.Use of Equipment Lifter.docx to TAS_ProjectTemplate.dotx, and you expected result is in WA_20180830_Detailed_ver1.pdf (page 52)? Or, do you mean WA_SWMS-003.Duct Erection.docx and WA_SWI-010.Use of Equipment Lifter.docx are the current result which did not meet your requirement?

    Do you want to copy the content to TAS_ProjectTemplate.dotx or create a new document which combines the header from TAS_ProjectTemplate.dotx and content from other documents? If it is later, there is no headers from TAS_ProjectTemplate in WA_SWI-010.

    Based on your code, it seems you modify the result document parts like PageSetup, books to achieve your expected result.

    Could you share us your demo project with source document and expected document through OneDrive?

    If you want to combine the header and content from different documents, would you mind make a copy of the content document and then add header to this document?

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 7, 2017 8:25 AM