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 / February 2005

Tip: Looking for answers? Try searching our database.

How to read and write  to an excel spreadsheet using Word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jan Groen - 26 Feb 2005 15:16 GMT
I have set up a small excel sheet containing addresses.

Reading the data is no problem but I want to write one
particular value (last number used) back to the excel
sheet and it keeps creating copies of the original sheet.

Thanks for any tip solving the problem

Kind regards

Jan Groen
The Netherlands

used code below

Dim myObject As Object
Dim i As Integer
Dim myRec As Integer

Dim myName As String
Dim myAddress As String
Dim myCity As String
Dim myDate2 As String
Dim myNumber As Integer

Dim myRange As Range

Const myFile As String = "C:\Documents and
Settings\Petra\Mijn documenten\Administratie\adressen.xls"

Private Sub cmdCancel_Click()
   myObject.Quit
   Unload Me

End Sub

Private Sub cmdOk_Click()
   If lbxAdressen.ListIndex < 0 Then
       MsgBox "Selecteer eerst een naam, klik dan op Ok"
   
   Else
       myRec = lbxAdressen.ListIndex + 2
       myObject.activesheet.Range("b" & myRec).Select
       myName = myObject.activesheet.Range("b" &
myRec).Value
       Set myRange = ActiveDocument.Bookmarks
("Name").Range
       myRange.Text = myName
       ActiveDocument.Bookmarks.Add "Name", myRange
       
       Set myRange = ActiveDocument.Bookmarks
("Name2").Range
       myRange.Text = myName
       ActiveDocument.Bookmarks.Add "Name2", myRange
     
       myObject.activesheet.Range("c" & myRec).Select
       myAddress = myObject.activesheet.Range("c" &
myRec).Value
       Set myRange = ActiveDocument.Bookmarks
("Address").Range
       myRange.Text = myAddress
       ActiveDocument.Bookmarks.Add "Address", myRange

       myObject.activesheet.Range("d" & myRec).Select
       myCity = myObject.activesheet.Range("d" &
myRec).Value
       Set myRange = ActiveDocument.Bookmarks
("City").Range
       myRange.Text = myCity
       ActiveDocument.Bookmarks.Add "City", myRange
       
       myObject.activesheet.Range("e" & myRec).Select
       myDate2 = myObject.activesheet.Range("e" &
myRec).Value
       Set myRange = ActiveDocument.Bookmarks
("Date2").Range
       myRange.Text = myDate2
       ActiveDocument.Bookmarks.Add "Date2", myRange

       Set myRange = ActiveDocument.Bookmarks
("Date").Range
       myRange.Text = DTPicker1.Value
       ActiveDocument.Bookmarks.Add "Date", myRange
             
       Set myRange = ActiveDocument.Bookmarks
("Number").Range
       myRange.Text = txtNumber.Text
       ActiveDocument.Bookmarks.Add "Number", myRange
                                     
       myObject.worksheets("Nummering").Select
       myObject.activesheet.Range("a1").Select
       myObject.activecell.Value = myNumber
       
       myObject.Save
       myObject.Quit
               
       ActiveDocument.Protect Password:="",
NoReset:=False, Type:= _
       wdAllowOnlyFormFields
       
       Unload Me
               
   End If
End Sub

Private Sub UserForm_Initialize()

   i = 2
   Set myObject = CreateObject("Excel.application")
   myObject.workbooks.Open (myFile)

   myObject.worksheets("Nummering").Select
   myObject.activesheet.Range("a1").Select
   myNumber = (myObject.activecell.Value)
   myNumber = myNumber + 1
   
   txtNumber.Text = Right("000" & myNumber, 3)

   myObject.worksheets("adressen").Select
   myObject.activesheet.Range("a2").Select
   
   While Not myObject.activecell = ""
       lbxAdressen.AddItem (myObject.activecell.Value)
       i = i + 1
       myObject.activesheet.Range("a" & i).Select
   Wend
                                                 
   DTPicker1.Value = Date
   
End Sub
Doug Robbins - 26 Feb 2005 22:59 GMT
See the article "Control Excel from Word" at:

http://word.mvps.org/FAQs/InterDev/ControlXLFromWord.htm

Signature

Please respond to the Newsgroup for the benefit of others who may be
interested.   Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP

>I have set up a small excel sheet containing addresses.
>
[quoted text clipped - 126 lines]
>
> End Sub

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.