Consulting

Results 1 to 2 of 2

Thread: Printing VBA code stopped working

  1. #1

    Smile Printing VBA code stopped working

    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

    Last edited by macropod; 03-05-2019 at 05:14 PM. Reason: Added code tags & formatting

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: https://answers.microsoft.com/en-us/...=1551867571524
    Please read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •