![]() |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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] |
|
#3
|
|||
|
|||
|
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
Last edited by macropod; 03-08-2017 at 12:47 AM. Reason: Added code tags & formatting |
|
#4
|
||||
|
||||
|
Quote:
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] |
|
#5
|
|||
|
|||
|
Ok, thanks a lot. These quirks of programming style sometimes make newbies crazy
![]() Cheers! |
|
#6
|
|||
|
|||
|
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
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
Last edited by gmaxey; 03-08-2017 at 03:30 PM. |
|
#7
|
|||
|
|||
|
Thanks both for your reply. Don't be annoyed
![]() I will take a closer look to your code later. HAve a nice day. cu |
|
| Thread Tools | |
| Display Modes | |
|
|
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 |
Find if Date range falls within another range
|
Triadragon | Excel | 3 | 05-02-2016 11:48 AM |
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 |
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 |