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