Repost from Nov 3rd How do you close a document (with VBA code) that you have opened

D

doctorjones_md

Jezebel,

Thank you for all your help. I've been able in incorporate your code into
my project, but I seem to be getting the following error message when I run
it:
"Run-time error 4605" This method or property is not available because no
text is selected

When I get the error message, if I choose debug, the following line is
highlighted:

pRange.Copy

If I Reset press the Reset button in the menu bar of VBE, then the data from
the seleted Row is inserted into Document1

I get the error message when I run routine #1 below, but not when I run
routine #2 (see below)

Could you please tell me how I can resolve this issue? Here is the code
I'm using:

#1 'Code for frmProducts:
Option Explicit

Dim pTable1 As Table
Dim pTable2 As Table
Dim pIndex As Long
Dim pRange As Word.Range
Dim ExportDoc As Word.Document '**** NOTE -- I set these dimensions
outside for the Sub (as in your example) so I could use it for several
subs/options on the form

Private Sub cbxLinen_Click()

Set ExportDoc = Documents.Open("E:\Products\Linen.doc") '*** This Opens
the file Linen.doc as ExportDoc Projects -- How do write the code to close
ExportDoc after the data has been inserted into Document1?****

Set pTable1 = ExportDoc.Tables(1)
Set pTable2 = Documents("Document1").Tables(12) '**** I want the data
from ExportDoc.Tables(1) to be inserted into Table 12 of Document1

If Me.cbxLinen.Value = True Then

Me.cbxNotIncluded.Value = False '***** This is the default value of
frmProducts

pTable1.Rows.Add BeforeRow:=pTable2.Rows(3) 'Adds a NEW row in the
destination table -- active document

For pIndex = 1 To pTable1.Columns.Count
Set pRange = pTable1.Cell(7, pIndex).Range 'The row in the Table you
want to import
pRange.End = pRange.End - 1
pRange.Copy '**** This is the line that's highlighted during debug
pTable2.Cell(3, pIndex).Range.Paste

Next

Me.cmdOK.Enabled = True

End If

Set ExportDoc = Nothing '**** I thought this might close ExportDoc, but it
does nothing -- any thoughts?

End Sub

==============================================
BUT -- everything works fine on this form (with identical code) -- EXCEPT
that the file ExportDoc doesn't close after copying the Rows to Document1
==============================================
#2 'Code for frmServices:
Option Explicit

Dim pTable1 As Table
Dim pTable2 As Table
Dim pIndex As Long
Dim pRange As Word.Range
Dim ExportDoc As Word.Document

Private Sub cbxDelivery_Click()

Set ExportDoc = Documents.Open("H:\Services\Delivery.doc")
Set pTable1 = ExportDoc.Tables(1)
Set pTable2 = Documents("Document1").Tables(8) 'Sets Table 8 as the
insertion point

If Me.cbxMS_IDE.Value = True Then

pTable1.Rows.Add BeforeRow:=pTable2.Rows(3) 'Sets the insertion
point before Row 3
For pIndex = 1 To pTable1.Columns.Count
Set pRange = pTable1.Cell(2, pIndex).Range 'Selects Row 2 in
ExportDoc
pRange.End = pRange.End - 1
pRange.Copy
pTable2.Cell(3, pIndex).Range.Paste 'Pastes data in Row 3 of Table 8
in Document1
Next

Me.cmdOK.Enabled = True

End If

Set ExportDoc = Nothing

End Sub

Much Thanks in Advance

Shane
 
J

Jezebel

Setting ExportDoc to nothing clears the variable. You first need to close
the document --

ExportDoc.Close
 
D

doctorjones_md

Jezebell,

Thank you so much for your expertise-- that did the trick in resolving the
problem I was having in getting ExportDoc to close -- thank you.

By any chance (in comparing the 2 samples of code in #1 and #2 -- from my
previous post), do you have any idea why the code works in Private Sub
cbxDelivery_Click() on frmServices, but errors in Private Sub
cbxLinen_Click() on frmProducts?

As I explained -- when I get the error message, if I press the Reset button
in the menu bar of VBE, then the data from the seleted Row is inserted into
Document1.

Shane
=========================================
 
D

doctorjones_md

Jezebell,

I found the problem -- WOW, this is an incredible learning experience --
thank you so much for the amount of hand-holding you've given me -- I need
to modify the remainer of my forms to match what I know have -- keeping
fingers crossed -- will let you know how it turns out. Thanks again!

Shane
==================
 

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