0
因此,基本上我使用vba從ms訪問表生成一個word文檔,然後打印它。
代碼如下:VBA - 使用標準而不是即時打印來打印word文檔與打印輸出
With rs
'ensure the recordset is populated
If Not .BOF And Not .EOF Then
Set objWord = CreateObject("Word.Application")
'objWord.Visible = True
Set doc = objWord.Documents.Add
doc.SaveAs CurrentProject.Path & "\report.doc"
'not necessary but good abtitude
.MoveLast
.MoveFirst
While (Not .EOF)
If Not IsNull(.Fields("REGIONE SOCIALE")) Then
regioneSociale = .Fields("REGIONE SOCIALE").Value
regioneSociale = UCase(regioneSociale)
End If
If Not IsNull(.Fields("INDIRIZZO")) Then
INDIRIZZO = .Fields("INDIRIZZO").Value
End If
If Not IsNull(.Fields("CAP")) Then
CAP = .Fields("CAP").Value
End If
If Not IsNull(.Fields("LOCALITÀ")) Then
LOCALITA = .Fields("LOCALITÀ").Value
End If
If Not IsNull(.Fields("CODICE FISCALE")) Then
codFiscale = .Fields("CODICE FISCALE").Value
End If
If Not IsNull(.Fields("CODICE STALLA")) Then
codStalla = .Fields("CODICE STALLA").Value
End If
If Not IsNull(.Fields("NOTE")) Then
NOTE = .Fields("NOTE").Value
End If
'Debug.Print regioneSociale & CAP & LOCALITA & codFiscale & codStalla & NOTE
objWord.Selection.Font.Bold = True
objWord.Selection.Font.Underline = True
objWord.Selection.TypeText regioneSociale
objWord.Selection.Font.Bold = False
objWord.Selection.TypeParagraph
objWord.Selection.Font.Italic = True
objWord.Selection.TypeText "INDIRIZZO:"
objWord.Selection.Font.Underline = False
objWord.Selection.Font.Italic = False
objWord.Selection.TypeText " " & INDIRIZZO
objWord.Selection.TypeParagraph
objWord.Selection.Font.Underline = True
objWord.Selection.Font.Italic = True
objWord.Selection.TypeText "LOCALITÀ:"
objWord.Selection.Font.Underline = False
objWord.Selection.Font.Italic = False
objWord.Selection.TypeText " " & CAP & " " & LOCALITA
objWord.Selection.TypeParagraph
objWord.Selection.Font.Underline = True
objWord.Selection.Font.Italic = True
objWord.Selection.TypeText "CODICE FISCALE:"
objWord.Selection.Font.Underline = False
objWord.Selection.Font.Italic = False
objWord.Selection.TypeText " " & codFiscale
objWord.Selection.TypeParagraph
objWord.Selection.Font.Underline = True
objWord.Selection.Font.Italic = True
objWord.Selection.TypeText "CODICE STALLA:"
objWord.Selection.Font.Underline = False
objWord.Selection.Font.Italic = False
objWord.Selection.TypeText " " & codStalla
objWord.Selection.TypeParagraph
objWord.Selection.Font.Underline = True
objWord.Selection.Font.Italic = True
objWord.Selection.TypeText "NOTE:"
objWord.Selection.Font.Underline = False
objWord.Selection.Font.Italic = False
objWord.Selection.TypeText " " & NOTE
objWord.Selection.TypeParagraph
objWord.Selection.TypeParagraph
.MoveNext
Wend
Else
MsgBox "No record selected/empty table"
Exit Sub
End If
.Close
End With
doc.Save
'this instruction gives immediate printing instead of the standard one...
doc.PrintOut , , , CurrentProject.Path & "\report.doc", , , , , , , True
doc.Close
事實是,其實我想給用戶在打印前更改打印機或任何其他選項的可能性。
打印之前,而不是立即打印文檔。
vba有可能嗎?代碼
片段,我用的@Sergey建議:
End With
objWord.Dialogs(wdDialogFilePrint).Show
doc.Save
doc.Close
感謝您的建議。然而,'objWord.Dialogs(wdDialogFilePrint).Show'會使Access凍結(代碼保持執行狀態),並且只有在流程管理器終止它之後,它纔會顯示打印對話窗口......有些東西不能按預期工作 – UrbiJr
它適合我。確保所有Word進程在執行前都被終止。你也可以顯示word窗口進行調試:'objWord.Visible = True' –
哦,現在我得到了什麼錯誤。事實是,打印對話窗口與Word應用程序有關。所以如果我以前沒有打開它,它基本上等待它被打開。實際上,使用'.Visible = True'可以工作,但是它會打開一個Word窗口,我不想... – UrbiJr