Home > Blockchain >  copy Charts excel to specific paragraphs word with VBA
copy Charts excel to specific paragraphs word with VBA

Time:09-16

I have a word file that contains text. And I have an Excel file that has several charts. I want these charts to be copied in specific paragraphs. For example, the first chart in the fifth paragraph and the second chart in the tenth paragraph and...

I know how to copy a chart in a word file, but not in a specific paragraph. The following code only copies the diagram into the word file, but its position cannot be adjusted (for example, in which paragraph it should be).

   Set chObject = wb1.Worksheets("Sheet1").ChartObjects(c1) 

   chObject.CopyPicture xlScreen, xlPicture

        On Error Resume Next
        Do
            Err.Clear
            WordRange.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine, DisplayAsIcon:=False
            DoEvents
            If Err.Number <> 0 Then Application.Wait DateAdd("s", 1, Now)
        Loop While Err.Number <> 0
        On Error GoTo 0
        

Can anyone help?

CodePudding user response:

Finally, I understood the answer to the problem, it is enough to use the paragraph object like the code below. The following code copies the diagram in the fifth paragraph

   chObject.Chart.ChartArea.Copy

           myDoc.Paragraphs(10).Range.PasteSpecial Link:=False, _
            DataType:=wdPasteMetafilePicture, _
            Placement:=wdTight, _
            DisplayAsIcon:=False
  • Related