Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-07-2017, 11:29 AM
Serpico Serpico is offline Word.Range Windows 10 Word.Range Office 2016
Novice
Word.Range
 
Join Date: Mar 2017
Posts: 4
Serpico is on a distinguished road
Default Word.Range

Hello everybody,



I have been trying for some time to get the basics of vba. Well, it is not that easy
Actually, I am trying to write a userform in Word in VBA. In some up looked examples I found these statements:

Dim var as Range & Dim var as Word.Range

I am looking now for several days an explanation, but without any success.

Can anybody tell me he real meaning of word.range or the difference to simply use range?

Thanks in advance
RR
Reply With Quote
  #2  
Old 03-07-2017, 02:16 PM
macropod's Avatar
macropod macropod is offline Word.Range Windows 7 64bit Word.Range Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Ordinarily, you'd only prefix a variable's type with the application name if the code will or might be run from a different application - especially if both applications have the same variable type. For example, both Word and Excel have Range objects. So, if the code was being run from Excel, you'd need to indicate when a range variable refers to a Word range to avoid it being treated as a reference to an Excel range.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-08-2017, 12:22 AM
Serpico Serpico is offline Word.Range Windows 10 Word.Range Office 2016
Novice
Word.Range
 
Join Date: Mar 2017
Posts: 4
Serpico is on a distinguished road
Default

Hello macropod,

Thank you for you reply.
If I understand you right, the prefix of the variable type range (in this case), is only to specify the application your running from (word.range -> word app)? Ok, that sounds clear for me. But look below on the example (it is from Gerg Maxey):
Why does he refer to the word app? His code is to be used anyhow in VBA Word?

Code:
Sub InsertInBookmarkIII()
Dim oRng As Word.Range
   Set oRng = ActiveDocument.Bookmarks("bmInBookmark").Range
   oRng.Text = InputBox("What is your favorite color?")
   ActiveDocument.Bookmarks.Add "bmInBookmark", oRng 
lbl_Exit:
   Exit Sub
End Sub
Thank you

Last edited by macropod; 03-08-2017 at 12:47 AM. Reason: Added code tags & formatting
Reply With Quote
  #4  
Old 03-08-2017, 12:50 AM
macropod's Avatar
macropod macropod is offline Word.Range Windows 7 64bit Word.Range Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Serpico View Post
But look below on the example (it is from Gerg Maxey)
Why does he refer to the word app? His code is to be used anyhow in VBA Word?
You should ask him. It may be just a quirk of his programming style - as is the quite unnecessary use of:
lbl_Exit:
Exit Sub

PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 03-08-2017, 12:57 AM
Serpico Serpico is offline Word.Range Windows 10 Word.Range Office 2016
Novice
Word.Range
 
Join Date: Mar 2017
Posts: 4
Serpico is on a distinguished road
Default

Ok, thanks a lot. These quirks of programming style sometimes make newbies crazy

Cheers!
Reply With Quote
  #6  
Old 03-08-2017, 05:50 AM
gmaxey gmaxey is offline Word.Range Windows 7 32bit Word.Range Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Serpico,

Each of us has our own style of coding and our own style of posting. Some seem to take pleasure in pointing out the quirks or flaws in others and some don't.

Dim Word.Range as Range and Dim Range contained in the same project executed in Word is the same thing. Sometime I use one, sometimes the other. I guess I'm just not very ordinary.

Paul is very rarely in error. Here again he is true to form, both correct and opinionated. Technically, in the example above, lbl_Exit followed by Exit Sub is certainly unnecessary.

However, opinions aside, those two lines of code are in the autotext I use when starting a scratch procedure.

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey

lbl_Exit:
Exit Sub
End Sub

... and I "ordinarily" use those two lines of code as a flag to myself that I have finished a review of existing code and as a placeholder for error escaping.

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDoc As Document
  On Error GoTo Err_Handler
  Set oDoc = Documents(2)
lbl_Exit:
  Exit Sub
Err_Handler:
  Select Case Err.Number
    Case Is = 5941: MsgBox "There is no document 2."
    Case Else
  End Select
  Resume lbl_Exit
End Sub
Here is something for you to ponder:

Code:
Sub A()
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  oRng.Text = "Find me. "
  With oRng.Find
    .Text = "Find me."
    While .Execute
      If .Found Then
        MsgBox "Some choose to use use .Found. Is it neccesary or just a quirk?  You decide."
        oRng.Text = "Found"
      End If
    Wend
  End With
End Sub
Sub B()
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  oRng.Text = "Find me. "
  With oRng.Find
    .Text = "Find me."
    While .Execute
     oRng.Text = "Found"
    Wend
  End With
lblExit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by gmaxey; 03-08-2017 at 03:30 PM.
Reply With Quote
  #7  
Old 03-08-2017, 09:36 AM
Serpico Serpico is offline Word.Range Windows 10 Word.Range Office 2016
Novice
Word.Range
 
Join Date: Mar 2017
Posts: 4
Serpico is on a distinguished road
Default

Thanks both for your reply. Don't be annoyed
I will take a closer look to your code later.
HAve a nice day.
cu
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Distribute text in one cell across a range of cells (overcoming selection.range.cells.count bug) slaycock Word VBA 0 02-18-2017 07:00 AM
Word.Range Find if Date range falls within another range Triadragon Excel 3 05-02-2016 11:48 AM
Word.Range Name a Range in a Word Document and then copy that range to the end of the doc w button click DanNatCorning Word VBA 1 04-29-2016 10:47 PM
Word.Range Set Excel Range using Word gbrew584 Word VBA 3 12-07-2015 01:51 PM
Through VBA, export range from Excel to Word duugg Word VBA 0 08-24-2009 07:50 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:31 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft