VBA macro to insert text with ifthenelse

Microsoft Word 2007
August 2, 2010 at 13:37:34
Specs: Windows XP
I am trying to automate a Word 2007 document. I have a yes/no combo box. If the user selects yes in that combo box, I want the macro to insert a block of text in a designated form field. If no, insert a different block of text at that location. I have tried everything I can think of, and all the different suggestions I have found online, in the macro but can never get anything except the first "if" or both "ifs" to populate the field...here's what I have ended up with today, it inserts both.

Sub CreateDoc()
If ComboBox3 = Yes Then
Selection.GoTo What:=wdGoToBookmark, Name:="Scope"
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.InsertAfter "SCOPE ATTACHED"
End If
If ComboBox3 = No Then
Selection.InsertAfter "NOTHING ATTACHED "
End If
End Sub

I want this to be a self-contained automated document that can be sent to various staff members to create documents. There will be multiple combo boxes and ifthenelse statements, but I need to get the first one to work before I can go on to the next. Any help would be most appreciated. Thanks.


See More: VBA macro to insert text with ifthenelse

Report •

#1
August 3, 2010 at 04:53:18
You don't have Option Explicit enabled, do you? You really should. It'd catch errors like this before you ripped your hair out.

Report •

#2
August 3, 2010 at 07:45:35
Unfortunately, everything I know about Option Explicit I read this morning through a Google search. I did enable it, but all it does is tell me I need to define the variable, and I don't know how to define the variable. I am not a programmer, obviously, and have limited macro experience, also trial and error, all previous in Access, not Word. Can you give me a suggestion on how to define the variables in the macro? Thanks..

Report •

#3
August 3, 2010 at 08:56:30
Though the Dim statement.

But that wasn't the lesson you were supposed to learn. In this instance, you're trying to use two variables named Yes and No. What you want to do is use a literal string containing "Yes" or "No".

Also, there's no difference between the behavior of VBA in Access and the behavior of VBA in Word. The only difference is in the object model each application presents.


Report •

Related Solutions

#4
August 3, 2010 at 12:59:11
Well, this stuff is obviously way above my head, but I was finally able to get it to work this way:

Private Sub ComboBox3_DropButtonClick()
ComboBox3.List = Array("CHOOSE YES OR NO", "Yes", "No")
End Sub
---------------------------------------------------------------------------
Private Sub ComboBox3_Change()
Select Case ComboBox3
Case "Yes"
Selection.GoTo What:=wdGoToBookmark, Name:="Scope"
Selection.InsertAfter "SCOPE ATTACHED"
Case "No"
Selection.GoTo What:=wdGoToBookmark, Name:="Scope"
Selection.InsertAfter "NOTHING ATTACHED "
End Select
End Sub

Thanks for your help! Some day I'm going to take a programming class so I'll know what language you're speaking :)


Report •

Ask Question