VBA: Application.PrintOut - vba

i am using this code to PrintOut a document three times in vba:
...
Application.PrintOut fileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
...
sometimes if i run this line of code, it will stopp working. Word freeze and nothing happend. I can only stop the process. i checked the PrinterSettings by
MsgBox Application.ActivePrinter
The activeprinter exists and is ready. What else can be the problem?
I am using word 2007.
Thanks for hints

Try using Document.PrintOut because Application.PrintOut prints active document, so if you're simultaneously working with another word document you can send to printer your document instead of desired one.

Related

Word VBA is saying "Wrong number of arguments or invalid property assignment" but I think my code is correct

I am working on a Word macro to print a letter and its envelope to two different printers:
Sub PrintEnvelopeAndLetter()
'
' PrintEnvelopeAndLetter Macro
'
'
Dim sCurrentPrinter As String
Dim pageCount As Integer
Dim currentPosition As Range
Set currentPosition = Selection.Range 'Save the current cursor position
pageCount = ActiveDocument.ActiveWindow.Panes(1).Pages.Count
' Prompt the user to ask if they are sure they are ready to print
Result = MsgBox("Are you sure you are ready to print?" & vbCr & vbCr & "Read through your letter one last time.", _
vbYesNo + vbDefaultButton2 + vbApplicationModal + vbExclamation)
If Result = vbNo Then
Exit Sub
End If
' Display a message box asking if the user has entered the correct date
' at the top of their letter.
Result = MsgBox("Did you enter the correct date at the top of your letter?", _
vbYesNo + vbDefaultButton2 + vbApplicationModal + vbExclamation)
If Result = vbNo Then
Exit Sub
End If
' Launch the Spelling and Grammar Check
ActiveDocument.CheckSpelling
ActiveDocument.CheckGrammar
' Save the document to the disk and do not prompt the user
' to save it if the user has made changes
If ActiveDocument.Saved = False Then
ActiveDocument.Save True
End If
' Get the total number of pages in the document. NOTE: The
' envelope counts as a page
sCurrentPrinter = ActivePrinter 'Save the current printer
ActivePrinter = "HP ENVY Photo 7800 series"
Selection.HomeKey Unit:=wdStory ' move to the top of the document (envelope)
Application.PrintOut FileName:="", Range:=wdPrintCurrentPage, Item:= _
wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
ActivePrinter = "HP OfficeJet Pro 8030 series"
For J = pageCount To 2 Step -1
Selection.GoTo wdGoToPage, wdGoToAbsolute, J
Application.PrintOut FileName:="", Range:=wdPrintCurrentPage, Item:= _
wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
Next
ActivePrinter = sCurrentPrinter 'Restore the original printer
' Scroll the insertion point to the very beginning of the document
Selection.HomeKey Unit:=wdStory
' Save the document to the disk and do not prompt the user
' to save it if the user has made changes
If ActiveDocument.Saved = False Then
ActiveDocument.Save NoPrompt:=True
End If
End Sub
I think that my code is correct, but I was hoping if I could get a second pair of eyes. It seems to be indicating that the invalid line is:
ActiveDocument.Save NoPrompt:=True
But I think that it is correct -- see the following link:
https://learn.microsoft.com/en-us/office/vba/api/word.document.save
Consulted the link:
https://learn.microsoft.com/en-us/office/vba/api/word.document.save
I checked my own VBA code and it seems as if I am specifying the NoPrompt property correctly.
I am using Microsoft® Word for Microsoft 365 MSO (Version 2210 Build 16.0.15726.20188) 64-bit on Windows 10. I did not change my code (at least, I do not think so). The macro worked perfectly the day before yesterday.
I have also checked my printers, and both of the printers listed in the macro are installed:
Figure 1. A screenshot of my installed Printers and Scanners.
I am not entirely sure what else to check.
FIRST UPDATE
I commented out both occurrences of the code
' Save the document to the disk and do not prompt the user
' to save it if the user has made changes
If ActiveDocument.Saved = False Then
ActiveDocument.Save NoPrompt:=True
End If
in the macro, and the macro works, so I know for certain that this is the code that it is complaining about. It would seem that the compiler is complaining about a syntax error somewhere, or a function parameter that is not valid. However, every source I've consulted seems to point to this as being valid code, so I am stumped.
I'd really appreciate it if someone could help me out.
Am I doing something wrong?

Trying to use InsertFile Method to APPEND to Word Document

Active Document is called “FinalDocument”
The following is in a loop (ie. Report1, Report2, etc..)
DoCmd.OpenReport ReportName, acPreview, , "Report1”
DoCmd.OutputTo acOutputReport, ReportName, acFormatRTF, “WordDoc1”
ActiveDocument.Content.InsertFile FileName:=WordDoc1, Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
I am successfully getting text from Report1 to FinalDocument.
My problem is that on every pass throught loop, InsertFile is REPLACING text.
I want to APPEND Text from Report1 to FinalDocument thereby building FinalDocument one pass at a time.
You should work with a Range object so that you can target the insertion point. First, set theRange to the entire document, then "collapse" it to a point - think of it like pressing the right-arrow key to make a selection a blinking cursor at the end of the selection.
For example:
Dim rng as Word.Range
Set rng = ActiveDocument.Content
rng.Collapse Word.WdCollapseDirection.wdCollapseEnd. 'Or use 0
rng.InsertFile FileName:=WordDoc1, Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
Try this:
ActiveDocument.ActiveWindow.Selection.InsertFile FileName:=WordDoc1, Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False

Excell Print VBA code combine with VBA Custom Footer

I need to create a macro to print two sheets and include a custom footer that references several of the cells.
I have tried so many combinations, but I don't know what I am doing wrong. I get the error Object does not support this property or method.
Sub PrintSummarySheet()
' PrintSummarySheet Macro
Sheets("Project Data Input").Select
With ActiveSheet.PageSetup
.CenterFooter = .Range("C6").Text And .Range("F2").Text _
And .Range("F4").Text And .Range("F5").Text
End With
Sheets(Array("Project Data Input", "Project Estimate Summary")).Select
Sheets("Project Data Input").Activate
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
Sheets("INSTRUCTIONS").Select
Sheets("Project Data Input").Select
End Sub
You are using With ActiveSheet.PageSetup but on the next line you are trying to refer to the Worksheet and not the PageSetup by doing .Range(...).
You need to replace .Range(...) by ActiveSheet.Range(...).
The Run-time error 13 Type mismatch occurs because you are using And to concatenate text instead of the concatenation operator &
.Range("C6").Text And .Range("F2").Text _
And .Range("F4").Text And .Range("F5").Text
Should be:
.Range("C6").Text & .Range("F2").Text & _
.Range("F4").Text & .Range("F5").Text

strange behaviour in vba macro when adding dynamic hyperlink

I've recorded a macro in word 2007 which looks like this:
Sub Makro1()
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
"http://www.example.com", SubAddress:="", ScreenTip:="", TextToDisplay:= _
"text"
End Sub
In my macro, I want to set address and textToDisplay dynamically, which I tried to do by doing this simple test:
Sub Makro1()
Dim text1 As String
text1 = "joe"
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
"http://www.example.com", SubAddress:="", ScreenTip:="", TextToDisplay:= _
text1
End Sub
When I ran this macro, whole Word crashed and had to recover the document, obviously I'm doing something wrong. Any help appreciated
Thanks
I was having the same problem, and found an answer on the Office Development Center
In short, the problem is in the TextToDisplay parameter. Changing your variable type from String to Variant will work around the problem.
Dim text1 As Variant

Word 2007 vba saveas start at page number lost

In my document I have the Format Page Numbers / Start at: set to 0 so that the title page is not counted.
When I do a SaveAs via VBA the document loses that setting! It was also losing the Different First Page setting so I set that directly in VBA which fixed that problem. I think that because I am formatting the footer via VBA before I do the SaveAs I am somehow affecting the settings? Anyway, I tried setting the Start At page number after the SaveAs but it doesnt set it.
' Save our new Workbook - the output file
' That makes the new file the ActiveDocument
ActiveDocument.SaveAs filename:=fname & ".docx", FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
' Sets this option correctly
ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
' Problem: Doesnt set this option
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers.StartingNumber = 0
' Update the TOC
ActiveDocument.TablesOfContents(1).Update
Any ideas?
Thanks,
Murray
"The RestartNumberingAtSection property, if set to False, will override the StartingNumber property so that page numbering can continue from the previous section." (http://msdn.microsoft.com/en-us/library/office/ff821408.aspx)
Therefore, you have to set the RestartNumberingAtSection property to true:
With ActiveDocument.Sections(1)
.Footers(wdHeaderFooterPrimary).PageNumbers.RestartNumberingAtSection = True
.Footers(wdHeaderFooterPrimary).PageNumbers.StartingNumber = 0
End With
Regards,
Leo