Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.beginners
external usenet poster
 
Posts: 87
Default Creating a new word doc and putting some VBA code behind it

I am opening a mail merge main document with the following code behind
it. My current method involves controlling everything from the mail
merge main document, with a loop using DoEvents. This seems rather
cumbersome. It would be neat if I could put some VBA behind the
Document_Close event of the mail merge result file. Ideally, I'd like
to pass the queue name by parameter which would be written into my word
document directly, so I don't have to read it from the file again


Option Explicit
Private Sub Document_Open()

Dim sDataSrc As String
Dim sThisDoc As String

sThisDoc = ThisDocument.Name
sThisDoc = Left(sThisDoc, Len(sThisDoc) - 4) & "Data.doc"
sDataSrc = ThisDocument.Path & "\" & sThisDoc

'Skip MailMerge if data source not present....
On Error Resume Next
MailMerge.OpenDataSource sDataSrc
If Err Then GoTo Done
MailMerge.Execute

'This created mail merge document
ActiveDocument.Saved = True
ThisDocument.Saved = True

Done:
SetNonFinePrint
WaitForClose

End Sub
Sub SetFinePrint()

Dim iHandle As Integer
Dim sPrinter As String


'FinePrint will always be the first row in the file
iHandle = FreeFile
Open "C:\PRINTERS.DAT" For Input As #iHandle
Input #iHandle, sPrinter
ActivePrinter = sPrinter
Close #iHandle

End Sub
Sub SetNonFinePrint()

Dim iHandle As Integer
Dim sPrinter As String


'FinePrint will always be the first row in the file
iHandle = FreeFile
Open "C:\PRINTERS.DAT" For Input As #iHandle
Input #iHandle, sPrinter

If Not EOF(iHandle) Then
Input #iHandle, sPrinter
End If

ActivePrinter = sPrinter
Close #iHandle

End Sub
Sub WaitForClose()

Dim iCount As Integer


iCount = Documents.Count

Do Until Documents.Count < iCount
DoEvents
Loop

SetFinePrint
Application.Quit

End Sub


The reason for doing all of this is my need to manipulate print queues
around the opening and closing of the mail merge result document. The
trouble being, that setting ActivePrinter in a word document changes the
system's default print queue, whereas setting it in excel is only
temporary within that workbook, it doesn't change the system default

--
Mike
  #2   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.beginners
external usenet poster
 
Posts: 3
Default Creating a new word doc and putting some VBA code behind it

Hi Mike,

See the article "Changing the selected (current) printer in Word without
changing the system default printer" at:

http://www.mvps.org/word/FAQs/Macros...CurPrinter.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
"Mike NG" wrote in message
...
I am opening a mail merge main document with the following code behind
it. My current method involves controlling everything from the mail
merge main document, with a loop using DoEvents. This seems rather
cumbersome. It would be neat if I could put some VBA behind the
Document_Close event of the mail merge result file. Ideally, I'd like
to pass the queue name by parameter which would be written into my word
document directly, so I don't have to read it from the file again


Option Explicit
Private Sub Document_Open()

Dim sDataSrc As String
Dim sThisDoc As String

sThisDoc = ThisDocument.Name
sThisDoc = Left(sThisDoc, Len(sThisDoc) - 4) & "Data.doc"
sDataSrc = ThisDocument.Path & "\" & sThisDoc

'Skip MailMerge if data source not present....
On Error Resume Next
MailMerge.OpenDataSource sDataSrc
If Err Then GoTo Done
MailMerge.Execute

'This created mail merge document
ActiveDocument.Saved = True
ThisDocument.Saved = True

Done:
SetNonFinePrint
WaitForClose

End Sub
Sub SetFinePrint()

Dim iHandle As Integer
Dim sPrinter As String


'FinePrint will always be the first row in the file
iHandle = FreeFile
Open "C:\PRINTERS.DAT" For Input As #iHandle
Input #iHandle, sPrinter
ActivePrinter = sPrinter
Close #iHandle

End Sub
Sub SetNonFinePrint()

Dim iHandle As Integer
Dim sPrinter As String


'FinePrint will always be the first row in the file
iHandle = FreeFile
Open "C:\PRINTERS.DAT" For Input As #iHandle
Input #iHandle, sPrinter

If Not EOF(iHandle) Then
Input #iHandle, sPrinter
End If

ActivePrinter = sPrinter
Close #iHandle

End Sub
Sub WaitForClose()

Dim iCount As Integer


iCount = Documents.Count

Do Until Documents.Count < iCount
DoEvents
Loop

SetFinePrint
Application.Quit

End Sub


The reason for doing all of this is my need to manipulate print queues
around the opening and closing of the mail merge result document. The
trouble being, that setting ActivePrinter in a word document changes the
system's default print queue, whereas setting it in excel is only
temporary within that workbook, it doesn't change the system default

--
Mike



  #3   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.beginners
external usenet poster
 
Posts: 87
Default Creating a new word doc and putting some VBA code behind it

On Sat, 19 Jul 2003 at 10:19:50, Doug Robbins - Word MVP (Doug Robbins -
Word MVP ) wrote:
See the article "Changing the selected (current) printer in Word without
changing the system default printer" at:

http://www.mvps.org/word/FAQs/Macros...CurPrinter.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

OK thanks. I will give this a go some time, and let you know how I get
on
--
Mike
  #4   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.beginners
external usenet poster
 
Posts: 87
Default Creating a new word doc and putting some VBA code behind it

On Sat, 19 Jul 2003 at 10:19:50, Doug Robbins - Word MVP (Doug Robbins -
Word MVP ) wrote:
See the article "Changing the selected (current) printer in Word without
changing the system default printer" at:

http://www.mvps.org/word/FAQs/Macros...CurPrinter.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

That was superb - worked a treat thanks

I'd still like to put an Application.Quit behind the _Close event of the
mail merge results document though. The only other solution would be to
put some code in the mail merge main document which loops round counting
document count with a DoEvents in between - which seems to be having
slight performance impacts on my system

This is the code in my mail merge main document

Option Explicit
Private Sub Document_Open()

Dim sDataSrc As String
Dim sThisDoc As String


sThisDoc = ThisDocument.Name
sThisDoc = Left(sThisDoc, Len(sThisDoc) - 4) & "Data.doc"
sDataSrc = ThisDocument.Path & "\" & sThisDoc
MailMerge.OpenDataSource sDataSrc
MailMerge.Execute

'Mail merge result document
ActiveDocument.Saved = True

'Mail merge main document
ThisDocument.Close False

Done:

End Sub
--
Mike
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Putting an Excel pivot report in a letter in Word! sumrgrl New Users to Excel 2 June 12th 09 04:20 PM
Excel 2003 - Putting an email/word document into a cell Manawydan Excel Discussion (Misc queries) 0 January 23rd 09 04:13 PM
EVEN AFTER PUTTING CODE IN VB, THE MESSAGE 'WORKBOOK CONTAINS LINK' POPS UP- HOW TO GET OVER THIS CAPTGNVR Excel Discussion (Misc queries) 3 February 3rd 07 08:46 PM
Putting a word into a cell. the-jackal New Users to Excel 2 September 15th 06 08:03 AM
Putting Excel Charts into a Word document that's getting too big Jennifer Charts and Charting in Excel 2 March 10th 06 01:36 AM


All times are GMT +1. The time now is 02:15 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"