vba and bookmarked word
RH

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » VBA and Bookmarked Word docs

VBA and Bookmarked Word docs

resolvedResolved · High Priority · Version 2010

Vicki has attended:
Excel VBA Introduction course

VBA and Bookmarked Word docs

Hi,

If I have bookmarks in a Word document can I somehow write script that enables me to select from a drop down box in a form in Excel and insert that value into a bookmark in the word doc?

Thanks

RE: VBA and Bookmarked Word docs

Hi Vicki,

Thank you for the forum question.

To navigate to the bookmark you can use the line below (in my example the name of the bookmark is KeyExp).


wApp.Selection.Goto What:=wdGoToBookmark, Name:="KeyExp"

Then just enter the code you used to enter the text in the code I have seen.

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

RE: VBA and Bookmarked Word docs

Thanks Jens,

I'm still struggling - getting the error message

Run time error 91
Object variable or With block variable not set

I know this is all advanced VBA but my reasons for coming on the course was purely to create this so I do need to keep persevering. I'm sorry for all the questions, I have been trying to find answers on google too :( . Also, should this not be wdoc.Selection.goto What....etc. because we're referring to a specific document not the application?

I feel like it's not finding the template. My code now looks like this:

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\Compliance Documents\SoDaN Test\SoDaN Test Template.dotx")
wapp.Visible = True
wapp.Selection.Goto What:=wdGoToBookmark, Name:="AddOns"
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: VBA and Bookmarked Word docs

Hi Vicki,

Thank you for the forum question.


You are not setting the wapp object. You will need to set an instance of the wapp object.

This is done like this:

Set wapp=CreateObject("Word.Application")

I have added the line in the right order in your code below.

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\Compliance Documents\SoDaN Test\SoDaN Test Template.dotx")
wapp.Visible = True
wapp.Selection.Goto What:=wdGoToBookmark, Name:="AddOns"
If Me.OptionButton1 = True Then
wdoc.Selection.typetext.Sheet2 Range("b2")
ElseIf Me.OptionButton2 = True Then
wdoc.Selection.typetext.Sheet2 Range("b3")
End If



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

RE: VBA and Bookmarked Word docs

Ok, I've done this and now I have another run time error of 5174 - application defined or object defined error. Does this mean it's not recognising my file path? I've tried saving the template to alternative file paths and re mapping but it doesn't seem to make a difference. :(

RE: VBA and Bookmarked Word docs

Hi Vicki,


I have tested your code in Excel (my code below). I didn't get any error.

It must be your file path.

I assume that you have activated the Word library and that when you reference the option buttons the are named OptionButton1 and OptionButton2.


Private Sub CommandButton1_Click()
Dim wapp As Word.Application
Dim wdoc As Word.Document
Set wapp = CreateObject("Word.Application")
Set wdoc = wapp.Documents.Open("C:\Users\Jens\Desktop\test.dotx")
wapp.Visible = True
wapp.Selection.Goto What:=wdGoToBookmark, Name:="Test"

wapp.Selection.TypeText "Test"
'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

Will be marked as resolved in 5 days

Notice: This is an automated message. Due to inactivity, this forum post will be marked as 'resolved' if there are no further responses in the next 5 days.

RE: VBA and Bookmarked Word docs

Hi, Still having problems with this, I'm trying to command that excel take the textbox1 and insert where a certain bookmark is in the word doc - it's opening the word doc but not populating it with the text. then when I close the word doc down I get the run time error 438 Object doesn't support this property or method. Current script I have is the below:

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:\SoDaN Test\SoDaN Test Template.dotx")
wapp.Visible = True

wapp.Selection.Goto what:=wdGoToBookmark, Name:="Reference"
Selection.TypeText.ActiveSheet.TextBox1

I know the top part works, but it's the selection bit that's making it fail (I think?)

Also, when using the option buttons, the same thing happens, it doesn't populate the bookmark. It doesn't do anything. Script I have for that is:

If Me.OptionButton1 = True Then
wapp.Selection.Goto what:=wdGoToBookmark, Name:="AddOns"
wdoc.Selection.TypeText.Sheet2 Range("b2")
ElseIf Me.OptionButton2 = True Then
Selection.TypeText.Sheet2 Range("b3")
End If

RE: VBA and Bookmarked Word docs

Hi Vicki,

I have amended the code. Please see the code below.


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\Compliance Documents\SoDaN Test\SoDaN Test Template.dotx")
wapp.Visible = True

wapp.Selection.Goto what:=wdGoToBookmark, Name:="test2"
wapp.Selection.TypeText ActiveSheet.TextBox1


If Me.OptionButton1 = True Then
wapp.Selection.Goto what:=wdGoToBookmark, Name:="test"

wapp.Selection.TypeText Sheet2.Range("b2")


ElseIf Me.OptionButton2 = True Then
wapp.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

RE: VBA and Bookmarked Word docs

YESSS!!!! THANK YOU SO MUCH!!!!

RE: VBA and Bookmarked Word docs

Hi Jens,

Thank you so much for your help on this, it's going to make a massive difference for us.

I have one last question, if I want to say if a text box is blank enter nothing, but if's it populated insert the text, how would I go about doing this?

For the second bit, I assume I can still use:

wapp.Selection.Goto what:=wdGoToBookmark, Name:="bkmarkname"
wapp.Selection.TypeText ActiveSheet.TextBox1

but not sure what to put for the If me part and how to highlight if it's blank and also what Else if command to use?

Thanks again for all your help.

RE: VBA and Bookmarked Word docs

Hi Vicki,

Please see my amended code below.



Private Sub CommandButton1_Click()

Dim wapp As Word.Application
Dim wdoc As Word.Document
Set wapp = CreateObject("Word.Application")
Set wdoc = wapp.Documents.Open("C:\Users\Jens\Desktop\del.dotx")
wapp.Visible = True


If ActiveSheet.TextBox1 = "" Then
GoTo ConTinueNext
Else
wapp.Selection.Goto what:=wdGoToBookmark, Name:="test2"
wapp.Selection.TypeText ActiveSheet.TextBox1
End If

ConTinueNext:

If Me.OptionButton1 = True Then
wapp.Selection.Goto what:=wdGoToBookmark, Name:="test"

wapp.Selection.TypeText Sheet2.Range("b2")


ElseIf Me.OptionButton2 = True Then
wapp.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

RE: VBA and Bookmarked Word docs

brilliant! thank you so much :)

 

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:

Counting Blanks

Some times you want to check if there are cells missing data in your range. You can use the COUNTBLANK FUNCTION to acheive this. It is =COUNTBLANK(Range). Note Cells with formulas that return "" (empty text) are also counted. Cells with zero values are not counted.

View all Excel hints and tips


Server loaded in 0.05 secs.