Transferring from Excel into Word template with Form Fields

zombiemaster

Board Regular
Joined
Oct 27, 2009
Messages
241
I found code online that I am hoping will copy from cells in a row in Excel into form fields in a Word document but I'm getting an error. Here is what I have:

VBA Code:
Sub Transition_Test()
    Dim ws As Worksheet
'    Set ws = ThisWorkbook.Sheets("Billing")
    Dim objWord As Object
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    objWord.Documents.Open "C:\Users\ZM\Desktop\Word template.rtf"
    With objWord.ActiveDocument
      .Text1.Value = ws.Cells(Selection.Row, 8).Value
      .Text2.Value = ws.Cells(Selection.Row, 5).Value
      .Text3.Value = ws.Cells(Selection.Row, 4).Value
      .Text4.Value = ws.Cells(Selection.Row, 6).Value
      .Text10.Value = ws.Cells(Selection.Row, 22).Value
    End With
End Sub

I'm getting a Subscript Out Of Range error on this row:

VBA Code:
    Set ws = ThisWorkbook.Sheets("Billing")

Any ideas? I'm sure it's simple but I can't think right now...

Thanks in advance,
~ZM~
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
UPDATE:

I have been researching and modified my code to show this:

VBA Code:
Sub Transition_Test()
    Sheets("Billing").Select
    
    Dim wb As Workbook
    Dim ws As Worksheet
    
    Set wb = ThisWorkbook
    Set ws = ThisWorkbook.ActiveSheet
    Dim objWord As Object
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    objWord.Documents.Open "C:\Users\ZM\Desktop\Word template.docx"
    With objWord.ActiveDocument
      .Text1.Value = ws.Cells(Selection.Row, 8).Value
      .Text2.Value = ws.Cells(Selection.Row, 5).Value
      .Text3.Value = ws.Cells(Selection.Row, 4).Value
      .Text4.Value = ws.Cells(Selection.Row, 6).Value
      .Text10.Value = ws.Cells(Selection.Row, 22).Value
    End With
End Sub

This got me past the initial error, but now I'm getting a Runtime error 438: object doesn't support this property or method when I get to the Text1.Value line. Thanks for any help, I know I'm missing something simple but I am in a different job now and this is the first time dealing with VBA in over a year...I've lost a lot of my previous know-how... :cry:

~ZM~
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top