Using VBA ti insert one document into another

K

Kaatwa

I have a routien which gathers information for a contract from the user by
way of a form. One check box gives the option to include an addendum from the
users own files. My routine opens the common file open dialog keeps the name
of the selected file as a string and later at print time opens that document,
copies the text and pastes it inthe main document.

All worked well for some times but now it has started throwing "Bad File
Name" errors and refusing to play. To date I can see no pattern to when this
happens

What have I got wrong that this should happen?

Help
 
D

Doug Robbins - Word MVP

From the information in your post, it is not possible to even tell what you
have got right, let alone what is wrong. How about showing us the code in
your routine(s)?

--
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, originally posted via msnews.microsoft.com
 
K

Kaatwa

OK - sorry if it was all confusing.

The section which asks for the location of the file to incorporate is like
this in the form's code:

Private Sub chkAddendum_Click()
' Options 11 b)

On Error GoTo chkAddendum_Click_Err

' If an addendum is wanted open the common dialog box for file open
' When file is selected, copy it all and store ready to paste into the end
of the Contract

With Dialogs(wdDialogFileOpen)
If .Display Then
strAddendumSource = .Name
Else: MainForm.chkAddendum = False
End If
End With

chkAddendum_Click_End:
Exit Sub

chkAddendum_Click_Err:
MsgBox "Error#: " & Err.Number & vbCrLf & Err.Description & vbCrLf & "In
Main Form - chkAddendum_Click"
Resume chkAddendum_Click_End

End Sub

And the section which (usually) pastes it in a different module is this:

Documents.Open FileName:=MainForm.strAddendumSource, _
ReadOnly:=False, _
AddToRecentFiles:=False

Documents(MainForm.strAddendumSource).Selection.Range
rngTemp.WholeStory
rngTemp.Copy
Documents(MainForm.strAddendumSource).Close
Selection.Paste

As I (tried) to explain it usually works but sometimes throws a "bad File
Name" error from a selection of real files!?!

Thanks in anticipation
 
F

Fumei2 via OfficeKB.com

I suspect it is this: MainForm.strAddendumSource

What is that? It sure does not look like a string. If mainForm is a
userform, then a string is NOT a dot object.
OK - sorry if it was all confusing.

The section which asks for the location of the file to incorporate is like
this in the form's code:

Private Sub chkAddendum_Click()
' Options 11 b)

On Error GoTo chkAddendum_Click_Err

' If an addendum is wanted open the common dialog box for file open
' When file is selected, copy it all and store ready to paste into the end
of the Contract

With Dialogs(wdDialogFileOpen)
If .Display Then
strAddendumSource = .Name
Else: MainForm.chkAddendum = False
End If
End With

chkAddendum_Click_End:
Exit Sub

chkAddendum_Click_Err:
MsgBox "Error#: " & Err.Number & vbCrLf & Err.Description & vbCrLf & "In
Main Form - chkAddendum_Click"
Resume chkAddendum_Click_End

End Sub

And the section which (usually) pastes it in a different module is this:

Documents.Open FileName:=MainForm.strAddendumSource, _
ReadOnly:=False, _
AddToRecentFiles:=False

Documents(MainForm.strAddendumSource).Selection.Range
rngTemp.WholeStory
rngTemp.Copy
Documents(MainForm.strAddendumSource).Close
Selection.Paste

As I (tried) to explain it usually works but sometimes throws a "bad File
Name" error from a selection of real files!?!

Thanks in anticipation
From the information in your post, it is not possible to even tell what you
have got right, let alone what is wrong. How about showing us the code in
[quoted text clipped - 17 lines]
 
D

Doug Robbins - Word MVP

strAddendumSource could be the name of a textbox control on the userform,
but if it were also declared somewhere else there could be some confusion
over what it actually is. As the OP is working across multiple modules,
this is a distinct possibility

If that is the case, it would be better to properly address it in the
Dialogs(wdDialogFileOpen) routine and use its .Text property as well. The
same should be done where ever it is used.

With Dialogs(wdDialogFileOpen)
If .Display Then
MainForm.strAddendumSource.Text = .Name
Else
MainForm.chkAddendum.Value = False
End If
End With

--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.

Fumei2 via OfficeKB.com said:
I suspect it is this: MainForm.strAddendumSource

What is that? It sure does not look like a string. If mainForm is a
userform, then a string is NOT a dot object.
OK - sorry if it was all confusing.

The section which asks for the location of the file to incorporate is like
this in the form's code:

Private Sub chkAddendum_Click()
' Options 11 b)

On Error GoTo chkAddendum_Click_Err

' If an addendum is wanted open the common dialog box for file open
' When file is selected, copy it all and store ready to paste into the
end
of the Contract

With Dialogs(wdDialogFileOpen)
If .Display Then
strAddendumSource = .Name
Else: MainForm.chkAddendum = False
End If
End With

chkAddendum_Click_End:
Exit Sub

chkAddendum_Click_Err:
MsgBox "Error#: " & Err.Number & vbCrLf & Err.Description & vbCrLf &
"In
Main Form - chkAddendum_Click"
Resume chkAddendum_Click_End

End Sub

And the section which (usually) pastes it in a different module is this:

Documents.Open FileName:=MainForm.strAddendumSource, _
ReadOnly:=False, _
AddToRecentFiles:=False

Documents(MainForm.strAddendumSource).Selection.Range
rngTemp.WholeStory
rngTemp.Copy
Documents(MainForm.strAddendumSource).Close
Selection.Paste

As I (tried) to explain it usually works but sometimes throws a "bad File
Name" error from a selection of real files!?!

Thanks in anticipation
From the information in your post, it is not possible to even tell what
you
have got right, let alone what is wrong. How about showing us the code
in
[quoted text clipped - 17 lines]
 

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