Consulting

Results 1 to 3 of 3

Thread: VBA Copy tables from excel to Wd

  1. #1
    VBAX Newbie
    Joined
    Jan 2019
    Posts
    1
    Location

    VBA Copy tables from excel to Wd

    Hi! I am new to VBA and I have a question. I have the following code, with it to copy a table which has a header (A1 to F1) from each sheet (GestiuneCCS; AlimATM; RidDepAng) to a designated bookmark (1;2;3…) in Wd. It works “fine”, but in case I don’t have information in one table the macro brings in word whole table blank, and I don’t want that, I want it to bring only the Header of that table. Attach the macro. How can I correct the macro? Thanks
    SubExportExcelDataToWordDocument()‘DimwdExcelApp As Application ‘Excel is the default library (optional)
    Dim wdWordApp As Word.Application ‘Word app
    Application.ScreenUpdating= False
    ‘Creating a new instance of Word
    Set wdWordApp = New Word.Application ‘instantiate a new instance of Word 2010
    WithwdWordApp
    ‘Making Word Visible on the screen
    .Visible = True ‘iff false, document is invisible.
    .Activate ‘ make it the top pane, bring it to the front.
    ””””””””””””””””””””””””””””””””””””””””””””””’
    ‘ create a new Word Document based on the specified template
    ””””””””””””””””””””””””””””””””””””””””””””””’
    .Documents.Add “C:Usersstefan.georgescuDesktopTemplate fisa de esantionare –ver. 5-2019.dotm”
    ‘asbefore, copy the whole table from sheet to clipboard.
    Worksheets(“GestiuneSSC”).Activate
    Range(“A1”, Range(“A1″).End(xlDown).End(xlToRight)).Copy
    .Selection.GoTowhat:=-1, Name:=”bookmark1” ‘ -1 means “wdgotobookmark”
    .Selection.Paste ‘paste from the clipboard to the Word Doc.
    ‘****************
    ‘as before, copy the whole table from sheet to clipboard.
    Worksheets(“AlimATM”).Activate
    Range(“A1”, Range(“A1″).End(xlDown).End(xlToRight)).Copy
    .Selection.GoTowhat:=-1, Name:=”bookmark2” ‘ -1 means “wdgotobookmark”
    .Selection.Paste ‘paste from the clipboard to the Word Doc.
    ‘****************
    ‘as before, copy the whole table from sheet to clipboard.
    Worksheets(“DepRidAngajati”).Activate
    Range(“A1”, Range(“A1″).End(xlDown).End(xlToRight)).Copy
    .Selection.GoTowhat:=-1, Name:=”bookmark3” ‘ -1 means “wdgotobookmark”
    .Selection.Paste ‘paste from the clipboard to the Word Doc.
    ”””””””””””””””””””””””””””’
    ‘ Save WORD Document
    ”””””””””””””””””””””””””””’
    Dim TheFileName As String
    TheFileName = “C:Usersstefan.georgescuDesktopFisa.docx”
    ‘(SaveAsis for Office 2003 and earlier – deprecated)
    .ActiveDocument.SaveAs2 TheFileName
    ‘replaces existing .doc iff exists
    ‘Close Documents and Quit Word
    .ActiveDocument.Close ‘close .DOCx
    .Quit ‘exit Word
    End With
    Application.ScreenUpdating= True
    ‘MEMORYCLEANUP
    Set wdWordApp = Nothing ‘garbage collection
    ‘Set wdExcelApp = Nothing ‘OPTIONAL
    End Sub

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: https://www.mrexcel.com/forum/genera...es-word-4.html
    where you have already been given extensive help on this issue. I have also deleted your duplicate post on the same issue here.


    Kindly don't ask the same question in multiple threads. Kindly also read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Also cross-posted at: https://www.excelforum.com/excel-pro...to-wd-vba.html
    Evidently you don't care about the cross-posting rules here or on any other forum. Thread closed.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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