new here, and in VBA in general. I have created a macro that copies the contents of a cell in excel and pastes in a specific location in a word document. FYI, i use bookmarks in word to select the exact location for pasting. the issue is that everything copied inserts a line and/or paragraph/carriage return. i have found a lot of possible solutions but none of them work, prob given my inexperience in VBA. Please help!
Sub OpenWord()
Dim WordApp As Object
Dim WordDoc As Object
Dim R1 As Object
Dim R2 As Object
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(Filename:="C:\Users\KG\Desktop\VBA WIP\FAfile.docx")
Set R1 = WordDoc.Bookmarks("b1")
Set R2 = WordDoc.Bookmarks("b2")
WordApp.Visible = True
WordApp.Activate
Sheets("Details INPUT").Range("H4").copy
R1.Select
WordApp.Selection.PasteAndFormat Type:=wdFormatSurroundingFormattingWithEmphasis
Application.CutCopyMode = True
Sheets("Details INPUT").Range("H7").copy
R2.Select
WordApp.Selection.PasteAndFormat Type:=wdFormatSurroundingFormattingWithEmphasis
Application.CutCopyMode = True
Set WordDoc = Nothing
Set WordApp = Nothing
Set R1 = Nothing
Set R2 = Nothing
End Sub
Multiple issues here.
At first since you are using late binding CreateObject("Word.Application")
you probably not have included references to Microsoft Word ... Object Library
. But then the constant wdFormatSurroundingFormattingWithEmphasis will not be set and be 0. Using late binding the constant names cannot be used. The appropriate values must be used instead.
And using Selection.PasteAndFormat you are pasting the whole table cell instead of only the value. As of your description you wants only pasting the value.
To pasting the value only try Selection.PasteSpecial:
...
Sheets("Details INPUT").Range("H4").Copy
R1.Select
'WordApp.Selection.PasteAndFormat Type:= 20
WordApp.Selection.PasteSpecial DataType:=2
Application.CutCopyMode = False
Sheets("Details INPUT").Range("H7").Copy
R2.Select
'WordApp.Selection.PasteAndFormat Type:= 20
WordApp.Selection.PasteSpecial DataType:=2
Application.CutCopyMode = False
...
Where the 2 is the value of wdPasteText.
If formatted content will be needed from Excel, then do using wdPasteRTF instead, which is 1 instead of 2.