PDA

View Full Version : Printing VBA code stopped working



farrahmna
03-05-2019, 04:36 PM
Hello,
I am having trouble with this code, which was working perfectly until it stopped printing all of a sudden or only prints the very last page. After testing it a little I figured it was still identifying the right pages but not giving the command to print. I bolded and highlighted below the part I think is causing the issue. When I deleted the "Range" part it printed out the whole document normally.
I am not sure how to solve this issue so any help is appreciated.


Sub printDCSum()

Dim endCount As Integer
Dim pag1 As String
Dim blnFound As Boolean
Dim diTag As Boolean
Dim inaccurateString As String
Dim cc As ContentControl
Dim answer As Boolean


ActiveDocument.Range.Select
Selection.Find.Text = "Discharge Instructions"
blnFound = Selection.Find.Execute

endCount = ActiveDocument.ComputeStatistics(wdStatisticPages)
startCount = Selection.Information(wdActiveEndAdjustedPageNumber)
pag1 = startCount & "-" & endCount - 1
pag2 = endCount & "-" & endCount
'MsgBox (pag1) test message box

'''first make sure that every content control in the discharge summary is autofilled. If a content control cannot be found then set boolean to notify user to check D/C summary manually.

''unlock all content controls
For Each cc In ActiveDocument.ContentControls
cc.LockContentControl = False
Next



'check the following variables and replace if inaccurate
Call setDCVal("Rec Diet", "Rec Diet", inaccurateString)


''re-lock controls
For Each cc In ActiveDocument.ContentControls
cc.LockContentControl = True
Next

''if there are inaccurate variables, ask the user if they would like to print anyway
answer = True ''defaul answer as true
If inaccurateString <> "" Then
answer = MsgBox(inaccurateString & " could not be verified by the code. Would you still like to print?", vbYesNo)
End If

If answer = True Then
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Copies:=2, Pages:=pag1

Set ccGroup = ActiveDocument.SelectContentControlsByTitle("Data Entry Value")
For Each cc In ccGroup
If cc.Range.Text <> "None" Then diTag = True
Next cc

Set ccGroup = ActiveDocument.SelectContentControlsByTitle("Record Retrieval Value")
For Each cc In ccGroup
If cc.Range.Text <> "None" Then diTag = True
Next cc
Set ccGroup = ActiveDocument.SelectContentControlsByTitle("Special Instructions Value")
For Each cc In ccGroup
If cc.Range.Text <> "None" Then diTag = True
Next cc

If diTag Then
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Copies:=1, Pages:=pag2
'MsgBox ("Data If Activated")
End If

MsgBox ("Discharge Summary Printed")

Else
MsgBox ("Discharge Summary was not printed") '04.06.18 ls
End If


End Sub

macropod
03-06-2019, 03:20 AM
Cross-posted at: https://answers.microsoft.com/en-us/msoffice/forum/all/printing-vba-word-code-issue/1a4b92b9-5589-4546-a8ea-945b7a884090?rtAction=1551867571524
Please read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3