PDA

View Full Version : Solved: excel to Word problem



Brett Desper
04-29-2010, 08:38 AM
I know this is probably absurdly simple, but I am still quite a newbie at most of this code. I need to take a cell value as and integer from excel and manipulate it as an integer in a word macro based upon some conditions. How do I 1) get the value of the C10 cell as an integer into the word macro, (I can manipulate it after that) and then 2) do I need to convert this to a string value before placing it into a Word document? If yes, what is the best way to to that.

Thanks in advance!

Brett

fumei
04-29-2010, 01:58 PM
Let's do #2 first: "do I need to convert this to a string value before placing it into a Word document?"

Technically speaking...no. Word is reasonably clever at handling this. It depends on your "conditions". It may be better to do some converting (hard to say), but generally speaking the data coming from excel is likely numbers, NOT text. In which case, you do not need to convert.

Selection.TypeText "12345" ' (a string)
Selection.TypeText 12345 ' (a number)
can be used in Word interchangeably. Word makes them all strings.

Now #1. It depends on how you are getting/creating your instance of Excel.

Assuming you HAVE an instance of Excel, and you HAVE the Workbook file open, and you HAVE the Worksheet you want active, then....

Dim lngFromExcel As Long

' no doubt other stuff.....


lngFromExcel = xlFile.WorkSheet("Sheet1").Cell("C10").Value

Voila. Your variable lngFromExcel (declared in your Word macro) has been given a value from your Excel file.

NOTE: VBA no longer uses Integers. You can still declare them, but all integers are internally parsed and converted to Long automatically.

Brett Desper
04-29-2010, 02:51 PM
Thanks. I appreciate it. Worked great. :bow:

lynnnow
04-30-2010, 11:58 PM
Please mark your thread solved if you have got the answer.