GreatSc0tt
GreatSc0tt

Reputation: 11

Import Excel Data into Word

I have some code that i wrote to export data from cells and it imports it into a word documents.

My two issues that i am having trouble with are as followed

  1. Right now it just imports the cell data into the start of the document. What i need it to do is import the data into predefined textboxes

  2. Right now i can run the code in the VBA editor and step into the code and run through it with no issues but if I assign the code to a button it errors. The problem is the box appear and there is no text in it

here is my current code

Sub Invoice()
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\Users\account\Desktop\Invoice.docx"

Dim strValue1 As String
Dim strValue2 As String
Dim strValue3 As String

objWord.Activate

strValue1 = Cells(Selection.Row, 2)
strValue2 = Cells(Selection.Row, 3)
strValue3 = Cells(Selection.Row, 4)
objWord.Selection.TypeText Text:=strValue1
objWord.Selection.TypeText Text:=strValue2
objWord.Selection.TypeText Text:=strValue3
End Sub

any help would be great appreciated

Cheers! GreatSc0tt

Upvotes: 0

Views: 180

Answers (1)

LKW
LKW

Reputation: 180

you can access the textboxes by:

objWord.ActiveDocument.Shapes(1).TextFrame.TextRange.Text = strValue1
objWord.ActiveDocument.Shapes(2).TextFrame.TextRange.Text = strValue2

hope this helps!

Upvotes: 1

Related Questions