Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

input form output

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Input form - output word doc

Input form - output word doc

ResolvedVersion 2010

Vicki has attended:
Excel VBA Introduction course

Input form - output word doc

I have created a form in Excel and want to get it to populate a word template when clicking on a command button. I have tried starting with the code below, but it's telling me there is a runtime error and looks like the error is relating to the file path, which is correct, am I doing the code wrong? would it be easier to do this all in word for a word output document? thanks

Private Sub CommandButton1_Click()
Dim wapp As Word.Application
Dim wdoc As Word.document
Set wdoc = wapp.documents.Open("J:\smi-uk.ajgco.com\smidata\VOL1\Financial Risks\Ops\Vicki\SoDaN Template.dotx")
wapp.Visible = True
If Me.OptionButton1 = True Then
wdoc.Selection.typetext.Sheet2 Range("b2")
ElseIf Me.OptionButton2 = True Then
wdoc.Selection.typetext.Sheet2 Range("b3")
End If

RE: Input form - output word doc

Hi Vicki,

Thank you for the forum question.

I didn't spot that you missed a very important line in the code you sent me in the email.


You do not create an instance of the Word application object.

Set wapp = CreateObject("Word.Application")

The above line will do this. I have placed the line in the code below.





Set wapp = CreateObject("Word.Application")

Private Sub CommandButton1_Click()
Dim wapp As Word.Application
Dim wdoc As Word.document
Set wapp = CreateObject("Word.Application")

Set wdoc = wapp.documents.Open("J:\smi-uk.ajgco.com\smidata\VOL1\Financial Risks\Ops\Vicki\SoDaN Template.dotx")
wapp.Visible = True
If Me.OptionButton1 = True Then
wdoc.Selection.typetext.Sheet2 Range("b2")
ElseIf Me.OptionButton2 = True Then
wdoc.Selection.typetext.Sheet2 Range("b3")
End If
End Sub

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Adding Rows or Columns in an Excel 2010 Worksheet

If you want to add a row to an Excel spreadsheet, these are the simple steps you should take:

With your mouse, right click on the row header below where you want the new row to be added. Then, click Insert.

Follow exactly the same steps if you want to add a column to an Excel worksheet, right click on the column header, choose Insert and the new column will be inserted to the left of the selected column.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.28 secs.