Please help me: I have copied cells in the buffer from Excell, I just can’t do a special insert in Word. Here is a piece of code:

xl.Run("PERSONAL.xlsb!Macros1") #запуск макроса в открытом Excel xl.ActiveCell.CurrentRegion.Select() #выбираем все ячейки xl.Selection.Copy() #копируем в буфер word = win32com.client.Dispatch("Word.Application") word.Documents.Open('12345.doc') #запускаем нужный документ word.Selection.Find.Text = 'some_word' #ищем слово, которое надо заменить word.Selection.Find.Execute() word.Selection.PasteSpecial() #Тут заменяем что искали на содержимое буфера со специальной вставкой 

The problem is actually in the last line, I don’t know which parameter to pass to be inserted with the necessary formatting (in my case, you need PasteAsImage). At the English-language forum they asked a similar question to insert into Excel, a person could decide by writing xl.Selection.PasteSpecial (Paste = constants.xlPasteValues) , but with Vord I didn’t have it. I think that the clue is to look for link 1 here and link 2 here, but still nothing works. If I pass a parameter from the second link, for example like this: word.Selection.PasteSpecial (DataType = 4) , it is still inserted in the original formatting.

    2 answers 2

    In the end, I decided to go a little on the other side. Instead of "Paste as picture", I used "Copy as picture" and then Paste pasted all the content. The output is the following:

      xl.Run("PERSONAL.xlsb!Macros1") #запуск макроса в открытом Excel xl.ActiveCell.CurrentRegion.Select() #выбираем все ячейки xl.Selection.CopyPicture() #копируем в буфер уже как картинку word = win32com.client.Dispatch("Word.Application") word.Documents.Open('12345.doc') #запускаем нужный документ word.Selection.Find.Text = 'some_word' #ищем слово, на место которого надо вставить скопированное из Excel word.Selection.Find.Execute() word.Selection.Paste() #Вставляем содержимое буфера (картинку) 

      Yes, the answer must be sought by the second link. All options are clearly spelled out there, and there’s nothing like PasteAsImage. By the way, and Excel too ( link ).

      Several options. You can set a link to the desired Range, and not copy it. You can copy to the object "Excel table". The remaining options are more complicated.

      UPD: Macro Recorder thinks that it is necessary like this:

       Sub Макрос1() Selection.PasteExcelTable False, False, False End Sub 

      But it is in Office-2003. Wait there is no other hand.

      • I did not mean that I am looking for a method directly with the name PasteAsImage, I wanted to describe the format in which I need an insert. Whichever key I use from the link is still inserted without formatting. - moffire
      • And what kind of formatting is we talking about then, if you want to insert LIKE A PICTURE? then you don't need to take a Range in the clipboard, but a screenshot. - Akina
      • No need to take any screenshot. Copy several cells in Excel and in the Word, click Paste and the picture-cells will be inserted as an image. That is what I need to do. - moffire
      • Well, do it and record it as a macro recorder ... - Akina