none
Header format changing (style not applied\copied from source) on copy and paste

All replies

  • There is more than one method described in the link, which did you try?

    Also, you need to make sure that the Style name in the source document exists in the target document (i.e. just because something looks like a Heading1 doesn't mean it's not actually using a Style named Heading1A); otherwise you are liable to end up with a new Style in the target document.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, November 15, 2013 12:02 PM
  • we are using following code and style seems there but its setting are different. 

    how can we copy\override style from source to destination document?

     Dim wrdMergedDoc As Word.Document = wrdApp.Documents.Add()
    
                                wrdDoc = wrdApp.Documents.Open(docPath, ReadOnly:=True)
                                                          wrdDoc.Activate()
                                wrdApp.Selection.WholeStory()
                                wrdApp.Selection.Copy()
                                
                                wrdMergedDoc.Activate()
                                
                                wrdApp.ActiveDocument.UpdateStylesOnOpen = False
                                wrdApp.Selection.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting)

    Monday, November 18, 2013 4:32 AM
  • I'm not sure which Styles you want to apply - those in the source document, or those in the destination document. To apply the Styles of the destination document, use:

    wrdApp.Selection.PasteAndFormat(Word.WdRecoveryType.wdUseDestinationStylesRecovery)

    Even so, if you paste content formatted in a Style that does not already exist in the destination document, the source document's formatting may be preserved.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, November 18, 2013 9:46 AM
  • Hello Paul,

    How can i add documents in forum. I want to send to you document i am trying  and its result. Below is the image of both documents, above is the text that i am trying to copy and below it is the resulting document text after copy.

    see, there is space between lines.

    Monday, November 18, 2013 10:12 AM
  • Yes, I can see the spaces,  but not the cause. You could upload both documents (delete anything sensitive) to a file hosting site (e.g. http://www.4shared.com/) from where I can download it, then post a link here.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, November 18, 2013 11:10 AM
  • Below are the links of both documents. First link is of the source document that we want to copy. Second is the link of result we are getting after the code execute that we provide in above post. When we do copy/past manually (using ctrl + c/v) then format persist. But from code, format changed (happening only for header /footer).

    Source Doc :

    http://www.4shared.com/file/LVHmBsFP/source.html

    Result Doc : 

    http://www.4shared.com/file/Gn_E92ZX/result.html

    Thanks

    Dalbir Singh

    Tuesday, November 19, 2013 3:03 AM
  • It appears Word is not respecting its own settings for copying/pasting Styles that conflict. Even when I change the settings via File|Options|Advanced>Cut, Copy & Paste, the source formats are not respected. I suspect a bug.

    You say "When we do copy/past manually (using ctrl + c/v) then format persist", but I can't even get Word to do that properly (i.e. using Word's option to retain source formatting, though it works OK via Paste Special).


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Wednesday, November 20, 2013 12:52 AM
    Tuesday, November 19, 2013 9:28 AM
  • What should we do\try now? Are you reporting it to Microsoft?

    Yes, in my case (ctrl + c/v ) is working.I did Ctrl +a and then Ctrl +c and Ctrl+v in new document.  If we modify the default styles setting then should it work? How can we modify settings of default (normal) style? I will try these things and will let you know.

    Please let me know if you found solution or any other info for this.

    Thanks

    Dalbir Singh

    Tuesday, November 19, 2013 10:12 AM
  • You can report the problem by posting a suggestion at:
    http://mymfe.microsoft.com/Office/feedback.aspx?formID=375
    and/or:
    https://feedback.office.microsoft.com/default.aspx?scrx=1


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, November 19, 2013 10:19 AM
  • After re-starting my system, I find the following macro works correctly when run from your source document (the results document is in my Downloads folder):

    Sub Test()
    Dim wdDoc As Document
    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Copy
    Set wdDoc = Documents.Open("C:\Users\" & Environ("Username") & "\Downloads\result.docx")
    wdDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.PasteAndFormat (wdFormatOriginalFormatting)
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, November 20, 2013 12:57 AM
  • Yes ,  i see it is working using macro/vba in source document. But we can not loop through each section to copy it to new document. Because in our case there can be lot of pages and many documents that we want to copy to new documents. And we don't want to skip\left anything behind that don't copy to new document.

    Do you know the way to send command (ctrl+c) to document?

    Wednesday, November 20, 2013 6:25 AM
  • The macro doesn't have to be in the source document or limited to working with just one Section or its header. All of that was just for demonstration purposes, in line with the sample documents you provided. The same approach works equally well for copying entire documents. Of course, if you have headers & footers to copy, you will still need to work through the headers & footers for the last Section in each source document.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, November 20, 2013 6:53 AM
  • Can you please explain here more? How can we access every section of source document? And if any section does not exist in destination document then how can we add that to destination document? There are different type of heades/footers story, some are mostly not exist in destination document.How can we add new range to destination document that match to source document?


    Wednesday, November 20, 2013 8:04 AM
  • Hello Paul,

    I tried with two documents. I see if i copy\override headers and footer in last after copy main content. Then Formatting of header\footer is good. But this take long time (loop through each header\footer of so many pages) and need to calculate next section's index for copy new document's header\Footer. I will try to implement this approach and will discuss if see any issue.

    Thanks

    Dalbir Singh

    Wednesday, November 20, 2013 10:33 AM
  • FWIW I received your message, but I don't think I have anything useful to add on this one.

    The other settings that supposedly affect the formatting of material copied between documents are in the Application.Options object - e.g.

    Options.PasteFormatBetweenDocuments

    and

    Options.PasteFormatBetweenFormattedDocuments

    One piece of documentation I found suggested that you can specify that the source Styles should be retained by setting 

    Options.PasteFormatBetweenFormattedDocuments = wdKeepSourceFormatting

    But I think that what happens is no different from in the PasteFormatBetweenDocuments case - i.e., if no manual formatting is applied to a paragraph, the Destination Style is applied. If manual formatting is applied, it will be retained if wdKeepSourceFormatting is specified, but dropped otherwise.

    It may be that some of the behaviour differences you are seeing are because of these settings.


    Peter Jamieson

    Wednesday, November 20, 2013 5:40 PM
  • Try something based on:

    Sub Demo()
    Dim wdDoc1 As Document, wdDoc2 As Document, Rng As Range, Sctn As Section, HdFt As HeaderFooter
    Set wdDoc1 = Documents.Open("C:\Users\" & Environ("Username") & "\Downloads\source.docx")
    Set wdDoc2 = Documents.Open("C:\Users\" & Environ("Username") & "\Downloads\result.docx")
    With wdDoc1
      .Range.Copy
      Set Rng = wdDoc2.Sections(1).Range
      Rng.Characters.Last.Previous.PasteAndFormat (wdFormatOriginalFormatting)
      For Each Sctn In .Sections
        For Each HdFt In Sctn.Headers
          With HdFt
            If .LinkToPrevious = False Then
              .Range.Copy
              With Rng.Sections(Sctn.Index).Headers(HdFt.Index)
              .LinkToPrevious = False
              .Range.PasteAndFormat (wdFormatOriginalFormatting)
              End With
            End If
          End With
        Next
        For Each HdFt In Sctn.Footers
          With HdFt
            If .LinkToPrevious = False Then
              .Range.Copy
              With Rng.Sections(Sctn.Index).Footers(HdFt.Index)
              .LinkToPrevious = False
              .Range.PasteAndFormat (wdFormatOriginalFormatting)
              End With
            End If
          End With
        Next
      Next
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, November 20, 2013 11:58 PM
  • Hi Peter,

    I think the fundamental issue is that .PasteAndFormat (wdFormatOriginalFormatting) doesn't work for header/footer content when that content is attached to a Section break that has been copied. In any event, copying & pasting document content never copies the final Section's header/footer content, so that still needs its own processing. The macro I've now posted should handle the lot. The one outstanding potential issue is that of differences in page formats for the last-copied Section, but the OP hasn't expressed any concerns about that.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, November 21, 2013 12:12 AM
  • Thanks Paul,

    Format is good with the approach copy/override header footer after main content. We will test this with more documents and if got any issue the will discuss on this.

    Thanks perter to you to check this issue on request.

    ----------------------------------------------

    Thanks
    Dalbir Singh

    Thursday, November 21, 2013 6:22 AM