Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic

Print Word Document Using VBA

If anyone could help me out with this i'd be grateful.Scenario---I wish to automatically print out a word document when forced by me using VBA code. I have used the commondialog.showprinter statement to bring up the common dialog printer window (as required) yet when I click print the document is not printed. It just returns me to the code. How do I get this to print. Any ideas. Have I missed out initialising some value somwhere? Is there another way to print from within the VBA section for word (I NEED TO PROVIDE A LIST OF PRINTERS TO CHOOSE FROM - THAT IS WHY I AM USING THE COMMON DIALOG!!!"

View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Print 1 To Many In A Word Document
Is is possible to create a mail merge through Acces 97 that will merge a main document trailed by a 1 to many list, similar to a main form/subform relationship?.....or would bookmarking be a better option?

Any help appreciated.


Print To Word Document
I want it in a specific layout. Excel doesn't have a layout format --- does it????

Print An MS Word Document
Is there a way in VB6 to print a MS Word document using a specified printer driver (not the default one)?

Print Word Document
How do I print a word document using the Win32 API StartDoc, StartPage,EndPage and EndDoc functions without using the Word object?

Any help or sample code would be greatly apprecited..


Print Word Document
how do i print a word document that i have created ?

i have this code, the print is missing.... please help


Set ObjWord = New Word.Application
ObjWord.Documents.Open (App.Path & "

ObjWord.Selection.TypeText ("abcdefg")

ObjWord.ActiveDocument.SaveAs (App.Path & "

Set ObjWord = Nothing

How To Print A Word Document From VB
Hey, guys a little help here please... I am creating an application and I need to print a Microsoft Word Document but I have to initiate the printing fom within vb.

Normally I can print a document by going to explorer then right on the document then choose print from the pop-up menu, but how can I print the document from VB????

How To Print Word Document From VB?
Another question about VB and Word.

Print Word Document

I 'm working in factory with a lot of computers where my program is installed.
How can I print a word document with VB, using a free component.
I can't use the word component because I don't install the office in all the computers.
I need a free viewer like WordView but this one do not permit me to print with VB a word document.
If you know a component, a DLL or a free viewer please advise me.
Thank you

Print Word Document
I have already posted this question in the VB newbies sections... but didn't get an answer.

So, i have made some changes, more tests, so here it goes:

I write data in an invisible Word Document, without the user being aware of it, in order to print it. But the code line DocWord.ActiveDocument.Printout generated an error, it exists the function!!!! The weird thing is that it works on some computer!!!,
I really really dont know how to debug this!, it's only one line of code.....

any ideas?

Thanx a ton!

Print Word Document Without Header
I want to write a macro to print a word document without the header. It seems that this would be an easy macro to write but I am stumped. You could select the header and make it hidden; print the file and then make the header unhidden. Problem is there is no way to select all the text in the header so that you can format it as hidden. (I want to use the macro with any document.) Any ideas?

Print Recordset In Word Document

I've got a bookmark in Word where I want to print my recordset. What is the code?

I'm afraid that when I'm using 'objDoc.Bookmarks("Table").Range.Text =' and a 'while' that he is going to overwrite each time the previous values.

Can anyone help? Thx!

Print A Word Document Withou Seeing It?
Hi, i wrote that code to print a word document with vb6 and it's working great, but i see word open and the document in it. If i change the visible option to false, it give me an error. Is it a possible way to print a document without seeing it?

Dim App As Object

Set App = GetObject(folderpath + "constitution.doc")
App.application.Visible = True


PS: If i can, is it the same if i add some informations in it before i print it?

Print Word Document In Background
Hi all,

I would like to print word document in backgruond but to be able to setup printer using builtin printer dialog.

When I print using PrintOut function document is printed on default printer and it's printed in backgruond, but when I display print dialog and click "OK" document is printed on selected printer but not in backgrund

This is code that I use for print is this

VB Code:
Public Sub PrintPhoneList(cPhoneCalls As clsPhoneCalls)    Dim oWord As Word.Application    Dim cPhoneCall As clsPhoneCall    Dim lBrojac As Long        Set oWord = New Word.Application     oWord.Visible = False    oWord.Documents.Open App.Path & "documentsPHONE CALLS INSERTION.doc", , True, False         With oWord.ActiveDocument         For Each cPhoneCall In cPhoneCalls            lBrojac = lBrojac + 1            .Tables(2).Rows.Add            .Tables(2).Cell(.Tables(2).Rows.Count, 1).Range.Text = lBrojac            .Tables(2).Cell(.Tables(2).Rows.Count, 2).Range.Text = cPhoneCall.CallingPerson            .Tables(2).Cell(.Tables(2).Rows.Count, 3).Range.Text = cPhoneCall.CompanyName            .Tables(2).Cell(.Tables(2).Rows.Count, 4).Range.Text = cPhoneCall.PhoneNumber            .Tables(2).Cell(.Tables(2).Rows.Count, 5).Range.Text = cPhoneCall.AquiredPerson            .Tables(2).Cell(.Tables(2).Rows.Count, 6).Range.Text = cPhoneCall.Message            .Tables(2).Cell(.Tables(2).Rows.Count, 7).Range.Text = cPhoneCall.CallDate                Next         oWord.Dialogs(wdDialogFilePrint).Show  ' with this option Word window become visible                '.PrintOut False  ' with this line printing is done in backgruond        .Close False    End With        oWord.Quit    Set oWord = Nothing End Sub

Print Data To Word Document
HI all,

I'am printing to a word document.
When I reopen the document I print at die top of the document.

Who can I get to the buttom of the document, or send Ctrl +end send to the word document.


Need To Print Active Word Document
Hello All

I need to print an already opened word document from my vb6 program.

The need is that staff are constantly selecting the print button and so the documents are getting printed to their default printer.

So i have made a button which looks exactly the same as the print button in ms word, but this opens my program, which is a listbox with a couple of printers listed by department name, hense if they want to print an accounts sheet, they can select accounts, etc.

I have tried bits of coding to get this to print with no joy.

Any help from you intelligent lot.

Thanx so much.

How To Print An Embeded Word Document In Excel?

I have an excel file, and in one sheet I have embeded a word document. the word document is 12 pages long... how can I print the hole embeded word document??
I tried adding a print button in the word document and I only wrote


it gives me this error: This command is Invalid

I tried the same code in a normal word document and works just fine... but I do't know what happens when you insert the word document in an excel sheet...


How To Print Reports And Data Into A Word Document?
I want to redirect data manually (not from any reports )from my program into a word document .How to print those data into the word document and also in pdf file ?

Print Word Document To Selected Printer

I have made a program which when clicked on a option, will print the active word document to that selected printer. The problem is that it works fine on a terminal but won't work on the server. It does change the default printer to the one selected, but won't print out. Below is the code i am using:

Reference to: Microsoft Word 10.0 Object Library

Dim WdApp As Word.Application

On Error Resume Next

SetDefaultPrinter "\LXXP13Samsung ML-7050 Series PCL5e (Copy 1)"

    Set WdApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Set WdApp = CreateObject("Word.Application")
    End If

WdApp.ActiveDocument.Close (yes)
Set WdApp = Nothing

And in a module i have:

Public Declare Function SetDefaultPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal PrinterName As String) As Boolean

Thanx for you help.

Create Word Document From Scratch && Print It - Help!!! [ RESOLVED ]
How do I create a Word Document from vb?

What reference is needed?

Thanks In Advanced.

Make Word Document Active And Print From Excel Macro
I have an excel spreadsheet with multiple hyperlinks to MS Word files. I know how to open the files using the following code:


ActiveWindow.View = xlNormalView
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True

I need to know how to make the Word file the active window and then print it to the default printer in VB. I also need to know how to handle any error or warning messages. ie., when a box pops up to tell me the file should be opened as read only.


Use VB6 To Activate A WORD Document And Use A Active X Barcode Control To Print Barcode
I am now using a VB program to open a MS WORD Document. In the word document, there is a ActiveX control which is a Barcode encoding control. I already used regsvr32 to register the DLL and inserted it into the WORD document. Also I can right click on the control directly in Microsoft word to set the property of the control.

But when I want to use the VB program to do so. I cannout find a function which can used to set the value of the information for generating barcode. There existed a property in the Barcode activeX control called 'DataToEncode'. What I want to do is to set a value to this properties. Can anyone help. Thanks a lot.

Opening Word Document From Excel When The Word Document Is Read Only
I'm trying to open a Word document from Excel and have hit a snag. The file I'm trying to access is Read-only and the code stops when the "This file is read-only..." prompt displays. How do I get past this? Here's the code that I have so far (thanks to some help from this forum):

Sub OpenAndPrint()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim stPathName As String

stPathName = Range("B4").Hyperlinks(1).Address

If Dir(stPathName) <> "" Then
Set wdApp = New Word.Application
wdApp.Visible = True
wdApp.DisplayAlerts = wdAlertsNone 'Tried to turn off the prompt, but it didn't work.
Set wdDoc = wdApp.Documents.Open(stPathName) 'Code stops here and waits for the user to respond to the prompt.
Set wdDoc = Nothing
Set wdApp = Nothing
MsgBox "The file does not exist!", vbInformation
End If

End Sub

Print Range Of Pages, Find Length Of Document To Print
Hi, I am trying to accomplish two things.
1) I would like to find out how many pages would print out, IF I were to print. ie. Warn the user: "You have asked to print out 500 sheets at $4.50 a sheet."
2) I would like to choose a range of pages to print out, without that range set as the "default" the next time the print function is called. So, my current printing method below is not what I would like. (Unless there is a way to restore defaults?) It displays the normal print dialog, and remembers the last print range entered by the user.

I am displaying web pages on a form within the webbrowser control. Some are very long, so these features are important to me. How it is done is less important. Any advice is appreciated. Thanks!

Here is my very simple example code:

Option Explicit

'Load a web page
Private Sub Command1_Click()
WebBrowser1.Navigate ("")
End Sub

'Brings up the print dialog box on button click.
Private Sub Command3_Click()
Dim eQuery As OLECMDF
eQuery = WebBrowser1.QueryStatusWB(OLECMDID_PRINT)
If Err.Number = 0 Then
End If
End If
End Sub

Private Sub cmdNavigate_Click()
Dim txtNavigate As String
WebBrowser1.Navigate txtNavigate
End Sub

MS Word 2000: Closing Forms In A Document From Another Document With Code
I have a project, in which I use a global template to manipulate different other templates. In those templates there are Userforms built in to gather information to use in a MailMerge. The problem I'm having is that I want only to hide the forms in the templates themselves, and close them in the global template when the whole application is closed. The trouble is that I can't find a way to access the open forms from the global template. Can anybody help me?

Merging/Importing Word, Excel, Powerpoint And Visio Files Into A Word Document
Does anyone know how to do this or know of a website that has source code that does this? I can't find anything anywhere.

Each Page/Worksheet/Slide goes on a seperate page. Print formatting is retained (landscape/portrait) and if the imported Page/Worksheet/Slide is larger than the page im merging/importing to, it should auto-resize to fit the page.


Reading The Word Document From Visual Basic Without Opening The Word Applicaiton

I need to access the word document from visual basic without opening the word application. I can do this by opening the word application. Is there any other way to read a document from vb.

I need to search for a particular word in the document without opening the word application.

Waiting for your reply,



Accessing Word Document 'header/footer' Via Word Object Library
Hi All

As the title suggests really, I can access everything I need on a Word document opened via the Word Library object but I can't find how I access the headers and footers of a docuemnt

Many Thanks

Merging Word, Excel, Powerpoint And Visio Files Into A Word Document
Does anyone know how to do this or know of a website that has source code that does this? I can't find anything anywhere.


I posted this in VB General but then i saw this forum so I wasn't sure where it belonged and posted in both. Reply to either one.

Error 4605 Calling Word.Document.Save With Word 2002 SP1
I have a test VB6 exe project with a WebBrowser control, called web1 (added via Project, Components, Microsoft Internal Controls). My project also references the Microsoft Word 10.0 Object Library.

As well as the WebBrowser control, my form has a Save button (cmdSave). Here is the complete code:


Private g_objDocument As Word.Document

Private Sub cmdSave_Click()
End Sub

Private Sub Form_Load()
Dim strFileName As String
strFileName = App.Path & "WordWrapper.doc"
web1.Navigate strFileName
End Sub

Private Sub web1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
Set g_objDocument = pDisp.Document
End Sub

The idea is when I run the exe, it loads a Word document into the WebBrowser control. This document is also assigned to
a global Word.Document object, g_objDocument, when web1 navigates to it. The problem occurs when I try to save the document (in cmdSave_Click). I get the error:

"The Save method or property is not available because this document is in another application."

This error occurs with Word 2002 SP 1 (on Windows XP), but not with Word 2000 SP3.

Please help.

Load Word Document In VB Form And Use Word's Zoom Features - Help

I've this VB6 application where I can opt to load a word document or scaned 8X11 image of a paper form on the VB form and let user write or draw using a tablet. Since screen doesn't have enough space to show complete form at one time, I need to zoom it down to fit the screen so it get mapped properly with tablet. Once user is done drawing or writing, I need to bring the document or image back to original size and print it or save it.

I don't know where to start. Plese guide me about the best way to accomplish this. Thanks.

Convert Html To Word Docuemtn Using The Word Document Object
i ant in vb6 to open an html page
and convert and save it as word *.doc
how can i do this?
and is it possible to open the html file from the internet with a give url so i will be :
open html document from www..../1.html -> save as 1.doc
thnaks in advance

Load Word Document On VB Form And Use Word's Zoom Fetrures - Help

I've this VB6 application where I can opt to load a word document or scaned 8X11 image of a paper form on the VB form and let user write or draw using a tablet. Since screen doesn't have enough space to show complete form at one time, I need to zoom it down to fit the screen so it get mapped properly with tablet. Once user is done drawing or writing, I need to bring the document or image back to original size and print it or save it.

I don't know where to start. Plese guide me about the best way to accomplish this. Thanks.

VB In Access To Find And Format A Word In A Mailmerged Word Document
I use my Access data base to create mail merged letters to our customers. Not only are the names etc stored on the database I also store the content of the letter in memo fields. I have occasions where I want the newly created letter to have some words bold. After having VB code open the main document, mail merge then close the main document I run the following code. It works the first time round, the specified word becomes bold. However after closing the letter and running it a second time the new letter opens but no changes are made and I get an error. "Run Time error '462' : The remote server machine does not exist or is unavailable". This gives me a Debug option. When selecting debug The first row in this code is selected.

With ActiveDocument.Content.Find

    .Font.Bold = False
    .MatchCase = True
    .Text = "SATURDAY"
    With .Replacement
        .Font.Bold = True
        .Text = "SATURDAY"
    End With
    .Execute Format:=True, Replace:=wdReplaceAll
End With

If I leave the new letter open and re run it the code opens a second new letter with no errors, however it does not make the changes to the newest document only the first new document it finds. ie If I then close the first one created and rerun it the second document will have the changes made and the third one does not change. The error only occurs once I have closed all new documents and try to open another letter.

Hope someone can help

MSAccess Database DOA

Word Automation - Retaining The Numbering Format In New Word Document
Hi all,

I am using word automation to read a word document. I use word automation to read the word document page by page and paste the contents of each page in a new document.

The problem i am facing is, the word document which is read contains Heading1,Heading 2 etc and these are numbered. When I paste these pages onto a new document the numbering is not retained. (Eg., if my Heading1 number is 2.1 when i paste it in a new document the Heading1 number is shown as 1.1)

VBA: Creating Word, And Sending Data To A Word Document Using Ranges
The following code works fine when it is executed only once in any instance of the VB program.

It is OK provided I do not close Word, and it will create multiple instances of the document into the same instance or word.

Screen.MousePointer = 11
WordApp.Documents.Open ("C:Program FilesWill WizardTemplatesClauseLib.doc")
set MyDoc = Documents.Open(FileName:="C:Program FilesWill WizardTemplatesClauseLib.doc")
set FldRng(1) = MyDoc.Range(Start:=MyDoc.Bookmarks("Name").Range.End, End:=MyDoc.Bookmarks("NameEnd").Start - 1) 'read bookmarks into range object variables
FldRng(1).Text = ClauseHold 'put the stuff into the document
WordApp.ActiveDocument.SaveAS FileName:=("C:Program FilesWill WizardTemplates emp" & Trim(Str(TMPNum)) & ".doc")
WordApp.Visible = true
set WordApp = nothing
set MyDoc = nothing
Screen.MousePointer = 0
Command8.Enabled = true
Exit Sub

If Word is closed, and the code is executed again, an error occurs "the remote server machine does not exist or is not available" The only remedy for this is close and re-start the VB application containing this code.

The GetWord2 et-al Function codes are: (which is stored in a module)

option Explicit
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (byval lpClassName as string, byval lpWindowName as string) as Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (byval hwnd as Long, byval wMsg as Long, byval wParam as Long, byval lParam as Long) as Long
public WordApp as Word.Application
public WordWasNotRunning as Boolean ' Flag for final word unload

public Sub DetectWord()
' Procedure detects a running Word and r
' egisters it.
Const WM_USER = 1024
Dim hwnd as Long
' If Word is running this API call retur
' ns its handle.
hwnd = FindWindow("OpusApp", vbNullString)
If hwnd = 0 then ' 0 means Word Not running.
Exit Sub
' Word is running so use the SendMessage
' API function to enter it in the Running
' Object Table.
SendMessage hwnd, WM_USER + 18, 0, 0
End If
End Sub
public Sub GetWord2()

' Test to see if there is a copy of Micr
' osoft Word already running.
on error resume next ' Defer error trapping.
' Getobject function called without the
' first argument returns a
' reference to an instance of the applic
' ation. If the application isn't
' running, an error occurs.
set WordApp = GetObject(, "Word.Application")
If Err.Number &lt;&gt; 0 then
WordWasNotRunning = true
WordWasNotRunning = false
End If
Err.Clear ' Clear Err object In Case error occurred.
' Check for Microsoft Word. If Microsoft
' Word is running,
' enter it into the Running Object table
' .
If WordWasNotRunning = true then
'set the object variable to start a new
' instance of Word.
set WordApp = new Word.Application
End If
' Show Microsoft Word through its Applic
' ation property. then
' show the actual window containing the
' file using the Windows
' collection of the MyWord object refere
' nce.
WordApp.Visible = true
WordApp.Application.WindowState = wdWindowStateNormal
Screen.MousePointer = vbDefault
End Sub

do I need to close the document? = deactivate something?

I would like a code example on how to fix the above, else, any suggestions, ideas...


A Search Of A Word Document And .XLS Document
I need to search a word document for time values that look like this 13:43:42. Find them in a spreadsheet document. Once found there are 3 columns of data in the .xls that correspond to the time. This then needs to go into a .txt file. Then need to search the word document again for the next time and do again. When done the final .txt file should have the time and cooresponding data across the page. Who out there can put this together to run in a macro?

Word Document (*.DOC) And WordPad Document (*.DOC) ?

I need to identify that a document file (*.DOC) is a Word document or a WordPad document in me program. Because WordPad can not open always a file (*.DOC) which can be openned by WORD.



Word: Counting A Specific String In A Word Document
Hi all,

After visiting the Word FAQ I couldn't find any clue for my query:
I would like to count (in VB) how many times the string "Yesterday"
appears in a word document I am working on.

By using the manual Replace function (replacing "Yesterday" by "Yesterday"),
Word tells you how many replacement it has done and therefore how many
instances of the string "Yesterday" were in the document.

Can anyone point me in the right direction about using VB to find out
how many times "Yesterday" appears in my document, please?

Many thanks

Can I Insert A Word In The Cursor Position In A Word Document
I use this statement in VBA2000:

ActiveDocument.Content.InsertAfter ("USA")

but it alwasy insert at the end.

How can I insert a word in the cursor position?

Selecting A Particular Word From A Word Document (.doc) Through Visual Basic 6
I am a developer using Visual Basic 6. One of my program accepts some values through text boxes and then creates a Word document. The text boxes are optional, and the created Word document contains only non-empty values along with appropriate labels. I am able to apply styles (bold and underline) to entire paragraphs. But the user wants me to apply bold formatting only to values entered by him and not to the labels which describe those values. Please help me.

Thank You.

Lalit Kumar Barik

Open A Document Word From A Template Of Word
Hello, I'm working in Visual Basic 6 and I want open a document word. But this document must be from a template of word (its name is Informe).

I do it with the code
Dim objWord As Word.Application
Dim objDoc As Word.Document

Set objWord = New Word.Application
Set objDoc = objWord.Documents.Add("")

It's ok but I only see the document, I want to see the menus of word,....
If it isn't possible can you send me the code to print this document?

Thank's and sorry for my poor english

Word Form &gt;&gt; Process &gt;&gt; Word Document
Please be gentle, as this is my first foray into VBA. To make matters worse, I'm not a programmer, just someone who needs to get something done. My guess, based on what I want to do, it that I can do it easiest in VBA, but I am hoping this forum will help flesh out that answer for me.

In a nutshell, I am trying to create a document (possibly a Word form?...) that has a definite format with fields to be filled in (mostly numberic values, checkboxes, or free text). This form will be filled out by our techs at a customer site. They may do this 'live' with Word open, or they may print it out, then fill in the document later from their handwritten copy.

Next, I would like to 'process' their answers by feeding some information directly into an output document (Word hopefully). Now, some fields like name, address, etc... will go straight over and don't require manipulation. Some other data will need to be processed.

For example, if the tech fills in 34, I might want the output document to put 'this text' under a certain heading. If it is >34, then some other text, less, maybe no text. I'd also like to be able to change some graphics based on limits. i.e. if the value is less than this, insert this picture into the output document at a specific location, if not, insert this picture.

So, in general, before I go down the road of learning a language... do the members here think VBA would be a good choice for this? Would I 'read' from the input document in Word Excel, process in Excel and then pump data out to the output docment, etc... I am looking for advice whether this is;

1. fairly straightforward, understanding that I would need to learn some VBA.
2. possible
3. a good choice. i.e. would another language, or a pre-done software make a better choice.

My contraints are that my company has Office 2000 rolled out, WITHOUT Access. We use Lotus Notes and Access is apparently frowned upon. I am an optimist, so I expect it's possible with careful design and help from the experts who've been down these roads before.

Thanks in advance for any help, insight, advice or code.


Opening A Word Document With Word From Excel
i want to have a part of an excel macro where it opens up a word document in word (so it would have to open the application itself) and then be able to do everything that i can do using the word vb editor. is there some easy way to do this?

Opening An Application Such As Word Document Using Word
how to open a .doc file using a word exe through vb gui.


Change Word On The Footer Of A Word Document
I like to change the word "Version 1.0" to "Version 2.0" on every page on the footer of a word documents using VB6 with the word document minimised when the program is making the change. Does anyone know how to do this? Thanks.

Word -VBA / Accessing Content Of A Word Document

Can you access the conent (document.WholeStory or document.Conent) of a Word document without opening the document or triggering the OpenDocument event?


Deleting Word Document Using Vb For Word 2000

how to delete a word document from the hard disk using vb for word 2000?
i tried using kill function but it shows an error -"permission denied"..pls can sum1 help?

Find A Specific Word From Word Document...
Hi All,

Need help...

i have a word doc. it contains some data.. and bottom of the page it contain the following paragraphs. its single page word doc.
Code:Very truly yours,

From vb i want to find word, the next line of the 'very truely yours'. like here in this example i have to capture the value 'sam'.

Any idea how to get that..


Edited by - pmmbala1976 on 6/2/2004 4:45:19 PM

Deleting Word Document Using Vb For Word 2000

how to delete a word document from the hard disk using vb for word 2000?
i tried using kill function but it shows an error -"permission denied"..pls can sum1 help?

Copyright 2005-08, All rights reserved