Home > database >  Word Table to CSV file Macro
Word Table to CSV file Macro

Time:08-03

I'm new to Macros and teaching myself as I go. My goal is to populate a file with data from a Word table using CSV formatting. The table spans over 300 pages with no exact number of rows per page. A user prompt for selecting a file at the beginning has been created. What would be recommended process wise? I know a For loop would be needed for the row count.

Dim fd As Office.FileDialog
Dim strFile As String

Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd

.Filters.Clear
.Filters.Add "Word Files", "*.docx?", 1
.Title = "Choose a Word file"
.AllowMultiSelect = False

.InitialFileName = "C:\VBA Folder"

If .Show = True Then
strFile = .SelectedItems(1)
End If
End With

Intended Output: Identifer, Artifact Type, Name, Red, Amber, White, Cust, IO_Type, M_680A, M_680_Phase, M_750, Left_Input, Right_Input, Input_Type, Source, Debounce, LOPI, TOPI, ESDI, EFI, BFI, On_gnd, In_air, FDRIO, Auto_test, Frome, Level, VerifiedBy, Status

XXX , Module, X346, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,

XXX , Module, S003, , , , , None,6, , , , , , , , , , , , , , , , , SAS, ,

XXX , Module, X301, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,

XXX , Module, X025, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,

XXX , Module, X511, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,

XXX , Module, X347, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,

XXX , Module, X514, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,

XXX , Module, S050, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,

XXX , Module, X021, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,

XXX , Module, X517, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,

XXX , Module, X599, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,

XXX , Module, X022, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,

XXX , Module, X521, , , , , None,6, ,7, , , , , , , , , , , , , , , ADD, , ,

XXX , Module, X523, , , , , None,6, ,7, , , , , , , , , , , , , , , ADD, , ,

XXX , Module, X019, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,

XXX , Module, X518, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,

XXX , Module, M141, , , W, , None,6, ,7, , , ,,0, L, T, , , , , , ,, SAS, ,,

CodePudding user response:

At close to its simplest, you can convert a table to CSV format with:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With Selection
  If .Information(wdWithInTable) = True Then
    Set Rng = .Tables(1).Range
    With .Tables(1)
      .ConvertToText Separator:=","
    End With
    Rng.Select
  End If
End With
Application.ScreenUpdating = True
End Sub

Simply place the selection point anywhere within the table. The converted range will be selected when the macro finishes.

  • Related