alexsly
01-08-2017, 10:32 AM
I'm trying to copy a cell value and paste it into a text box (ActiveX) in a Word document. It's a part of longer code and the rest works fine, but I'm struggling with this part. What I want to achieve is to create a new document from existing template, add two variables into it and save as a new file. The variables are simple just name and last name.
While saving a file is not a problem I can't copy values from excel into text boxes in a word file. I use ActiveX text boxes. Fields have to be easily moved and arranged in the document. Previously I just used copy/paste code from excel into word and it worked but horizontal and vertical alignments are crucial and with a copied cell I could do nothing. I couldn't find any solution here on forum and on the web. If someone has any ideas what I am missing, it would be very helpful, I'm poor at vba so I guess I miss a simple code line.
Dim WdObj As Object, fname As Stringfname = "test"
Set WdObj = CreateObject("Word.Application")
WdObj.Visible = False
WdObj.Documents.Open Filename:="C:\path"
WdObj.textbox_name.Value = Sheets("sheet_name").Range("A2").Value 'here is what does not work
WdObj.textbox_name2.Value = Sheets("sheet_name").Range("A3").Value 'here is what does not work
With WdObj
.ChangeFileOpenDirectory "C:\path2"
.ActiveDocument.SaveAs Filename:=fname & ".doc"
End With
With WdObj
.ActiveDocument.Close
.Quit
End With
Set WdObj = Nothing
While saving a file is not a problem I can't copy values from excel into text boxes in a word file. I use ActiveX text boxes. Fields have to be easily moved and arranged in the document. Previously I just used copy/paste code from excel into word and it worked but horizontal and vertical alignments are crucial and with a copied cell I could do nothing. I couldn't find any solution here on forum and on the web. If someone has any ideas what I am missing, it would be very helpful, I'm poor at vba so I guess I miss a simple code line.
Dim WdObj As Object, fname As Stringfname = "test"
Set WdObj = CreateObject("Word.Application")
WdObj.Visible = False
WdObj.Documents.Open Filename:="C:\path"
WdObj.textbox_name.Value = Sheets("sheet_name").Range("A2").Value 'here is what does not work
WdObj.textbox_name2.Value = Sheets("sheet_name").Range("A3").Value 'here is what does not work
With WdObj
.ChangeFileOpenDirectory "C:\path2"
.ActiveDocument.SaveAs Filename:=fname & ".doc"
End With
With WdObj
.ActiveDocument.Close
.Quit
End With
Set WdObj = Nothing