Connecting Tech Pros Worldwide Forums | Help | Site Map

VBA in Word

Newbie
 
Join Date: Dec 2008
Posts: 9
#1: Dec 23 '08
Hi,

Can you tell me what is the difference between two codes below:
Expand|Select|Wrap|Line Numbers
  1.     Set wdTable = wdDoc.Tables(2)
  2.     Set wdRange = wdTable.Range
  3.     wdRange.Collapse wdCollapseEnd
  4.     wdRange.Paste
--------------------------
Expand|Select|Wrap|Line Numbers
  1. wdDoc.Tables(2).Range.Collapse wdCollapseEnd
  2. wdDoc.Tables(2).Range.Paste
Why their behaviors not the same?

Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#2: Dec 31 '08

re: VBA in Word


Greetings, markjavascript!

I have no idea; however adding to VBA for a closer look...

Hope you get this one nailed?;-)

In a bit!
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,727
#3: Jan 12 '09

re: VBA in Word


Technically, the two sets of code appear similar to me. Obviously one is using object variables to reference the objects and the other references them specifically each time.

In general, these should be equivalent. However I can think of two issues which may effect this :
  1. Some objects only work properly when referenced via object variables. The only item I'm aware of though is CurrentDB in Access. Not familiar with MS Word environment but it's possible the same restrictions apply there for some objects.
  2. Your code snippet uses a Paste. If the two sets of code are run when different items are available on the clipboard, then you will have different results.
Perhaps, if you described exactly what results you're getting in each test we could be more help, although few of us are as expert in Word as we are in Access.

Welcome to Bytes!
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,727
#4: Jan 12 '09

re: VBA in Word


Alternatively, consider using the "With" approach :
Expand|Select|Wrap|Line Numbers
  1. With wdDoc.Tables(2).Range
  2.   .Collapse wdCollapseEnd
  3.   .Paste
  4.   ...
  5. End With
This is both easier to develop and more efficient in execution.
Reply