+ Reply to Thread
Results 1 to 3 of 3

Vba Macro in Excel for exporting multiples files in Word with prompt

Hybrid View

  1. #1
    Registered User
    Join Date
    12-13-2017
    Location
    France
    MS-Off Ver
    2013
    Posts
    4

    Vba Macro in Excel for exporting multiples files in Word with prompt

    Hi,

    I create a macro in Excel for exporting data in Word:

    Sub export_workbook_to_word() 
        Dim sheetName As String 
        Set obj = CreateObject("Word.Application") 
        obj.Visible = True 
        Set newobj = obj.Documents.Add 
         
        For Each ws In ActiveWorkbook.Sheets 
            sheetName = ws.Name 
             
             'Retrieve name of the Worksheet
            newobj.ActiveWindow.Selection.TypeText sheetName 
            newobj.ActiveWindow.Selection.Style = ActiveDocument.Styles(-2) 
            newobj.ActiveWindow.Selection.TypeParagraph 
             
            ws.UsedRange.Copy 
            newobj.ActiveWindow.Selection.PasteExcelTable False, False, False 
            newobj.ActiveWindow.Selection.InsertBreak Type:=7 
             
        Next 
        newobj.ActiveWindow.Selection.TypeBackspace 
        newobj.ActiveWindow.Selection.TypeBackspace 
         
        obj.Activate 
        newobj.SaveAs Filename:=Application.ActiveWorkbook.Path & "\" & Split(ActiveWorkbook.Name, ".")(0) 
         
    End Sub
    It's working correctly for one file. I would like now to do the same for several files in a folder. So I desire to update this script for creating a prompt in order to select the origin folder (with Excel files) and the destination folder (with Word files).

    Could you please help me to do that?

    Regards

  2. #2
    Registered User
    Join Date
    12-13-2017
    Location
    France
    MS-Off Ver
    2013
    Posts
    4

    Re: Vba Macro in Excel for exporting multiples files in Word with prompt

    I change the script and now all is ok :

    Private Sub ExportExcelToWord_Click()
    
      Dim xlApp As Object 'Excel.Application
      Dim xlWb As Object 'Excel.Workbook
      Dim xlWs As Object 'Excel.Worksheet
      Dim wdApp As Object 'Word.Application
      Dim wdDoc As Object 'Word.Document
      Dim Path As String
      Dim i As Long
      
      Set xlApp = CreateObject("Excel.Application")
      xlApp.EnableEvents = False
      xlApp.DisplayAlerts = False
    
      With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Choose the destination folder for Word documents"
        If Not .Show Then Exit Sub
        Path = .SelectedItems(1)
        If Right(Path, 1) <> "\" Then Path = Path & "\"
      End With
    
      With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = True
        .Title = "Choose the folder with Excel original documents"
        .Filters.Add "Excel files", "*.xls*"
        If Not .Show Then Exit Sub
    
        Set wdApp = CreateObject("Word.Application")
        wdApp.Visible = True
        wdApp.DisplayAlerts = 0 'wdAlertsNone
    
        For i = 1 To .SelectedItems.Count
          Set xlWb = xlApp.Workbooks.Open(.SelectedItems(i), False, True)
          Set wdDoc = wdApp.Documents.Add
    
          For Each xlWs In xlWb.Worksheets
            wdDoc.ActiveWindow.Selection.TypeText xlWs.Name
            wdDoc.ActiveWindow.Selection.Style = wdDoc.Styles(-2)
            wdDoc.ActiveWindow.Selection.TypeParagraph
            
            xlWs.UsedRange.Copy
            wdDoc.ActiveWindow.Selection.PasteExcelTable False, False, False
            wdDoc.ActiveWindow.Selection.InsertBreak Type:=7
          Next
          wdDoc.ActiveWindow.Selection.TypeBackspace
          wdDoc.ActiveWindow.Selection.TypeBackspace
          wdDoc.SaveAs Path & Split(xlWb.Name, ".")(0)
          wdDoc.Close False
          xlWb.Close False
        Next
      End With
      On Error Resume Next
      wdApp.Quit
      xlApp.Quit
    
    End Sub

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

    Re: Vba Macro in Excel for exporting multiples files in Word with prompt

    Rule 08: Cross-posting Without Links

    Your post does not comply with Rule 8 of our Forum RULES. Do not cross-post your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

    No further help to be offered, please, until the OP has complied with this request.
    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. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    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. Need to Merge Data of Multiples Excel Files Together Using VBA
    By vbaxcel in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 06-27-2017, 04:56 PM
  2. Macro to convert all word files to excel
    By hariexcel1987 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-11-2014, 11:15 AM
  3. Macro that will open and print files(word, excel, pdf)
    By ILoveStMartin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-18-2014, 11:16 AM
  4. Replies: 1
    Last Post: 05-19-2014, 08:34 AM
  5. Macro to open files in word and paste into excel
    By iliauk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2013, 01:29 AM
  6. Replies: 8
    Last Post: 10-20-2012, 06:51 AM
  7. Macro to copy data from multiples files
    By p1rate78 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-06-2009, 05:35 PM

Tags for this Thread

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