• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Macro to copy excel into word table

edbyford

New Member
Hello everyone


I've written a macro which is designed to run through a selected range of cells, open a word document, paste data from the selected rows into a table, and then save the document as a different file (so it creates one document for each row).


You need these files to run it:

https://dl.dropbox.com/u/420883/New%20folder/Book1.xlsm


https://dl.dropbox.com/u/420883/New%20folder/Tracker%20Template.docx


However it has several problems:


[*]I keep getting an error 4198 when runnning it on Excel/Word 2007.

[*]It also sometimes prompts me to save changes to the Normal.dot template, and because it's opening and closing Word this causes major problems as it causes Excel to freeze, Word to freeze etc.

[*]I would like to filter on certain columns in the Excel file, select those rows, and have it only work on those rows, but I'm unsure how to do this.

[*]The ErrorHandler is to handle where sometimes the RecordInfo(1) may have a / or a : in it, which means the file name will be invalid, so the user is prompted to rename it. Could these values be removed automatically?


Please can you take a look at let me know how you think it could be improved?


Option Explicit


Sub AutoFillWordTables()


Dim C As Range

Dim FileFilter As String

Dim Rng As Range

Dim WordFile As String

Dim wdApp As Object

Dim wdDoc As Object

Dim wdTbl As Object

Dim Wks As Worksheet

Dim fldr As Variant

Dim proceed As Variant

Dim strPath As String

Dim retVal As Variant


On Error GoTo ErrHandler:


Dim RecordInfo(0 To 12)

Set Rng = Nothing

Set Rng = Selection


proceed = MsgBox("This will create a tracker for the " & Rng.Rows.Count & " currently selected rows. Click OK to continue", vbOKCancel, "Create Tracker")


If proceed = vbOK Then


MsgBox ("Select Tracker Template")

FileFilter = "All Files(*.*),*.*"

WordFile = Excel.Application.GetOpenFilename(FileFilter)


If WordFile = "False" Then

MsgBox ("No file selected, exiting")

Exit Sub

End If


MsgBox ("Select destination folder")

With Application.FileDialog(msoFileDialogFolderPicker)

.Show

fldr = .SelectedItems(1)

End With


If fldr = "False" Then

MsgBox ("No folder selected, exiting")

Exit Sub

End If


For Each C In Rng


If InStr(1, Cells(C.Row, 11), "CLOSED") > 0 Then


Set wdApp = CreateObject("Word.Application")

wdApp.Visible = True

Set wdDoc = wdApp.Documents.Open(WordFile)


RecordInfo(0) = Cells(C.Row, 1)

RecordInfo(1) = Cells(C.Row, 2)

RecordInfo(2) = Cells(C.Row, 3)

RecordInfo(3) = Cells(C.Row, 4)

RecordInfo(4) = Cells(C.Row, 5) '


wdDoc.Tables(1).Cell(2, 2).Range.Text = RecordInfo(0)

wdDoc.Tables(1).Cell(3, 2).Range.Text = RecordInfo(1)

wdDoc.Tables(1).Cell(4, 2).Range.Text = RecordInfo(2)

wdDoc.Tables(1).Cell(5, 2).Range.Text = RecordInfo(3)

wdDoc.Tables(1).Cell(6, 2).Range.Text = RecordInfo(4)


strPath = fldr & "" & RecordInfo(1) & " - " & RecordInfo(2) & " v0.1"


wdApp.ActiveDocument.SaveAs strPath, FileFormat:=16


'nnnext:

wdApp.ActiveDocument.Close>
wdApp.Application.Quit

End If


Next C


MsgBox ("Complete")


retVal = Shell("explorer.exe " & fldr, vbNormalFocus)


Else

Exit Sub

End If


Set wdApp = Nothing

Set wdDoc = Nothing

Set wdTbl = Nothing


ErrHandler:

If Err.Number = 5152 Then

MsgBox ("File name (i.e. the title) contains invalid character. Please change this so that the tracker can be saved")

With wdApp.FileDialog(msoFileDialogSaveAs)

.InitialFileName = strPath

.Show

.Execute

End With


Resume Next

Else

MsgBox ("Error: " & Err.Number)

End If

End Sub
 
Hi, ebdyford!


I couldn't test it 2007 versions but in 2010 ones in the first run it didn't wrote any file.


Without digging deeply into the code I had to change this statement:

If InStr(1, Cells(C.Row, 11), "CLOSED") > 0 Then

to this:

If InStr(1, UCase(Cells(C.Row, 11)), "CLOSED") > 0 Then


Otherwise it never entered to the Word part. Once having done so a filename error prompt appear repeteadly, I didn't debug thoroughly it but it seemed to having entered and tried to save the first record that satisfied the condition in an endlessly loop.


The problem appeared to be related when trying to save a file with name as the contents of cell B13 (13th. row was my first selected and K column "Closed" record):

1/1/2012 09:34:46

as it has invalid characters for file name.


I suggest you to change format of cells in column B from:

m/d/yyyy hh:mm:ss

to (so as to not change your notation):

mm-dd-yyyy hh.mm.ss

but in fact I should use (so as to handle easier files within the created folder):

yyyy-mm-dd hh.mm.ss


Despite of this I hadn't no time to further analysis, so I leave these suggestions for you as homework. Check them, test them, apply them upon your consideration, and good luck.


Regards!
 
Back
Top