Hello
The idea :
1 Select data : The Macro open a FilePicker to select the file.xls where the data (adresses) are. Then you choose the sheet and manualy put the columne into B4.
2 Launch : Get the data from the file/sheet/column specified in B2-B4. Then generate a Word document from Label Template "3448" and fill 24 adresses per pages, save as word/pdf.
That part is working, i got bored with from internet code so I done it from scratch (despite not being used to do some VBA, ugly code here).
I should say that part is working great with "1 line text" from the specified data, the 24 texts are well placed on the template.
No more working if the specified data are "multiline text" (with ALT+ENTER) ; the 24 texts aren't well placed on the template.
I tried some workaround ( dealing with Chr(10), Chr(13), vbCrlf,etc.. ) same problem.
Note: The data are copied from the Specified file.xls into a hidden sheet "Work" on the Main workbook (from wich I launch the macro). Then i try to put the data from "Work" into a new word template.
Note: There is a will of creating new Hidden Instances of Excel & Word instead of getting already opened one.
Here is some code: CurrentRow and LastRow are used to reference 24rows of adresses
The logic : Template '3448' consist of (3 columns by 8 rows for a total of 24 labels). I got to understand that there are in fact 32 paragraphs on the new generated doc. A 4th column exist wich are EndOfLine ( i tried to catch it with IsEndOfLineMark but never returns true ), i catch these by Range.Orientation (column 1-3 Orientation = 0 , column 4 Orientation = 999999).
Use Case okay: (sheet 2 D:D -> single line text)
1- Open "GenerateurEditique.xls", click on button "Choisir Fichier", filePicker choose "Adresses.xls"
2- Click on "sheet 2" in ListBox
3- Manualy type text the text "D" into B4
4- Click on button "Générer un document Word d'Etiquettes"
5- Enjoy good output
Use Case NOT okay: (sheet 1 B:B -> multiline text)
1- Open "GenerateurEditique.xls", click on button "Choisir Fichier", filePicker choose "Adresses.xls"
2- Click on "sheet 1" in ListBox
3- Manualy type text the text "B" into B4
4- Click on button "Générer un document Word d'Etiquettes"
5- Do Not Enjoy bad output
Your thoughts are higly appreciated
Regards,
MrBadTSP
PS: files here http://s000.tinyupload.com/index.php?file_id=25475903505084180086
The idea :
1 Select data : The Macro open a FilePicker to select the file.xls where the data (adresses) are. Then you choose the sheet and manualy put the columne into B4.
2 Launch : Get the data from the file/sheet/column specified in B2-B4. Then generate a Word document from Label Template "3448" and fill 24 adresses per pages, save as word/pdf.
That part is working, i got bored with from internet code so I done it from scratch (despite not being used to do some VBA, ugly code here).
I should say that part is working great with "1 line text" from the specified data, the 24 texts are well placed on the template.
No more working if the specified data are "multiline text" (with ALT+ENTER) ; the 24 texts aren't well placed on the template.
I tried some workaround ( dealing with Chr(10), Chr(13), vbCrlf,etc.. ) same problem.
Note: The data are copied from the Specified file.xls into a hidden sheet "Work" on the Main workbook (from wich I launch the macro). Then i try to put the data from "Work" into a new word template.
Note: There is a will of creating new Hidden Instances of Excel & Word instead of getting already opened one.
Here is some code: CurrentRow and LastRow are used to reference 24rows of adresses
Code:
Sub CreateAndSaveOnePage(ByVal Page As Integer, ByVal CurrentRow As Integer, ByVal LastRow As Integer)
'Ouvre une nouvelle instance word hidden
Set oWD = CreateObject("Word.Application")
With oWD
.Visible = False
'créer un document word
.Documents.Add
'choisir le template label 3448
Set oDoc = .MailingLabel.CreateNewDocument(Name:="3448")
'template 3448 : 24 etiquettes par page; 3 colonnes de paragraphes, 8 lignes. (il y a une 4eme colonne avec les retour a la ligne, donc 4x8=32 paragraphs)
For m = 1 To 32
'Si le paragraphe est une etiquette : orientation 0, si c'est un retour à la ligne : orientation 9999999
If oDoc.Paragraphs(m).Range.Orientation = 0 Then
If CurrentRow <= LastRow Then
[COLOR=#ff0000]oDoc.Paragraphs(m).Range.Text = Worksheets("Work").Range("A" & CurrentRow).Value[/COLOR]
CurrentRow = CurrentRow + 1
End If
End If
Next m
'Sauvegarde document word
oDoc.SaveAs (Application.ActiveWorkbook.Path + "\" + Format(Now(), "yyyy-mm-dd") + "_p" + CStr(Page) + "_LabelsEditiques.docx")
'Sauvegarde document pdf
oDoc.SaveAs2 Application.ActiveWorkbook.Path + "\" + Format(Now(), "yyyy-mm-dd") + "_p" + CStr(Page) + "_LabelsEditiques.pdf", 17
'Fermeture de l'instance hidden word
.Quit
End With
'release variables
Set oWD = Nothing
Set oDoc = Nothing
End Sub
The logic : Template '3448' consist of (3 columns by 8 rows for a total of 24 labels). I got to understand that there are in fact 32 paragraphs on the new generated doc. A 4th column exist wich are EndOfLine ( i tried to catch it with IsEndOfLineMark but never returns true ), i catch these by Range.Orientation (column 1-3 Orientation = 0 , column 4 Orientation = 999999).
Use Case okay: (sheet 2 D:D -> single line text)
1- Open "GenerateurEditique.xls", click on button "Choisir Fichier", filePicker choose "Adresses.xls"
2- Click on "sheet 2" in ListBox
3- Manualy type text the text "D" into B4
4- Click on button "Générer un document Word d'Etiquettes"
5- Enjoy good output
Use Case NOT okay: (sheet 1 B:B -> multiline text)
1- Open "GenerateurEditique.xls", click on button "Choisir Fichier", filePicker choose "Adresses.xls"
2- Click on "sheet 1" in ListBox
3- Manualy type text the text "B" into B4
4- Click on button "Générer un document Word d'Etiquettes"
5- Do Not Enjoy bad output
Your thoughts are higly appreciated
Regards,
MrBadTSP
PS: files here http://s000.tinyupload.com/index.php?file_id=25475903505084180086
Last edited: