VBA excel macro to print data in a word document.

joepublic

New Member
Joined
Nov 23, 2005
Messages
24
Hello,
I am really newbie in excel vba...
I am using excel and word 2000. Thi is a really simple issue, but I can't solve.

I try to pass some values from Excel table to an especific position of a document word using bookmarks and it must be printed automaticaly by pressing a shortcut keyboard from excel.

The process must be all invisible.
I have done that by copying some vba code and pasting and it works fine.

The problem comes up when by the print out PART.
I can't do that it gets printed out automatically.

Only when I create an object

Set wdApp = CreateObject("Word.Application")

It prints out but something goes wrong, because word doesn't close properly and if I try to run this macro again... always appears a window saying that the document is being used by other user... but I can't close it...

This here below is my code... can you review it please... Thanks
****************************************************
Sub passdatatoword ()
On Error GoTo errorHandler
Dim wdApp As Word.Application
Dim Mydoc As Word.Document

'********************** variables Excel **************

Dim Usuario_Nombre As Excel.Range
Dim Usuario_Apell As Excel.Range

Set wdApp = New Word.Application
Set Mydoc = wdApp.Documents.Add(Template:=" \\Infofim74\serviciossociales\SERSOCIAL\Atencion_Publico\CORREO\Correo_vinculo_Sius\X2_Oficio_usuario_vinculado_siuss Pruebas.doc")


'*********** Giving variables from excel ************
Set Usuario_Nombre = Sheets("Correo").Range("H2")
Set Usuario_Apell = Sheets("Correo").Range("I2")

' **** giving variables too bookmarks of word document ****************
With Mydoc.Bookmarks

.Item("Usuario_Nombre").Range.InsertAfter Usuario_Nombre
.Item("Usuario_Apell").Range.InsertAfter Usuario_Apell

End With

'******************PRINT OUT PART ** Print document word ***********
'we have severals printers.
ActivePrinter = "HP Officejet K7100 Series"

'I need to print two pages. Page 1 one copy and page 2 three copies. Can you help me to do that?

with
.PrintOut Background:=False, Copies:=1
.Close False 'It doesn't close...
End With

'********* Close template and word application ************
'probabli here I have to close a file and use CLOSE AND QUIT.


'**************** Final ******************
errorHandler:
Set wdApp = Nothing
Set Mydoc = Nothing

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,216,096
Messages
6,128,809
Members
449,468
Latest member
AGreen17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top