Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
doctorjones_md - 10 Nov 2006 23:11 GMT
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
Jezebel - 11 Nov 2006 01:13 GMT
Setting ExportDoc to nothing clears the variable. You first need to close
the document --

ExportDoc.Close

> Jezebel,
>
[quoted text clipped - 109 lines]
>
> Shane
doctorjones_md - 11 Nov 2006 04:13 GMT
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
=========================================
> Setting ExportDoc to nothing clears the variable. You first need to close
> the document --
[quoted text clipped - 114 lines]
>>
>> Shane
doctorjones_md - 11 Nov 2006 04:28 GMT
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
==================
> Jezebell,
>
[quoted text clipped - 132 lines]
>>>
>>> Shane

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.