How to PASTE contents into WORD doc via VBA?

M

Mark Findlay

I currently use the following VBA script to print a WORD doc automatically.

oWord = new ActiveXObject("Word.Application");
oWord.Visible = false;
oWord.Documents.Open(sFilename,false,true);
oWord.Documents.Item(1).PrintOut(false);
oWord.Documents.Close();

I would like to now modify the script so that it can PASTE the contents of
the clipboard into the doc before printing.

I have searched the document object model but can't seem to locate how to do
this.

Can anyone lend a hand?

Many thanks!
Mark
 
W

Wei-Dong XU [MSFT]

Hi Mark,

From my understanding, you are going to paste the content in the clipboard to your doc file before the printing. You can retrieve the range object
of the doc file and then use the Paste method to copy the content into the doc file, then print them. Please see my sample with the commentary.

'-------------------------------------------------------
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oRng As Word.Range

Set oApp = CreateObject("Word.Application")
'specify the doc file here
Set oDoc = oApp.Documents.Open("3723.doc")

'retrieve the Range object of the doc file
Set oRng = oDoc.Range(1, 1)
'paste the content from clipboard into doc
oRng.Paste
'call the printout method
'...
'then exit
oDoc.Close
oApp.Quit
'-------------------------------------------------------

Please feel free to let me know if you have any further questions.

Best Regards,
Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Hewett

Hi Wei-Dong XU [MSFT]

I'm just being picky. But it may be worth pointing out that:
Set oRng = oDoc.Range(1, 1)
oRng.Paste

pastes the contents of the clipboard at the begriming of the document. If you want to add
them to the end use:
Set oRng = oDoc.Content
oRng.Collaspse wdCollapseEnd
oRng.Paste

If you want to paste the contents somewhere other than either of these location you can
use a bookmark:
Set oRng = oDoc.Bookmarks("BM1").Range
oRng.Paste

HTH + Cheers - Peter


(e-mail address removed) (Wei-Dong XU [MSFT]), said:
 
M

Mark Findlay

Thank you Wei-Dong,

I have now been instructed that I will not be opening up an existing
document. Instead, I am to paste the contents of the clipboard directly into
a 'new' document, then print it without saving it, then close Word. This
means I am essentially printing the contents of the clipboard, and using
Word as my print mechanism.

Since I am not going to open an existing document, would the resulting code
look like this? (I am using Javascript, so would I need to replace the Set
oRng with something else?)

oWord = new ActiveXObject("Word.Application");
oWord.Visible = false;
oWord.Documents.Add();
Set oRng = oWord.Documents.Item(1).Range(1, 1);
oRng.Paste();
oWord.Documents.Item(1).PrintOut(false);
oWord.Documents.Close();

Thanks!
Mark


Wei-Dong XU said:
Hi Mark,

From my understanding, you are going to paste the content in the clipboard
to your doc file before the printing. You can retrieve the range object
of the doc file and then use the Paste method to copy the content into the
doc file, then print them. Please see my sample with the commentary.
 
W

Wei-Dong XU [MSFT]

Hi Peter,

You are exactly right! My sample is just used for the demonstration of paste in Word doc for Mark.

Best Regards,
Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark Findlay

Thanks Wei. Between your advice and some previous experience, I believe I
have a working solution.
I list it below for anyone who may find it helpful: (Javascript)

var oWord=null;
var idTmr = "";

//********************************
// Cleanup from WORD doc printing
//********************************
function Cleanup()
{
window.clearInterval(idTmr);
try
{
if (oWord != null)
{
oWord.Quit();
oWord=null;
}
}catch(e){}

CollectGarbage();
}

//********************************
// PrintCellContents
//********************************
// Prints the cell contents to the clipboard, then uses VBA to print via
WORD
function PrintCurrentFlash()
{
Copied = holdtext.createTextRange();
//Copied.execCommand("RemoveFormat"); // optional
Copied.execCommand("Copy");

try
{
oWord = new ActiveXObject("Word.Application");
oWord.Visible = false;
}
catch(e)
{
alert("Unable to create the Word.Application object. " + e.description +
". Please ensure that your 'ActiveX Controls and plug-ins' security setting
for 'Initialize and script ActiveX controls not marked as safe' is set to
either PROMPT or ENABLE for the current zone");
location.reload();
}

try
{
oWord.Documents.Add();
oWord.Selection.Paste();
oWord.Documents.Item(1).PrintOut(false);
oWord.Documents.Close(0);
}
catch(e)
{
alert("MSWord Error attempting to print cell/clipboard contents. " +
e.description);
Cleanup();
location.reload();
return false;
}

idTmr = window.setInterval('Cleanup()',250);

return false;
}


===============================================

Wei-Dong XU said:
Hi Mark,

From my understanding, you are going to paste the content in the clipboard
to your doc file before the printing. You can retrieve the range object
of the doc file and then use the Paste method to copy the content into the
doc file, then print them. Please see my sample with the commentary.
 
W

Wei-Dong XU [MSFT]

Hi Mark,

You are welcome! Please feel free to let me know if you have any further questions.

Best Regards,
Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top