PDA

View Full Version : Conditional insert of document into word



pdavis
07-02-2007, 11:18 AM
I am trying to set up a form in word which will allow users to chose whether certain text should be included (depending on variables). I have the beginning set up but in trying to test, I cannot get the macro to insert text.

I have created a separate word document that includes all of the text to be inserted (and nothing else).

The code I have is:

Sub GetAnAnswer()
Set objVar = Word.Application
MsgBox "Is this being sent to a special provider (i.e. radiologist or MRI)?", vbYesNo
Response = GetYesNo((Prompt))
End Sub
Function GetYesNo(Message)
If MsgBox(Message.vbYesNo) = vbYes Then
GetYesNo = True
Else
GetYesNo = False
End If

If GetYesNo = True Then
Selection.InsertFile ("I:\MRI Additions.doc")
End If
End Function


I continue getting the "Object required" error and have tried multiple ways to set my documents but have gotten nowhere.

Any suggestions?

mdmackillop
07-02-2007, 01:25 PM
Hi
Welcome to VBAX
You're overcomplicating a bit.:bug:
Option Explicit
Sub GetAnAnswer()
Dim Response As Integer
Response = MsgBox("Is this being sent to a special provider (i.e. radiologist or MRI)?", vbYesNo)
If Response = vbYes Then
Selection.InsertFile ("I:\MRI Additions.doc")
End If
End Sub

If you're looking to make up a document from Inserts, this could be made easier by using a Userform to list the available inserts, or you could start with the whole thing and delete options. Have a look at this question (http://www.vbaexpress.com/forum/showthread.php?t=13543) and the enclosure here (http://www.vbaexpress.com/forum/showpost.php?p=104877&postcount=11).

fumei
07-03-2007, 01:40 PM
pdavis, please note the use of Option Explicit in Malcolm's code.

It is strongly suggested that you use Option Explicit. It will save you grief in the long run.

What the heck are you doing with the Set objVar = Word.Application???????