Wrox Programmer Forums
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2011, 10:07 AM
Authorized User
 
Join Date: May 2011
Posts: 44
Thanks: 7
Thanked 0 Times in 0 Posts
Default error 5850

Hi,

I have an error which I cannot find any info on. It is error 5850 ('The Specified range is not from the correct document or story')

I am trying to transfer data

The error appears directly above the ?????????????????????? (the 2 lines below I commented out - see later):

Code:
Dim rngSupply As Word.Range
Set rngSupply = ActiveDocument.Bookmarks("bmSupply").Range
Dim rngSupplyBody As Word.Range
Set rngSupplyBody = ActiveDocument.Bookmarks("bmSupplyBody").Range
Dim rngSupplyBreak As Range
Set rngSupplyBreak = ActiveDocument.Bookmarks("bmSupplyBreak").Range
Call RetrieveExcelData
    
    If Me.chkSupply.Value = True Then
        rngSupply.Text = vbNewLine & "Scope of Supply" & vbNewLine
        ActiveDocument.Bookmarks.Add "bmSupply", rngSupply
         ???????????????????????????????????????????????????????
        
        rngSupplyBody.Text = docSupply.Bookmarks("test").Range & vbNewLine
        ActiveDocument.Bookmarks.Add "test", rngSupplyBody
        
        rngSupplyBreak.Select
        Selection.Range.InsertBreak (wdPageBreak)
        ActiveDocument.Bookmarks.Add "bmSupplyBreak", rngSupplyBreak

As I stated above I commented out the 2 lines but that didn't do anything. I then commented out the code below and the error disappeared:

Code:
Function RetrieveExcelData()
 'Scope of Supply Document
    Public appSupply As Word.Application
    Public docSupply As Word.Document

    Set appSupply = Word.Application
    Set docSupply = appSupply.Documents.Open("H:\proposalDocumentDevelopment\proposalTemplates\scopeOfSupply.docx")

End Function
Can anybody help? I might have just written the wrong transfer code for inserting data from one word dcoument to another.

Thanks
 
Old June 24th, 2011, 07:30 AM
Authorized User
 
Join Date: May 2011
Posts: 44
Thanks: 7
Thanked 0 Times in 0 Posts
Unhappy Almost Solved

I also used code from this website instead:
http://www.wiredbox.net/forum/Thread...o_another.aspx

The same error came up until I realised that the ActiveDocument was actually the one I had just opened to retrive the data from so it was looking for the bookmark in the new document.

However, I cannot re-insert the bookmark using this code and the old code I was using still comes up with the error (even after rearranging).

Does anybody have any ideas? Either way is fine as long as though it works!

Thanks

Last edited by cjcant; June 24th, 2011 at 10:09 AM.. Reason: Thought it was solved bt not quite
 
Old June 24th, 2011, 10:35 AM
Authorized User
 
Join Date: May 2011
Posts: 44
Thanks: 7
Thanked 0 Times in 0 Posts
Default result has changed slightly

Hi,

I have included the code below:

Quote:
Dim rngSupplyBody As Word.Range
Set rngSupplyBody = ActiveDocument.Bookmarks("bmSupplyBody").Range

If Me.chkSupply.Value = True Then

'Import Scope of Supply Doc
Dim appSupply As Word.Application
Dim docSupply As Word.Document
Set appSupply = Word.Application
Set docSupply = appSupply.Documents.Open("H:\proposalDocumentDevel opment\proposalTemplates\scopeOfSupply.docx")

rngSupplyBody = docSupply & vbNewLine
''Re-insert the bookmark
ActiveDocument.Bookmarks.Add "bmSupplyBody", rngSupplyBody
I do not know why but the text being inserted is 'scopeOfSupply.docx' instead of the data within the document.

The bookmark is also not re-inserting but that might be a derivitative of the insert problem.

Thanks
 
Old June 28th, 2011, 04:59 AM
Authorized User
 
Join Date: May 2011
Posts: 44
Thanks: 7
Thanked 0 Times in 0 Posts
Default Almost Solved: formatting not applied

Hi,

This works except for the formatting:

Code:
    'Body
        'Import Scope of Supply Doc
        Dim appSupply As Word.Application
        Dim docSupply As Word.Document
        Set appSupply = Word.Application
        Set docSupply = appSupply.Documents.Open("H:\proposalDocumentDevelopment\proposalTemplates\scopeOfSupply.docx")
    
        
        rngSupplyBody = docSupply.Bookmarks("test").Range.FormattedText & vbNewLine
        
        docSupply.Close wdDoNotSaveChanges
        
        ''Re-insert the bookmark
        ActiveDocument.Bookmarks.Add "bmSupplyBody", rngSupplyBody
        
        
        ''Re-insert the bookmark
        ActiveDocument.Bookmarks.Add "bmSupplyBody", rngSupplyBody
Is anybody able to tell me how to keep the formatting from the previous document (where the data has come from)?

Thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
HTTP error 500: Internal Server Error in Load Testing sherin Visual Studio 2008 0 May 19th, 2010 09:02 AM
Remote Connection Error: Named Pipes Provider error hbansal ASP.NET 2.0 Basics 5 April 12th, 2009 09:51 AM
Insert Query Error & Run-Time Error 3022 DavidWE Access 1 July 31st, 2008 11:17 AM
Ch 4: Parse error: syntax error, unexpected T_SL hanizar77 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 June 23rd, 2008 09:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.