注册 登录
编程论坛 VB6论坛

vb与ms publisher打印

houyoung 发布于 2013-10-05 11:22, 721 次点击
我需要用excel来控制打印对应ms publihser文档,假设:文档名为"c:\1009.pub",该文档已经设置过自定义页面,如19cm*6.6cm,打印机中有自定义纸型6.6cm*19cm,名字叫200M
我的vba代码如下:
Sub Fprint()
Dim oAdd As Object
Dim apppub As Object '
Dim pub As Publisher.Application
Dim pubInstalledPrinters As Publisher.InstalledPrinters
Dim pubApplication As Publisher.Application
Dim pubPrinter As Publisher.Printer
Dim FileName As String
FileName = "c:\1009.pub"
Application.EnableEvents = False

Set apppub = CreateObject("Publisher.Application")
On Error GoTo ErrorHandler
apppub.Open FileName, False, True, pbDoNotSaveChanges

Set pubApplication = ActiveDocument.Application'''可能报错的位置
Set pubInstalledPrinters = pubApplication.InstalledPrinters

apppub.ActiveWindow.Visible = True
For Each pubPrinter In pubInstalledPrinters
If pubPrinter.IsActivePrinter Then
 
ActiveDocument.PageSetup.PageHeight = Publisher.CentimetersToPoints(6.6)
ActiveDocument.PageSetup.PageWidth = Publisher.CentimetersToPoints(19)
pubPrinter.PaperHeight = Publisher.CentimetersToPoints(19)
pubPrinter.PaperWidth = Publisher.CentimetersToPoints(6.6)
pubPrinter.PaperOrientation = pbOrientationLandscape

ActiveDocument.PrintOutEx FROM:=1, To:=1, COPIES:=1
End If
Next

 apppub.Quit
   Set apppub = Nothing
   Exit Sub

ErrorHandler:
   apppub.Quit
   Set apppub = Nothing

end sub
结果有2个问题:1、打印纸型不对,尺寸是A4,且文档被打印在纸张最中央;2、如果打印一次该文档后,不关闭excel,直接再打印一次,462错误,远程服务器不存在或不可用,调试就落在标记可能错误的地方那个,请大家给与解答。
6 回复
#2
houyoung2013-10-05 12:55
有偿
#3
bczgvip2013-10-05 17:48
Set pubApplication = apppub.ActiveDocument.Application ' 试试吧。
#4
houyoung2013-10-05 23:11
谢谢,462的错误是windows自身资源不能很快释放造成的,如何能加速它释放资源呢?
#5
houyoung2013-10-05 23:12
该问题是有偿的,不是20分的问题
#6
houyoung2013-10-06 10:12
期待高手
#7
Artless2013-10-06 11:49
以下是引用houyoung在2013-10-5 11:22:47的发言:

我需要用excel来控制打印对应ms publihser文档,假设:文档名为"c:\1009.pub",该文档已经设置过自定义页面,如19cm*6.6cm,打印机中有自定义纸型6.6cm*19cm,名字叫200M
我的vba代码如下:
Sub Fprint()
Dim oAdd As Object
Dim apppub As Object '
Dim pub As Publisher.Application
Dim pubInstalledPrinters As Publisher.InstalledPrinters
Dim pubApplication As Publisher.Application
Dim pubPrinter As Publisher.Printer
Dim FileName As String
FileName = "c:\1009.pub"
Application.EnableEvents = False

Set apppub = CreateObject("Publisher.Application")
On Error GoTo ErrorHandler
apppub.Open FileName, False, True, pbDoNotSaveChanges

Set pubApplication = ActiveDocument.Application'''可能报错的位置
Set pubInstalledPrinters = pubApplication.InstalledPrinters

apppub.ActiveWindow.Visible = True
For Each pubPrinter In pubInstalledPrinters
If pubPrinter.IsActivePrinter Then
 
ActiveDocument.PageSetup.PageHeight = Publisher.CentimetersToPoints(6.6)
ActiveDocument.PageSetup.PageWidth = Publisher.CentimetersToPoints(19)
pubPrinter.PaperHeight = Publisher.CentimetersToPoints(19)
pubPrinter.PaperWidth = Publisher.CentimetersToPoints(6.6)
pubPrinter.PaperOrientation = pbOrientationLandscape

ActiveDocument.PrintOutEx FROM:=1, To:=1, COPIES:=1
End If
Next

 apppub.Quit
   Set apppub = Nothing
   Exit Sub

ErrorHandler:
   apppub.Quit
   Set apppub = Nothing

end sub
结果有2个问题:1、打印纸型不对,尺寸是A4,且文档被打印在纸张最中央;2、如果打印一次该文档后,不关闭excel,直接再打印一次,462错误,远程服务器不存在或不可用,调试就落在标记可能错误的地方那个,请大家给与解答。

以下是引用houyoung在2013-10-5 23:11:28的发言:

谢谢,462的错误是windows自身资源不能很快释放造成的,如何能加速它释放资源呢?


在代码中设置纸张类型。阻止用户太快再次打印比vb加速windows释放资源可能更好。

[ 本帖最后由 Artless 于 2013-10-6 11:51 编辑 ]
1