PDA

View Full Version : Solved: Problem using VBA to Copy/Paste in Word



OldRod
01-09-2007, 07:52 AM
We have run into a problem. We use Visual Foxpro to control Microsoft Word and use it to build a new Word document from pieces of a master Word document.

The master document is divided up with bookmarks and Foxpro steps through a list of items to see which the user has selected and based on that list it then selects the corresponding bookmarked text from the master file, copies it and pastes it into the new document.

Each pasted item is in it's own section so footers/headers can be unique and once we are all done, we print the new document.

This worked fine in Office 2000 and we've used it hundreds of times, but we recently upgraded to Office 2003 and now our paste is acting differently.

Here is the relevant code:


oWdSourceDoc.activate

*** Copy Body ********************
oWdSourceDoc.Bookmarks(mWdBookMark).select
oWord.Selection.Copy
oWdDestDoc.activate
RangeStart = oWdDestDoc.Content.End -1
RangeEnd = oWdDestDoc.Content.End -1
oWord.ActiveDocument.Range(RangeStart,RangeEnd).Select
oWord.Selection.Paste
WITH oWord.Selection
.WholeStory
.Font.Name = "Arial"
.Font.Size = 8
ENDWITH

*** Enter Header Text ********************
IF oWord.ActiveWindow.ActivePane.View.Type = 1 OR oWord.ActiveWindow.ActivePane.View.Type = 2
oWord.ActiveWindow.ActivePane.View.Type = 3
ENDIF
HeaderText = ALLTRIM(ThisForm.Client_Name) + CHR(10) + ALLTRIM(wpname) + CHR(10) _
+ yeDay + ALLTRIM(ThisForm.prog_year)
IF oWord.ActiveWindow.ActivePane.View.Type = 1 OR _
oWord.ActiveWindow.ActivePane.View.Type = 2
oWord.ActiveWindow.ActivePane.View.Type = 3
ENDIF
oWord.ActiveWindow.ActivePane.View.SeekView = 9
WITH oWord.Selection
.WholeStory
.delete
.Font.Name = "Arial"
.Font.Size = 8
.ParagraphFormat.Alignment = 1
.TypeText(HeaderText)
ENDWITH
oWord.ActiveWindow.ActivePane.View.SeekView = 0

*** Enter Footer Text ********************
oWord.ActiveWindow.ActivePane.View.SeekView = 10
WITH oWord.Selection
.wholestory
.delete
.Font.Name = "Arial"
.Font.Size = 8
.ParagraphFormat.Alignment = 2
.TypeText(ALLTRIM(wpnum))
ENDWITH
oWord.ActiveWindow.ActivePane.View.SeekView = 0

*** Create next section *****************
IF NumCreated < NumSelected
oWord.Selection.InsertBreak(2)
NumCreated = NumCreated + 1
oWord.ActiveWindow.ActivePane.View.SeekView = 9
oWord.Selection.HeaderFooter.LinkToPrevious = .F.
oWord.ActiveWindow.ActivePane.View.SeekView = 0
oWord.ActiveWindow.ActivePane.View.SeekView = 10
oWord.Selection.HeaderFooter.LinkToPrevious = .F.
oWord.ActiveWindow.ActivePane.View.SeekView = 0
ENDIF

What's happening now is that all of the pasted text is ending up in the very last section, rather than in each section as the document is built. I'm sure it has something to do with our range we are selecting when doing the paste, or when inserting the next section, but we just can't see the problem - and the fact that it worked in Word 2000, but not 2003 is puzzling.

Can anyone see what we are doing wrong here? Thanks!

*edited to break long lines

fumei
01-09-2007, 11:15 AM
Could you please use the underscore character ( _ ) to break the lines of your code? Otherwise the code is stretched left/right in the code window and it makes it very difficult to read.

Ther are synatx errors as it stands. I copied the code into a code modukle (so I could read it better). There are many instances of IF statements without a THEN.

Are you saying the following worked previously?oWord.Selection.HeaderFooter.LinkToPrevious = .F.

The ".F." seems pretty darn weird to me.

OldRod
01-09-2007, 11:51 AM
Sorry about the long lines - hope I broke them better now.

The code I posted is from FoxPro, so it won't run in Word directly. We are running Word from within FoxPro.

The main part to look at is here:


oWdSourceDoc.Bookmarks(mWdBookMark).select
oWord.Selection.Copy
oWdDestDoc.activate
RangeStart = oWdDestDoc.Content.End -1
RangeEnd = oWdDestDoc.Content.End -1
oWord.ActiveDocument.Range(RangeStart,RangeEnd).Select
oWord.Selection.Paste
WITH oWord.Selection
.WholeStory
.Font.Name = "Arial"
.Font.Size = 8
ENDWITH
The oWord is a handle in FoxPro to control Word.

I'm thinking the RangeStart and RangeEnd are the culprits, but haven't figured out why Word 2000 would run it properly this way and not Word 2003.

Dave
01-09-2007, 05:39 PM
This sounds somewhat familiar. Perhaps this link may be of some use. HTH. Dave
http://www.vbaexpress.com/forum/showthread.php?t=7782

OldRod
01-11-2007, 06:26 AM
That sounded real promising, Dave, but unfortunately it didn't work. I'm wondering if there could be another similar setting that is the culprit though. If I can find a machine in the office that still has Office 2000 on it, I'll compare all the settings and see if anything like that pops out.

fumei
01-11-2007, 07:08 AM
1. No, it still stretches badly. Could you change the following?
IF oWord.ActiveWindow.ActivePane.View.Type = 1 _
OR oWord.ActiveWindow.ActivePane.View.Type = 2
oWord.ActiveWindow.ActivePane.View.Type = 3

2. There is nothing wrong with the "main part" you posted above. But then nothing much is happening with it. You may want to consider changing it using Range (rather than Selection):Dim SourceBMRange As Range
Set SourceBMRange = oWdSourceDoc.Bookmarks(mWdBookMark).Range
SourceBMRange.Copy
With oWdDestDoc
.Activate
.Selection.EndKey Unit:wdStory
.Selection.Paste
End WithFrom what I can see, your code:RangeStart = oWdDestDoc.Content.End -1
RangeEnd = oWdDestDoc.Content.End -1
oWord.ActiveDocument.Range(RangeStart,RangeEnd).Selectis selecting the end of the document. You then paste.

I see no need to select the end of the document. Just go there and paste.

My code above copies the Range of the bookmark in the source doc, and pastes it at the end of the Dest document.

As for:WITH oWord.Selection
.WholeStory
.Font.Name = "Arial"you may want to try being absolutely explicit.
With oWdDestDoc.Selection

Could you reiterate the problem with the Sections though?

OldRod
01-11-2007, 01:50 PM
I can't seem to go back and edit the first post any longer. Sorry :(


Could you reiterate the problem with the Sections though?

The way this is supposed to work is this: the text gets located and copied using the bookmark. In the new document, a new section is inserted and the text is pasted into that new section. Then a header/footer is applied.

What's happening is that all the text is ending up in the last section, instead of each section where it belongs.

It's like we are getting page breaks instead of section breaks, as there are blank pages (with the header/footer of the first section) inserted for each block we were supposed to copy. Then all of those blocks end up one right after the other in the final section.

OldRod
01-11-2007, 03:09 PM
Ah, I finally got it working!! After typing up the last post, I got to thinking, maybe the problem wasn't with the pasting, but with the section break insertion. Sure enough.

I added code to force the selection to the end of the selected text before inserting the section break and it works under 2003 now. Apparently there was a change in behavior between 2000 and 2003 so that the section break insertion works slightly different than before (or maybe it was wrong before and we just got away with it ;)).

Thanks to everyone who helped with this... this has been a tough one :)

fumei
01-12-2007, 09:10 AM
If this is solved for you please mark it as Solved.