Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-14-2012, 03:08 PM
jsb73 jsb73 is offline Use of Range Object Question Windows 7 32bit Use of Range Object Question Office 2007
Novice
Use of Range Object Question
 
Join Date: Aug 2012
Posts: 4
jsb73 is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 08-14-2012, 03:15 PM
macropod's Avatar
macropod macropod is offline Use of Range Object Question Windows 7 64bit Use of Range Object Question 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

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]
Reply With Quote
  #3  
Old 08-14-2012, 04:07 PM
jsb73 jsb73 is offline Use of Range Object Question Windows 7 32bit Use of Range Object Question Office 2007
Novice
Use of Range Object Question
 
Join Date: Aug 2012
Posts: 4
jsb73 is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 08-14-2012, 04:22 PM
macropod's Avatar
macropod macropod is offline Use of Range Object Question Windows 7 64bit Use of Range Object Question 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

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]
Reply With Quote
  #5  
Old 08-14-2012, 04:51 PM
jsb73 jsb73 is offline Use of Range Object Question Windows 7 32bit Use of Range Object Question Office 2007
Novice
Use of Range Object Question
 
Join Date: Aug 2012
Posts: 4
jsb73 is on a distinguished road
Default

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?
Reply With Quote
  #6  
Old 08-14-2012, 05:35 PM
macropod's Avatar
macropod macropod is offline Use of Range Object Question Windows 7 64bit Use of Range Object Question 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 jsb73 View Post
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?
Pretty much, yes. Of course, if you have many paragraphs to insert and most of them are to be in the same Style, you might start off by applying that Style to the whole document, insert all the text, then apply the alternative Styles to the paragraphs that need them. There are numberous possible approaches, none of which is necessarily 'right' and the others 'wrong'; it all comes down to what's easiest to develop, maintain and execute. The important point, though, is to avoid overriding Style formats - doing so can create all sorts of problems for users later on.

PS: There's an .InsertBefore method too.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 08-14-2012, 07:35 PM
jsb73 jsb73 is offline Use of Range Object Question Windows 7 32bit Use of Range Object Question Office 2007
Novice
Use of Range Object Question
 
Join Date: Aug 2012
Posts: 4
jsb73 is on a distinguished road
Default

Thanks, Paull. You have helped me a lot.
Jim
Reply With Quote
Reply

Tags
range, vba word



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use of Range Object Question Range.Information(wdStartOfRangeRowNumber): Application-defined or Object-defined err tinfanide Excel Programming 2 06-09-2012 10:19 AM
Use of Range Object Question 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:08 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