+ Reply to Thread
Results 1 to 3 of 3

VBA Beginner - Copying Data from word file to a new excel document

Hybrid View

  1. #1
    Registered User
    Join Date
    06-20-2020
    Location
    India
    MS-Off Ver
    10.02
    Posts
    6

    VBA Beginner - Copying Data from word file to a new excel document

    #1
    Hi,

    I am an Excel VBA noob. Could you please help me correct this code? The Excel file that gets created is corrupted/blank.

    Thanks,
    GreyFox


    Sub CopyWordtoExcel()
    
    Dim FileName As String
    Dim myExcel As Object
    Dim myWb As Object
    Dim MainData As String
    
    FileName = ActiveDocument.Name
    
    ActiveDocument.Tables(1).Select
    Selection.Cut
    Selection.TypeParagraph
    Selection.InsertBreak Type:=wdPageBreak
    Selection.PasteAndFormat (wdFormatOriginalFormatting)
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
    Selection.GoTo What:=wdGoToBookmark, Name:="\page"
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.GoTo What:=wdGoToBookmark, Name:="\page"
    Selection.Delete Unit:=wdCharacter, Count:=1
    
    MainData = ActiveDocument.Content
    
    Set myExcel = CreateObject("Excel.Application")
    Set myWb = myExcel.Workbooks.Add
    Range("A1").Value = MainData
    
    Application.DisplayAlerts = False
    myWb.SaveAs FileName:="XX" & "\" & FileName & ".xls"
    
    Application.DisplayAlerts = False
    
    myWb.Close False
    Set myWb = Nothing
    Set myExcel = Nothing
    
    End Sub
    Last edited by davesexcel; 07-18-2021 at 12:14 AM.

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,053

    Re: VBA Beginner - Copying Data from word file to a new excel document

    Maybe you need to refer to a worksheet in myWB?

    Set myExcel = CreateObject("Excel.Application")
    Set myWb = myExcel.Workbooks.Add
    myWB.Worksheets(1).Range("A1").Value = MainData

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    81,721

    Re: VBA Beginner - Copying Data from word file to a new excel document

    Administrative Note:

    We would very much like to help you with your query, however it has been brought to our attention that the same query has been posted on one or more other forums and you have not provided the required cross-post link(s) here.

    Please see Forum Rule #3 about cross-posting and adjust accordingly. Read this to understand why we (and other sites like us) consider this to be important.

    (Note: this requirement is not optional. I will do it for you just this once: https://www.mrexcel.com/board/thread...ument.1176722/.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro that transfers data from excel to word document and then saves document as pdf file
    By mlcfexcel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-08-2021, 08:47 PM
  2. Copying data out of a word document issue
    By tom.mat.walker in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 06-17-2016, 06:51 AM
  3. Replies: 0
    Last Post: 06-15-2011, 02:22 PM
  4. Replies: 0
    Last Post: 12-16-2009, 07:39 PM
  5. copying from excel to word document
    By walid66 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-02-2007, 01:36 PM
  6. Copying Cell data and pasting into word document?
    By Simon Lloyd in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-07-2006, 05:41 AM
  7. [SOLVED] Copying an Excel chart into a Word document
    By Carolyn in forum Excel General
    Replies: 7
    Last Post: 07-11-2005, 03:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1