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 2008

Tip: Looking for answers? Try searching our database.

Copy data from Excel into a Word variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
XP - 04 Feb 2008 19:13 GMT
Using Office 2003 on Windows XP;

Could someone please post generic example VBA code that would:

1) Activate the MS-Excel file named "XL.xls" (assume this is already open);
2) Copy a string from column "A" of the row which contains the activecell on
sheet named "Program" into a variable;
3) Re-activate MS-Word;

Your help would be greatly appreciated. Thanks much in advance.
Jay Freedman - 04 Feb 2008 20:03 GMT
> Using Office 2003 on Windows XP;
>
[quoted text clipped - 6 lines]
>
> Your help would be greatly appreciated. Thanks much in advance.

This is probably lacking in some necessary error trapping, but it does the
minimum:

Sub demo()
   Dim myString As String
   Dim xlApp As Excel.Application
   Dim xlSheet As Excel.Worksheet

   On Error GoTo NoExcel
   Set xlApp = GetObject(, "Excel.Application")
   Set xlSheet = xlApp.Worksheets("Program")

   On Error GoTo 0
   myString = xlSheet.Cells(xlApp.Selection.Row, 1).Text
   MsgBox myString

   Set xlSheet = Nothing
   Set xlApp = Nothing
   Exit Sub
NoExcel:
   MsgBox "Excel is not running or sheet Program not found."
End Sub

Notice that you _do not_ have to (nor should you) activate the Excel window
and then reactivate the Word window. VBA is perfectly capable of
transferring (not copying, which implies using the clipboard) data from one
document or program to another without those contortions.

See http://www.word.mvps.org/FAQs/InterDev/ControlXLFromWord.htm and
http://www.word.mvps.org/FAQs/InterDev/ControlWordFromXL.htm for more help.

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Jay Freedman - 04 Feb 2008 20:10 GMT
>> Using Office 2003 on Windows XP;
>>
[quoted text clipped - 38 lines]
> http://www.word.mvps.org/FAQs/InterDev/ControlWordFromXL.htm for more
> help.

One more thing: As indicated in bold near the top of the first of those
articles, you have to go into Tools > References and put a check in the box
for the Microsoft Excel Object Library. If you miss that step, VBA will
complain that a "user-defined type is not defined" in the lines that use
Excel.Application and Excel.Worksheet.

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

XP - 04 Feb 2008 20:25 GMT
Just what I needed, thanks!

> >> Using Office 2003 on Windows XP;
> >>
[quoted text clipped - 44 lines]
> complain that a "user-defined type is not defined" in the lines that use
> Excel.Application and Excel.Worksheet.
 
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.