Inserting a file

L

Lee Kiwiflame

I am wanting to insert a file depending on which option button is selected.
I am using DOCVARIABLES within the template and need to insert a file if the
"OptNoOther" is True.

The code I have is:

If frmAudit.OptNoOther = True Then
ActiveDocument.Select.Variables ("additional")
With Selection
.InsertFile
FileName:="M:\PROCESSES\Documents\Templates\Building\BWOFInsert.doc"
End With

The above code does not work and I am new to VBA so I'm not sure what code I
should be using.

Help is appreciated.
 
D

Doug Robbins - Word MVP

You should insert a bookmark where you want the file to be inserted and then
use:

ActiveDocument.Bookmarks("additional").Range.InsertFile
"M:\PROCESSES\Documents\Templates\Building\BWOFInsert.doc"


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
L

Lee Kiwiflame

Thanks Doug

The reason I had a DOCVARIABLE was because someone on here said it wasn't
good practice to use bookmarks when inserting text.

To explain the whole story - users have three option boxes. The following
is what is inserted depending on the option box.

'Test to see which Option box was checked for the Building being Compliant

If frmAudit.OptYes = True Then
strOption = 1
End If

If frmAudit.OptNoUrgent = True Then
strOption = 2
End If

If frmAudit.OptNoOther = True Then
strOption = 3
End If

'Insert the correct option box

Select Case strOption
Case 1
ActiveDocument.Variables("additional").Value = "All records and
systems were found to comply with the Building Act 2004. We thank you for
your co-operation and look forward to your continued compliance in the
interests of safety within your premises."

Case 2
ActiveDocument.Variables("additional").Value = _
"The following items require rectification within " & strrectify
& " days." & vbCr & vbCr & _
"1. " & vbTab & vbCr & vbCr & _
"2. " & vbTab & vbCr & vbCr & _
"3. " & vbTab

Case 3
ActiveDocument.Select.Variables ("additional")
With Selection
.InsertFile
FileName:="M:\PROCESSES\Documents\Templates\Autotext\BWOFInsert.doc"
End With
End Select

Option 3 is where I want the file to be inserted.

Regards
Lee
 
L

Lee Kiwiflame

and another thing... With DOCVARIABLES, I have a button set up that people
can click to "Reset Form Details". This will launch the userform for people
to fill again, and it will "overtype" what information is already there....

Because inserted information "lives" within the DOCVARIABLE brackets and
everything can be deleted / replaced / updated etc.

With bookmarks, it can only insert. If it needed to be updated, people will
need to manually delete the inserted text and hope that they don't delete the
bookmark.

If there is a better way, then I'm listening :)
 
D

Doug Robbins - Word MVP

It's a case of "horses for courses"

To have text that is entered into a control on a userform appear in a
document, the use of document variable and docvariable fields definitely has
its advantages. Not so though for creating a place holder into which the
text from an external file is to be inserted.

See the article "Working with Bookmarks in VBA" at:

http://www.word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top