1

I want to develope VBA macro at excel due to export data to a spefic word documents with bookmarks. My Questions are: c#-1. I have one table and i sent it as unformated txt. c#-2. Also to choose between to word files where to sent the data:

The code that i have is :

Private Sub CommandButton1_Click()

Dim WordApp As Word.Application
Dim WordDoc As Word.Document

Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open("C:\Users\ChatzisavasK\Desktop\Offering\test\Test2.docx") `(Also i need to see if there is the possibility to choose *.docx )`

Range("a1:c10").Select `This work properly`
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
WordApp.Visible = True
WordApp.ActiveDocument.Bookmarks("offer").Select
Set objSelection = WordApp.Selection
objSelection.Paste

Range("F1").Select `this does not work`
Selection.PasteSpecial DataType:=2
WordApp.Visible = True
WordApp.ActiveDocument.Bookmarks("e1").Select
Set objSelection = WordApp.Selection
objSelection.Paste

End Sub


braX
  • 11,506
  • 5
  • 20
  • 33
  • You do not need to select the range (either in Word or Excel) to work with it. You may want to see [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba). Also when you say `this does not work` What do you mean? What error message are you getting? – Siddharth Rout Jan 27 '21 at 11:15
  • hello, i have #Run-time error 1004: Application-defined or object-defined error#. Also when i just use '''Range("F1").copy (With out selection) it is past as cell to word'''. – Kostas Chatzi Jan 27 '21 at 11:36

1 Answers1

0

Here's the code that works. Be aware that once you paste into a bookmarked area, the bookmark is removed. If you want to retain the bookmark, you must re-insert it.

Sub TestDemo()
    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    
    Set WordApp = New Word.Application
    WordApp.Visible = True
    Set WordDoc = WordApp.Documents.Open("\\Mac\Home\Desktop\TestDoc.docx")
    
    Range("a1:c10").Select 'This work properly`
    Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    WordApp.ActiveDocument.Bookmarks("offer").Range.Paste
End Sub
Rich Michaels
  • 1,663
  • 2
  • 12
  • 18
  • Thanks Michael for your advice. But if you see to my question at the above this operation i have it and it is work properly. The problem is in the second part of my code with the extra data that i want to export as text and i want to choose 1 of the 2 word file that i will have for this project. – Kostas Chatzi Jan 27 '21 at 14:29
  • @KostasChatzi what exactly are you trying to do with Range("F1") in your worksheet? Are you trying to paste into it something from the clipboard? Or do you want to copy the value of F1 and paste it into a Word document? – Rich Michaels Jan 27 '21 at 17:04
  • i have one excel tool and i made some cost analysis for differents projects. From this excel i would like to extract all the information and to choose between 2 Words (Offers, one in English and the other in Greek Lag) the below categories: c#-1. One table as picture format and c#-2 some cells in txt format – Kostas Chatzi Jan 28 '21 at 07:46