![]() |
#1
|
|||
|
|||
![]()
I'm using adobe acrobat to convert a pdf into a word doc, but that doc has its own styles that if I directly copy and paste all the contents into my custom template, it'll flush out my custom styles in that template. As a compromise, I currently just copy and paste the texts as plain texts into my template and manually insert individual images into the doc. Is there a way to copy and paste texts as plain texts but with images such that the order of them is maintained through VBA code? My current code below can only copy and paste the images without changing my template, but it even sometimes also skips some images that I don't know why. I also attached images and files at the very end as examples for clarification.
Code:
Sub CopyPasteToTemplate() Dim InputDoc, OutputDoc As word.Document Dim paragraph As word.paragraph Dim shape As word.InlineShape Application.ScreenUpdating = False Set InputDoc = Application.ActiveDocument Set OutputDoc = Documents.Add(Template:="XXX\Templates\Normal.dotm") For Each paragraph In InputDoc.Paragraphs For Each shape In paragraph.Range.InlineShapes If shape.Type = wdInlineShapePicture Then 'Copy and paste the images without formating shape.Select Selection.Copy With OutputDoc.Content .InsertParagraphAfter .Paragraphs.Last.Range.Paste End With End If Next ' paragraph.Range.Copy ' With OutputDoc.Content ' .InsertParagraphAfter ' .Paragraphs.Last.Range.PasteSpecial wdFormatPlainText 'Sometimes gives an error ' End With Next OutputDoc.Activate Application.ScreenUpdating = True End Sub Edit: What I mean by "flushing the styles" is simply that before the pasting, my template and its instance doc have the styles as the left screenshot below, but after pasting in the contents, the styles section becomes the right screenshot below. In the attached files, "acrobat export.docx" is the example docx output by acrobat, "direct copy paste.docx" is the resulting doc if I simply ctrl + A and ctrl + C to copy all contents in "acrobat export.docx" and paste in my custom dotm. "manually processed.docx" is what I want the copy and paste into my template to look like. You can see that it's quite different from the previous 2 docx, including the page size/orientation and headers etc--these are the same as my dotm template. Currently, I can only make it by copying and pasting "acrobat export.docx" as plain texts into my template and manually inserting the images with a reference to the "acrobat export.docx" file. Is there a way to iterate through all the objects (or paragraphs?) in the "acrobat export.docx" and judge... - If text, copy and paste plain text into "code processed.docx" - If bullet list, copy and paste plain text into "code processed.docx" with that "·" in the front. Same for the numbered list to preserve the numbering - If image (no matter anchored or not), copy and paste the image as it is into "code processed.docx" - If text box, extract the content, copy, and paste plain text into "code processed.docx" - Ignore all other cases I just want to maintain the order of texts and images in a cleaner way. Last edited by puff; 01-11-2023 at 07:00 PM. |
Tags |
copy and paste, image insert, template document styles |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Copy Between Two Headings/Texts | Noob_VBA | Word VBA | 14 | 12-01-2021 12:28 PM |
copy paste webp image from internet to word fails to appear | DBenz | Word | 4 | 05-20-2020 06:49 PM |
Need to copy texts from excel and paste in to the Notepad++ in between the particular tags | ganesang | Word VBA | 2 | 08-27-2018 02:05 AM |
![]() |
Wyck | Word | 1 | 09-21-2016 03:49 PM |
OneNote - Copy and paste image to the table | MartinK | OneNote | 0 | 09-03-2013 05:08 AM |