Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Loosing formatting problem

    I am loosing my text formatting and I do not know why or how to fix.

    My code is as follows:

    Dim strCopiedText as String

    With Selection
    'Selection options
    End Selection
    strCopiedText = Selection.Range

    Documents.Add DocumentType:=wdNewDocument
    ActiveDocument.Range.SetRange Start:=wdEndOfDocument End:=wdEndOFDocument
    Selection.TypeText strCopiedText
    Selection.TypeParagraph


    End of Code...

    I need to keep the formatting, can anyone help?

    Thanks

    James

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loosing formatting problem

    James,

    Try this:

    Dim rngFrom As Range
    Set rngFrom = Selection.Range
    Documents.Add ' type wdNewBlankDocument is the default

    ' Bookmark doc is a built-in bookmark referring to the whole document

    I've also not used "Selection" when pasting, as working with a range is faster (andcleaner) the working with a selection


    ActiveDocument.Bookmarks("doc").Range.FormattedTex t = rngFrom.FormattedText
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loosing formatting problem

    Thanks - it works!

    Problem now, how do you release the resources that this method uses? My macro has to loops through this section of code 50 or (usually) more times and it destroys the rrsource levels and hangs the computer.

    Thanks so far!

    James

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Loosing formatting problem

    Geoff,

    That "Range.FormattedText" looks very useful - I'm going to use that.

    If you don't object, here is a selection from a range of alternative methods:

    <pre>Sub DocRangeTransfer1()
    Dim OrigRng As Range
    Dim NewDoc As Document
    Set OrigRng = ActiveDocument.Range
    Set NewDoc = Documents.Add
    NewDoc.Range.FormattedText = OrigRng.FormattedText
    End Sub
    </pre>

    <pre>Sub DocRangeTransfer2()
    Dim OrigDocPath As String
    Dim NewDoc As Document
    OrigDocPath = ActiveDocument.FullName
    Set NewDoc = Documents.Add
    NewDoc.Range.InsertFile FileName:=OrigDocPath
    End Sub
    </pre>

    (James, when you use a string to bring text across, you will lose formatting because a string contains only text and no formatting.)

    Gary

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Loosing formatting problem

    Hi James,
    (Looks like we posted about simultaneously.)

    Setting a reference to the range object over and over again in a loop shouldn't cause a resource problem per se, because you still only have the one object, just reassigning it each time (unless I'm mistaken, in which case I hope someone will set me straight!).
    So there may be something else going on, that's causing the resource issue.
    But if you want to destroy the range object (release the memory) each time just to be safe, you can use:

    Set rngFrom = Nothing

    at the end of each loop.

    Gary

  6. #6
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loosing formatting problem

    Gary, thanks very much for the examples.

    One question about the first one.... How would I set the range in the first document? (Its a number of reports in a single document, each end with ^~ to mark end of report)

    Thanks for the wonderful response so far!

    James

  7. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Loosing formatting problem

    Hi again,

    Do you need to just set one range for the entire contents of the first document (which is what that sample code does), or do you need to set a number of individual ranges, each corresponding to a single report?

    If the latter, it would be trickier, but there would still be some ways to do it. One way would be to first select each report, using a Selection.Extend method, which lets you extend the selection to a specific character, and assign a unique range object to each selection in turn - this is handy, but as Geoff might point out, might be a little slow.

    Another method would be if you have a convenient way to define a bookmark for each report, encompassing the full range of each report.
    You can then use the InsertFile method, and specify the particular bookmark name, as the range to be inserted.
    As in code like the following:

    <pre>Sub DocRangeTransfer3()
    Dim OrigDocPath As String
    Dim NewDoc As Document
    OrigDocPath = ActiveDocument.FullName
    Set NewDoc = Documents.Add
    NewDoc.Range.InsertFile OrigDocPath, "bmkReportOne"
    End Sub
    </pre>

    This last method is a very powerful and flexible way to get content from one file, into another file.

    Gary

  8. #8
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loosing formatting problem

    What the main document is, is an export of statements to a number of people from a financial system to a single RTF document. SO it would be a number of individual ranges.

    My code now looks as follows:

    Dim rngFrom as Range

    With Selection
    .Collapse Direction:=wdCollapse
    .MoveEndUntil Cset:="^~", Count:=wdForward
    .MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
    End With

    Set rngFrom = Selection.Range

    Documents.Add

    ActiveDocument.Bookmarks("doc").Range.FormattedTex t = rngFrom.FormattedText

    'I then have code to construct the filename and to save the file.

    Set rngFrom = Nothing

    End of Code.

    My problem is that this still eats the resources and doesn't release them before the macro is finished, which is too late of course.

    Any suggestions?

    James

  9. #9
    Star Lounger
    Join Date
    Jan 2001
    Location
    Adelaide, South Australia
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loosing formatting problem

    Could your resource problem be associated with the new documents you're creating rather than the ranges?

    How about:
    Set newDoc = new Document
    ... code using newDoc rather than ActiveDocument ...
    Set newDoc = nothing

    This should clear up after the document every time.

    Jon

  10. #10
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loosing formatting problem

    James,

    it may well be the use of "Selection" again which takes resources. I've just had a quick play with "range" though- it works differently, and I've run out of time just now.

    But try running the program with :

    Set rngFrom = ActiveDocument.Content
    Documents.Add
    ActiveDocument.Bookmarks("doc").Range.FormattedTex t = rngFrom.FormattedText

    That at least may help isolate whether it's the use of select (plus the moving around in the selection) which is causing the problem, or whether it's the document creation and copying.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Loosing formatting problem

    Selection may cause code to run slower in certain circumstances, but it's very unlikely to cause a degradation in resources. If the resource hit increases, the more times the loop runs, then it's not about Selection.

    It's more likely, as raised in the other branch of this thread, that the problem has to do with the creation of so many new documents - this could cause a performance hit, particularly if run on Word 2000, since a new Word 'process' is started for each document.

    It would probably be a good idea to have the code save, name and close each new document as it is created, before continuing on to the next iteration of the loop.

  12. #12
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loosing formatting problem

    No, thats not the problem.

    Once the original posting is read, I'm sure you'll come to the same conclusion.

  13. #13
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loosing formatting problem

    Gary is quite correct about the Selection code. The example of my code which I used in the post, didn't including the formatting for the text being "copied" or being assigned to the new document and I did NOT have a resource problem with it.

    Once the code was changed to accomodate the formatting, the resource issue cropped up.

    The resource issue doesn't really seriously show it's head until you get to about page 30. And this is after each new document is saved and closed after it is created.

  14. #14
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loosing formatting problem

    Gary,

    Is there a VB(A) or API call to free up resources?

    James

  15. #15
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loosing formatting problem

    James,

    Do you think you might be able to attach a (cut-down if necessary) Word document with full code- one which exhibits the problem? It may help us to get a solution for you faster.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •