Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB : Paste From Word To Word -> format ?

Status
Not open for further replies.

Lemox

Programmer
Dec 13, 2003
67
GB
Hi,

Here's the situation :
(- Win 2k
- VB6
- MS Word 2k SP3)

My vb6 app has to produce reports about vehicles.
To do this, it loops x times through a procedure that should insert each vehicle's 'paragraph'.
Maybe 'Paragraph' is not the right word : I mean all data related to ONE vehicle.
I use a template to know how to diplay the data.

First problem :
- if my procedure has to loop about 70 times (70 vehicles), Word uses all my pc's memory and CPU, causing my app to run slower and slower (first 3 vehicles take 1 second to be inserted and around 50th vehicle, it takes 5 seconds for only one !)

So I tried to use a 'temp' doc to write vehicles data AT ONCE then select the whole content of the temp file and paste it in the 'real' doc.
i.e :
-----
open 'real' file
open 'temp' file
while not vehicle.EOF
write vehicle.data in 'temp'
copy 'temp' content to 'real'
clear 'temp' content
Next vehicle
wend
save 'real'
delete 'temp'


Time is much better for long files BUT
Now the problem is that ONE vehicle's data is written line per line... and those lines can be very different.
When I Paste from 'temp' to 'real', EACH LINE APPEARS WITH THE FORMAT OF THE FIRST ONE !

Globally, my question would be :
'How to Copy and Paste from one Word file to another EXACTLY LIKE IT IS ?"

I tried the clipboard.copyformat and .pasteformat commands but they always fail ('no format to copy' or something like that)
Far from being an expert with VBA, I tried the Macro Recorder... bad luck : the code it produces doesn't change anything.

Thanks in advance : any idea welcome !

Lemox
 
1. How is the data for each wehicle format originally?
2. use Styles.
3. is it always the same number of vehicles?
4. are the chunks of data PER vehicle formatted differently from each other? I am assuming yes.
5. How many chunks PER VEHICLE are there?

Possible solution:
A Table style. Have the header row be the vehicle identification (however you are doing that). The following rows formatted as per the Table style, as required.

Have you app create a new table (using the table style) for each vehicle. Insert the data row by row.

Possible solution:
create Bookmarks for each of the chunks of data PER VEHICLE. Have your app replace the Range.Text of the bookmarks with the data per chunk/per vehicle.

Other than that, unless I have real details it is difficult to suggest another solution.

Gerry
 
fumei,

1. In my template I have a line for :
- immatr. number
- brand
- type
- ...
and so on : if the current vehicle has data for the line, it is inserted.

2. Styles ???
3. No, the number of vehicles is not always the same.
4. If by 'chunk' you mean a 'line' like i mean in .1, then yes, they are different.
5. Like I said in .1, the number of 'lines' depends on the data we retrieve about the vehicle so the number of line PER vehicle is variable.

My solution is the second you proposed : I use a Bookmark for each line with a field in it.
If data is found for current line, I first insert the Bookmark then replace the field with the Value.

I can tell you that everything is going fine in the 'temp' doc.
The problem rises only when I Paste its contents to the 'real' doc.
 
If you are putting ??? after Styles, that may be a key. Style determine the format of a paragraph. If the paragraph (any paragraph) has a style attached to it, any text inserted into that paragraph takes up the style.

Could you post some code to show exactly how you are putting the text into the bookmark?

Gerry
 
Gerry,

Thanks for your help : I'll post my code tomorrow 'coz it's stored on our server (I'm currently at home...).

Lemox
 
Gerry,

Here's the code for
1. Insert BookMark
2. Replace the Value Field ([fldToReplace])
3. Reformat whole doc

Code:
   '1-------------------------------------------------------------
Public Function InsertBookmark(strBookmark As String) As Boolean
    
    On Error GoTo InsertBookmark_Error
       
    oWordTemplate.Activate
    oWordTemplate.Bookmarks(strBookmark).Select
    Me.oWordApplication.Selection.Copy
    oWordDocument.Activate
    Me.oWordApplication.Selection.EndKey wdStory
    Me.oWordApplication.Selection.Paste
    oWordDocument.Bookmarks(strBookmark).Select
    
    InsertBookmark = True

End Function


'2--------------------------------------------------------------
Public Sub ReplaceField(strFieldName As String, strValue As String, Optional PasteRTF As Boolean = False _
    , Optional DeleteSelectionIfNull As Boolean = True)
   On Error GoTo ReplaceField_Error
    
    'Vars for pasting rtfText
    Dim lSuccess As Long
    Dim lRTF As Long
    Dim hGlobal As Long
    Dim lpString As Long
    
    'This must be done before ".Execute" because if field is found, .Selection contains only [field]
    'For line with only one field (DeleteSelection = True) field,
    'we want all the line (referenced by current bookmark) to be removed if value is null.
    If strValue = vbNullString Then
        If DeleteSelectionIfNull Then
            Me.oWordApplication.Selection.Delete
            Exit Sub
        End If
    End If
    
    With Me.oWordApplication.Selection.Find
                                            
        .Forward = True                     
        .Wrap = wdFindAsk                  
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute findtext:="[" & strFieldName & "]"
    End With

    If Me.oWordApplication.Selection.Find.Found Then
        If PasteRTF Then
            'Paste Remark.Text : Rich-Formatted
            lSuccess = OpenClipboard(frmDossier.hwnd)
            lRTF = RegisterClipboardFormat("Rich Text Format")
            lSuccess = EmptyClipboard
            hGlobal = GlobalAlloc(GMEM_MOVEABLE Or GMEM_DDESHARE, Len(strValue))
            lpString = GlobalLock(hGlobal)

            CopyMemory lpString, ByVal strValue, Len(strValue)
            GlobalUnlock hGlobal
            SetClipboardData lRTF, hGlobal
            CloseClipboard
            GlobalFree hGlobal
            oWord.oWordApplication.Selection.Paste
        Else
            If LenB(strValue) <> 0 Then
                Me.oWordApplication.Selection.TypeText strValue
            Else
                Me.oWordApplication.Selection.Delete
            End If
        End If
    End If
End Sub


'3---------------------------------- When the report is complete, do this :

For Each opendoc In .oWordApplication.Documents
        If opendoc.Name = oWord.oWordDocument.Name Then
            With opendoc
                .PageSetup.LeftMargin = oWord.oWordTemplate.PageSetup.LeftMargin
                .PageSetup.RightMargin = oWord.oWordTemplate.PageSetup.RightMargin
                .PageSetup.TopMargin = oWord.oWordTemplate.PageSetup.TopMargin
                .PageSetup.BottomMargin = oWord.oWordTemplate.PageSetup.BottomMargin
                .PageSetup.FooterDistance = oWord.oWordTemplate.PageSetup.FooterDistance
                .PageSetup.PaperSize = oWord.oWordTemplate.PageSetup.PaperSize
                .Sections(1).Footers(wdHeaderFooterPrimary).Range.FormattedText = oWord.oWordTemplate.Sections(1).Footers(wdHeaderFooterPrimary).Range.FormattedText
                SendKeys "^{HOME}"
                oWord.AlignAllTablesLeft
            End With
        End If
Next opendoc

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top