Error 91 Copying to Word Table

  • I’m using this code to copy a range of cells from an Excel sheet to a template table in Word.


    When I try to copy the data from a sheet cell to a Word table cell, appears error 91


    Any help ?


    Thanks, everbody,
    Marta



    This is my code...


  • Re: Error 91


    Hi Marta,

    Welcome to the OzGrid forum.

    Two things:

    1) Your document object 'wDoc' does not appear to have been assigned to anything, although it is commented out in a statement that assigns it to a new document. I assume this may be part of your testing of the problem and that wDoc should be assigned to a document added.

    2) If the statement to assign wDoc to a new document was reinstated into your program, you would not need to use the 'appWord' object as wDoc would already have been assigned to refer to a new document within the appWord object. Even if you were to use the appWord object it would need to appear before the document object so that you used An 'application.object' structure, rather than 'object.application'.

    Do you want to assign wDoc to the document that you open, rather than to a new document? If so, use

    Code
    Set wDoc = appWord.Documents.Open(Filename:=NomPlantilla)


    then subsequently

    Code
    wDoc.Selection.TypeText Text:=Membrete


    Hope this helps.

    [COLOR="Purple"]Regards,[/COLOR]
    [COLOR="Purple"]Batman.[/COLOR]

  • Re: Error 91


    Dave,

    I suspect that the thread was originally in the correct forum. It looks as though this is an Excel application that is attempting to control Word.

    Looking at it again, I suspect that what is missing is a reference to the relevant Word object library, as Bocanegra has had to comment out statements that would otherwise have worked, i.e.

    Code
    [COLOR=darkgreen]'Set appWord = New Word.Application[/COLOR]
         [COLOR=darkgreen]'Set wDoc = appWord.Documents.Add()[/COLOR]


    and has had to use an 'Object' variable, rather than a 'Word.Application' variable for appWord.

    [COLOR="Purple"]Regards,[/COLOR]
    [COLOR="Purple"]Batman.[/COLOR]

  • Re: Error 91


    Hi Batman


    No, the Excel forum is for Excel/VBA questions only. The Excel/Word forum is for questions pertaining to Word and Excel, or Word only.


    This is detailed here: Main Question Help Forums

  • Re: Error 91


    Dave,

    Apologies. I so rarely go to the other forums that I had assumed that Word related to Word and that the correct forum would be the one that was actually controlling the application.

    Perhaps I ought to spend a little more time exploring the forum instead of just attempting to answer questions!

    [COLOR="Purple"]Regards,[/COLOR]
    [COLOR="Purple"]Batman.[/COLOR]

  • Re: Error 91


    Careful, Dave, you're making me blush!

    Marta, please do not send me private e-mails asking for help on a specific question. I have copied your problem into the forum and will see if I can get to look at it here, but I am extremely busy with work at the moment.

    If you need to, you can attach a sample of your workbook on the forum.

    Marta's problem (edited from the original e-mail to me):
    *********
    I have started with excel VBA a few months ago, but I have not enough experience with automation from Excel to Word.


    I have changed my code with your changes, but still it doesn't work. My workbook is an application for printing the number of labels you want to for each customer in the table of sheet DATA.


    What I want to do is:


    1) Create a range of cells in an excel sheet.
    2) Each cell in this range has the data of a label (Name, direction, zip code, City.)
    3) When this range is full, I want to send it to a Word template (a table). I want to copy the first cell in the sheet range into the first cell in the Word template, the second cell in the sheet range into the second cell in the Word template and so on.
    4) When all cells are copied, I want to print the Word template which contains the labels data.
    *********

    [COLOR="Purple"]Regards,[/COLOR]
    [COLOR="Purple"]Batman.[/COLOR]

  • Re: Error 91 Copying to Word Table


    Thank you for your help, Batman.


    I can't upload may workbook, because its filesize is too bigger. Well, I have tried your solution and now appears error 438 in this piece of code


    wDoc.Selection.TypeText Text:=Membrete


    Thanks again for your interest


    Marta

  • Re: Error 438 Copying to Word Table


    Hello, friends



    Here is a small example of my workbook. And the piece of code where error 438 appears.


    For printing labels I do this:


    1) I copy the customer's data (sheet DATA) into the range HojaEtiquetas (blue area in sheet LABELS).


    2) With the code below, I copy the contents of range HojaEtiquetas into a Word table template.





    Thanks everybody again


    Marta

  • Re: Error 438 Copying to Word Table


    Although I don't have your Word template workbook to work with, I believe that these two statements should work:

    Code
    appWord.Selection.TypeText Text:=Membrete
    appWord.Selection.MoveRight Unit:=wdCell, Count:=1, Extend:=wdMove


    Hope this helps.

    [COLOR="Purple"]Regards,[/COLOR]
    [COLOR="Purple"]Batman.[/COLOR]

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!