• help to debug VBA code – error 4605

    Author
    Topic
    #497040

    I am getting an “error 4605” This method is not available because the current selection partially covers a plain text content control.

    The program is creating a form. The first part creates header, collects a name and create a file with that name.
    The second part is creating a line with header and a selection box.
    This part should be dumplicated few times.
    The problem is that that I get the error message at the 2nd instance, sometimes at the 3rd or 4th.
    I assume it relates to the layout of the page.

    The first part code:

    Code:
    Sub CreatePFT()
    
        Documents.Add ‘ creating new blank document
    
        Dim zFileName  As String
       
        zFileName = InputBox(“Enter patient name”, “User Entry Required”)
       
        If zFileName = “” Then
            MsgBox “File NOT saved you did not supply a filename!”, _
            vbOKOnly + vbCritical, “Error: Filename missing!”
        Else
           ChangeFileOpenDirectory “C:UsersyigalDocumentsYigalGuy Livnattemp”
         
            ActiveDocument.SaveAs2 filename:=zFileName & “.docm”, FileFormat:= _
                wdFormatXMLDocumentMacroEnabled
    
    Selection.HomeKey wdStory
    ‘ ActiveWindow.View.ReadingLayout = False
    
    With Selection
        .Font.ColorIndex = wdDarkBlue
        .Font.Bold = True
        .Font.Size = 28
        .Font.Underline = True
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .TypeText “PFT Interpretation”
        .TypeParagraph
        .Font.Size = 16
        .TypeText “Dr. Guy Livnat”
        .TypeParagraph
        .TypeParagraph
        .Font.Bold = False
        .Font.Underline = False
        .ParagraphFormat.Alignment = wdAlignParagraphLeft
        .Font.ColorIndex = wdBlack
        .TypeText “The patient name is: ”
        .Font.Underline = True
        .Font.ColorIndex = wdDarkBlue
        .TypeText zFileName
        .TypeParagraph
    End With
    

    The 2nd part, 2 instances:

    Code:
    ‘ Vital Capacity
    With Selection
        ‘ .MoveDown Unit:=wdLine, Count:=7, Extend:=wdExtend
        .MoveDown wdLine, 1
        .Font.ColorIndex = wdDarkBlue
        .Font.Size = 12
        .Font.Bold = False
        .Font.Underline = False
        .TypeText Text:=”The Vital Capacity is:” & vbTab
    End With
    
    Dim objcc1 As ContentControl
        Set objcc1 = Selection.Range.ContentControls.Add(wdContentControlDropdownList)
        objcc1.DropdownListEntries.Add “Normal”
        objcc1.DropdownListEntries.Add “Low Normal”
        objcc1.DropdownListEntries.Add “Increased”
        objcc1.DropdownListEntries.Add “Decreased”
    
    With Selection
        .Font.Bold = False
        .Font.Underline = False
        .ParagraphFormat.Alignment = wdAlignParagraphLeft
        .Font.ColorIndex = wdDarkBlue
        .Font.Size = 12
        .MoveDown Unit:=wdLine, Count:=5
        .TypeParagraph
        .TypeParagraph
        .TypeParagraph
        .TypeParagraph
    End With
        
    ‘——————————————————————————————
    
    
    ‘ Vital Capacity 1
    With Selection
        ‘ .MoveDown Unit:=wdLine, Count:=7, Extend:=wdExtend
        .MoveDown wdLine, 1
        .Font.ColorIndex = wdDarkBlue
        .Font.Size = 12
        .Font.Bold = False
        .Font.Underline = False
        .TypeText Text:=”The Vital1 Capacity is:” & vbTab
    End With
    
    Dim objcc11 As ContentControl
        Set objcc11 = Selection.Range.ContentControls.Add(wdContentControlDropdownList)
        objcc11.DropdownListEntries.Add “Normal”
        objcc11.DropdownListEntries.Add “Low Normal”
        objcc11.DropdownListEntries.Add “Increased”
        objcc11.DropdownListEntries.Add “Decreased”
    
    With Selection
        .Font.Bold = False
        .Font.Underline = False
        .ParagraphFormat.Alignment = wdAlignParagraphLeft
        .Font.ColorIndex = wdDarkBlue
        .Font.Size = 12
        .MoveDown Unit:=wdLine, Count:=1
        [COLOR=”#FFD700″].TypeParagraph[/COLOR]
    End With
        
    

    The error points to the last

    .TypeParagraph

    I marked with yellow.

    Any idea how to solve the problem?

    Viewing 4 reply threads
    Author
    Replies
    • #1472691

      Is there a reason you aren’t using a template with all the formatting & content controls in place? With a template, all the code you posted could be reduced to:

      Code:
      Sub CreatePFT()
      Dim wdDoc As Document, zFileName As String, strFldr As String
      strFldr = "C:UsersyigalDocumentsYigalGuy Livnattemp"
      Set wdDoc = Documents.Add("SomeTemplate")  ' creating new blank document
      With wdDoc
        zFileName = InputBox("Enter patient name", "User Entry Required")
        If zFileName = "" Then
          MsgBox "File NOT saved you did not supply a filename!", _
            vbOKOnly + vbCritical, "Error: Filename missing!"
        Else
          .ContentControls("Patient").Range.Text = zFileName
          .SaveAs2 FileName:=strFldr & zFileName & ".docx", FileFormat:=wdFormatXMLDocument
        End If
      End With
      End Sub

      where SomeTemplate is the full path & name for your template.

      PS: Nothing you’ve posted indicates any need for a docm output file.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1472696

      Thanks for answering. Since I am not an expert, I can only explain what were my assumptions when I answer:

      .docm: I was sure that this is the extension for macro enabled document. At this code, there is no macro, but later I want to add “makePDF” button to create PDF file after the user selects several drop down menus and check boxes.

      About template:
      I understand that template will reduce the code, but it still needs to be created, so we just move the effort to somewhere else.
      I prefer not to go with template at this stage because I know the structure will change dynamically. This whole “mini project” started with “help me to make a simple form” and like every project evolving.
      I prefer to create the form dynamically, by program, and not by WYSIWYG, because it seems better and easier to maintain and understand over time.

      • #1472700

        .docm: I was sure that this is the extension for macro enabled document. At this code, there is no macro, but later I want to add “makePDF” button to create PDF file after the user selects several drop down menus and check boxes.[/quote]
        Sure, the docm extension s for macro-enabled documents but, unless you’re going to start programmatically adding VBA code to the document you’re creating, it’ll never need that format. And if you’re going to go down that path, it means forcing the users to work with Word giving unrestricted access to the VBA project model, which is a serious security risk.

        Instead of adding code to the documents, your button’s code be stored in their template (yet another reason for using one). Do be aware that every document you create uses a template, even if your VBA code doesn’t say to. If you omit it from the Documents.Add code, all that means is Word will use its Normal template.

        About template:
        I understand that template will reduce the code, but it still needs to be created, so we just move the effort to somewhere else.

        That’s the whole point of using a template. You do it once and from then on you don’t need any of the extra code just to do a template’s job. Indeed, depending on what you’re trying to achieve, it’s possible all the code could reside in the template instead of being run from wherever you’re running it now.

        I prefer not to go with template at this stage because I know the structure will change dynamically. This whole “mini project” started with “help me to make a simple form” and like every project evolving.
        I prefer to create the form dynamically, by program, and not by WYSIWYG, because it seems better and easier to maintain and understand over time.

        A template is far easier to develop and maintain in a dynamic environment than hard-coding everything, especially once end-users need to start doing the maintenance for themselves.

        Cheers,
        Paul Edstein
        [Fmr MS MVP - Word]

        • #1472707

          You convinced me. I will create a template. This means I need now to learn how to do it.

          About the docm: you have a good point. But I still need to add a button that will get email address from the user and send a PDF version of the result document by email. Can I do it without a macro? I prefer not to use the file->save and send menu.

          • #1472709

            But I still need to add a button that will get email address from the user and send a PDF version of the result document by email. Can I do it without a macro? I prefer not to use the file->save and send menu.

            You can’t do that without a macro unless you’re using mailmerge (which isn’t the case here). However, if you add the button and the code for it to the template, you can do it all from the new .docx file created from the template, because that file will have both the button and the required access to the template’s code.

            Cheers,
            Paul Edstein
            [Fmr MS MVP - Word]

    • #1472708

      Creating a template is very easy. Simply create a document with all the features you need, then use SaveAs to save it as a template, in the .dot, .dotx or .dotm format, as appropriate. For more information, see: http://office.microsoft.com/en-au/word-help/save-a-word-document-as-a-template-HA101868862.aspx

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1472710

      Thanks. I will do so.

      What about the docm issue: if I need to create a PDF and send by email, do I have to use a macro and maintain the docm or is there a better solution?

    • #1472711

      Thank you!
      Now the ball is in my court: I will use your good ideas, and hope that within few days I will have it working.
      I appreciate your guidance.

    Viewing 4 reply threads
    Reply To: help to debug VBA code – error 4605

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: