Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-26-2023, 07:56 PM
knpaddac knpaddac is offline Merging word documents containing tables Windows 10 Merging word documents containing tables Office 2016
Novice
Merging word documents containing tables
 
Join Date: Jan 2018
Posts: 13
knpaddac is on a distinguished road
Default Merging word documents containing tables


I frequently have to merge many (even hundreds of them) Word documents into one. Recently many of these have included content that is in tables within the documents. In the resulting doc, I am getting line breaks (enters) between the content from each cell but I want it to be tabs. Can anybody help me with this? I have included the code below as I am currently using it:


Code:
Sub Import_Text()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document, rtfFile As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.rtf", vbNormal)
Set wdDoc = ActiveDocument
While strFile <> ""
  Set rtfFile = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False, ConfirmConversions:=False)
  wdDoc.Range.InsertAfter rtfFile & vbCr & rtfFile.Range.Text & vbCr
  rtfFile.Close SaveChanges:=True
  strFile = Dir()
Wend
Set rtfFile = Nothing: Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Reply With Quote
  #2  
Old 11-27-2023, 02:49 PM
Guessed's Avatar
Guessed Guessed is offline Merging word documents containing tables Windows 10 Merging word documents containing tables Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Do you really want this as text only? Wouldn't it be better to import the content with its formatting & tables retained?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 11-27-2023, 04:04 PM
knpaddac knpaddac is offline Merging word documents containing tables Windows 10 Merging word documents containing tables Office 2016
Novice
Merging word documents containing tables
 
Join Date: Jan 2018
Posts: 13
knpaddac is on a distinguished road
Default

Yes, that would be fine as well.

Quote:
Originally Posted by Guessed View Post
Do you really want this as text only? Wouldn't it be better to import the content with its formatting & tables retained?
Reply With Quote
  #4  
Old 11-27-2023, 05:27 PM
Guessed's Avatar
Guessed Guessed is offline Merging word documents containing tables Windows 10 Merging word documents containing tables Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Try this version
Code:
Sub Import_Text()
  Dim strFolder As String, strFile As String, wdDoc As Document
  Application.ScreenUpdating = False
  strFolder = GetFolder
  If strFolder = "" Then Exit Sub
  strFile = Dir(strFolder & "\*.rtf", vbNormal)
  ChangeFileOpenDirectory strFolder
  Set wdDoc = ActiveDocument
  
  While strFile <> ""
    wdDoc.Range.InsertAfter vbCr & strFile & vbCr
    wdDoc.Range.Paragraphs.Last.Range.InsertFile FileName:=strFolder & "\" & strFile, ConfirmConversions:=False, Link:=False, Attachment:=False
    strFile = Dir()
  Wend
  Set wdDoc = Nothing
  Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
  Dim oFolder As Object
  Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
  If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
  Set oFolder = Nothing
End Function
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Guessed; 11-27-2023 at 07:44 PM.
Reply With Quote
  #5  
Old 11-27-2023, 07:32 PM
knpaddac knpaddac is offline Merging word documents containing tables Windows 10 Merging word documents containing tables Office 2016
Novice
Merging word documents containing tables
 
Join Date: Jan 2018
Posts: 13
knpaddac is on a distinguished road
Default

This does pull all the content in BUT it does not put the filenames in the resulting document like the previous version did.


Quote:
Originally Posted by Guessed View Post
Try this version
Code:
Sub Import_Text()
  Dim strFolder As String, strFile As String, wdDoc As Document
  Application.ScreenUpdating = False
  strFolder = GetFolder
  If strFolder = "" Then Exit Sub
  strFile = Dir(strFolder & "\*.rtf", vbNormal)
  ChangeFileOpenDirectory strFolder
  Set wdDoc = ActiveDocument
  
  While strFile <> ""
    wdDoc.Range.Paragraphs.Last.Range.InsertFile FileName:=strFolder & "\" & strFile, ConfirmConversions:=False, Link:=False, Attachment:=False
    strFile = Dir()
  Wend
  Set wdDoc = Nothing
  Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
  Dim oFolder As Object
  Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
  If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
  Set oFolder = Nothing
End Function
Reply With Quote
  #6  
Old 11-27-2023, 07:40 PM
Guessed's Avatar
Guessed Guessed is offline Merging word documents containing tables Windows 10 Merging word documents containing tables Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I added a line in the earlier post to include the filename. It is the red one.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 11-27-2023, 07:57 PM
knpaddac knpaddac is offline Merging word documents containing tables Windows 10 Merging word documents containing tables Office 2016
Novice
Merging word documents containing tables
 
Join Date: Jan 2018
Posts: 13
knpaddac is on a distinguished road
Default

This seems to be exactly what I need. Thank you very much! Will save me tons of time.



Quote:
Originally Posted by Guessed View Post
I added a line in the earlier post to include the filename. It is the red one.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging word documents containing tables Merging Word documents kp4bnx Word VBA 4 03-10-2021 02:49 PM
Merging word documents containing tables Batch merging of Word documents Harry Gateaux Word VBA 6 11-19-2020 09:58 AM
Merging word documents containing tables Updating links while merging Word documents ndw Word VBA 1 11-10-2017 01:07 PM
Merging word documents containing tables Merging two tables in WORD with VBA ffinley Word VBA 3 03-14-2017 07:43 PM
Merging different documents in one big document keeping the chapters, figure numbers and tables village Word 8 06-06-2016 04:16 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:24 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft