#1
|
|||
|
|||
Use of Range Object Question
In Access I create a Word document for reports. I have used Selection.TypeText to write to the Word document. I understand that using the Range object would make it faster. Would using the ActiveDocument.Range improve the speed of execution, or should I try to use a Range for each paragraph? If so how do I add a paragraph and use that paragraph's range. I have looked and looked and can't seem to find code that works. |
#2
|
||||
|
||||
Hi jsb,
In most cases, working with ranges where practical (and it almost always is unless you have a reason to modify something based on the fact of its selection) is far more efficient than making and working with selections. Whether working with 'ActiveDocument.Range' or a specific paragraph range (or some other range specification) is appropriate depends on what you're trying to do. Without seeing the code, I can't say more than that.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks Paul,
In the following code, how is the optimal way to add another paragraph to the document using the Range object if that is the most efficient way? Code:
Dim wo As Word.Application Set wo = CreateObject("Word.Application") Dim woDoc As Word.Document Dim woRng As Word.Range With wo .WindowState = wdWindowStateMaximize .Documents.Add Set woDoc = wo.ActiveDocument Set woRng = woDoc.Range With woRng .Font.Bold = True .Font.Size = 12 .Paragraphs.Alignment = wdAlignParagraphCenter .InsertAfter "Hello Ranger Again" End With End With ActiveDocument.SaveAs "C:\LookingAtWord\Ranger.doc" wo.Visible = True Last edited by macropod; 08-14-2012 at 04:15 PM. Reason: Added code tags & formatting |
#4
|
||||
|
||||
OK, with your code, you're formatting the whole document with 12pt bold text, centred paragraphs. Is that what you really want?
It is always better to use appropriately-defined paragraph Styles for managing document formatting, rather than overriding an existing Style with the format you want. FWIW, to insert another paragraph is as simple as adding another line with: .InsertAfter vbCr & "My New paragraph" Then, of course, you'd want to format the new paragraph, for which you could use code like: .Paragraphs.Last.Range.Style = "Some Style"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Thanks again Paul. I am still a bit confused. Are you saying it is best not to sart with fomatting the Document.Range, but better to start by adding paragraphs using InsertAfter then adding the paragraph styles for each paragraph?
|
#6
|
||||
|
||||
Quote:
PS: There's an .InsertBefore method too.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Thanks, Paull. You have helped me a lot.
Jim |
Tags |
range, vba word |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err | tinfanide | Excel Programming | 2 | 06-09-2012 10:19 AM |
use VBA to name a range | g48dd | Excel Programming | 7 | 06-20-2011 06:34 AM |
Problem: object library invalid or contains references to object definitions | aligahk06 | Office | 0 | 08-19-2010 12:29 PM |
Could not load this object as this object is not present in your computer | k.gaurav | Office | 0 | 08-17-2009 09:57 PM |
Categories question & replying with attachment question | glitzymama | Outlook | 0 | 03-15-2006 09:32 AM |