Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-18-2017, 02:49 PM
Farnkf1970 Farnkf1970 is offline Copy excel userform data to a word template Windows 7 64bit Copy excel userform data to a word template Office 2013
Novice
Copy excel userform data to a word template
 
Join Date: Sep 2017
Posts: 1
Farnkf1970 is on a distinguished road
Default Copy excel userform data to a word template

Hello. I'm relatively new to vba and need some help. I have an excel user form that has a combobox and a number of text boxes, the combobox contains a list of course names. When the user clicks a course name in the combobox the text boxes are then populated by information that is relevant to this course such as cost of course, length of course and the tutor's name and contact number. What I want to do is put a command button on the form so that when the user choses a course in the combobox and all the relevant information is added to the text boxes the user then clicks the command button and this information is copied into a word template. I know how to add the command button and I am quiet happy creating the Word Template I just don't know how to code the command button to copy the data to the word template (I have created form fields in my word template). I am using office 2013 for Windows



Any help would be greatly appreciated.
Reply With Quote
  #2  
Old 10-31-2017, 01:35 AM
FionaMcKenzie FionaMcKenzie is offline Copy excel userform data to a word template Windows 10 Copy excel userform data to a word template Office 2016
Novice
 
Join Date: Oct 2017
Location: Surrey, United Kingdom
Posts: 14
FionaMcKenzie is on a distinguished road
Default

Hi there,

Let me know if this code stub helps you work this one out?

Kind regards

Private Sub DataToWordTemplate()

Dim oWordApp As Word.Application
Dim oWordDoc As Word.Document

Dim lComboBoxIndex As Long

On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")
If Not Err.Number = 0 Then
Err.Clear
Set oWordApp = New Word.Application
End If

On Error GoTo ErrorHandler
oWordApp.Visible = True

' Provide the path to your template
Set oWordDoc = oWordApp.Documents.Add("C:\WordTemplate.dotx", False, wdNewBlankDocument, True)

' Code stub for one form field
lComboBoxIndex = Me.ComboBox1.ListIndex

If lComboBoxIndex >= 0 Then

' To set the text in a FormField
If oWordDoc.FormFields.Count >= 1 Then
' Where Text1 is the Name you gave to the Form Field
oWordDoc.FormFields.Item("Text1").Result = Me.ComboBox1.List(lComboBoxIndex)
End If

End If

CleanUp:
Set oWordDoc = Nothing
Set oWordApp = Nothing

Exit Sub
ErrorHandler:
MsgBox "An error has occurred." & vbCr & vbCr & _
Err.Number & " " & Err.Description
Err.Clear
GoTo CleanUp
End Sub
Reply With Quote
Reply

Tags
transfer to word

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel to Word data import based on UserForm entry jhancock1994 Word VBA 8 05-16-2017 04:41 PM
Excel Userform to Word Template Bookmarks JCrinage Excel Programming 1 11-02-2016 07:03 PM
userform to enter multiple lines of data in template callasabra Word VBA 0 06-27-2014 05:29 PM
Copy data from Word into Excel chinchee Word VBA 18 05-23-2014 05:25 AM
Copy Excel data into an existing Word Table JJG Word 1 12-18-2013 05:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:08 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft