Results 1 to 14 of 14
  1. #1
    Spilloss77 is offline Novice
    Windows 11 Office 365
    Join Date
    Apr 2024
    Posts
    7

    Bookmarks and VBA

    Hi, my name is Sandra and I need help.
    I have a database with a form that allows me to fill out a word sheet using bookmarks.
    I have the following problem:


    - I have a combo box with 2 value: Option1 and Option2;
    - When I select Option1 then I need the following writing in bold centered text to appear in the Word sheet: "Lorem Ipsum etc...";
    If I select Option 2 instead, nothing should appear
    Can anyone help me?

    Code:
    Private Sub Comando171_Click()
    Call ActivateWord
    Call Avviso369
    End Sub
    
    
    Private Sub Avviso369()
    
    
     Dim appword As Word.Application
     Dim doc     As Word.Document
     Dim fPath   As String
     Dim FName   As String
     Dim ReplSel As Boolean
     Dim i As Integer
    
    
     On Error Resume Next
     Set appword = GetObject(, "Word.Application")
     Error.Clear
     If Err.Number <> 0 Then
        Set appword = CreateObject("Word.Application")
     End If
     
     On Error GoTo 0
     
     fPath = "C:\415\Modelli\"
     FName = "Avviso369.dotx"
     
     appword.Visible = True
       
        appword.Activate
    
    
        ReplSel = appword.Options.ReplaceSelection
        appword.Options.ReplaceSelection = True
    
    
        Set doc = appword.Documents.Add(fPath & FName)
       
        doc.Activate
        
        If doc.Bookmarks.Exists(Tipo_Dif_Indag_1) Then
        doc.Bookmarks("avviso369bis").Select
    
    
        If doc.Bookmarks(Me.Tipo_Dif_Indag_1) = "Option1 " Then
        appword.Selection.TypeText Me."Lorem Ipsum etc..."
        
    Else
        appword.Selection.TypeText ""
    
    
    End If
    End If
    
    
    End Sub

  2. #2
    madpiet is online now Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    156
    Quote Originally Posted by Spilloss77 View Post
    Hi, my name is Sandra and I need help.
    I have a database with a form that allows me to fill out a word sheet using bookmarks.
    I have the following problem:
    - I have a combo box with 2 value: Option1 and Option2;
    - When I select Option1 then I need the following writing in bold centered text to appear in the Word sheet: "Lorem Ipsum etc...";
    If I select Option 2 instead, nothing should appear
    Can anyone help me?

    Code:
    Private Sub Comando171_Click()
    Call ActivateWord
    Call Avviso369
    End Sub
    
    
    Private Sub Avviso369()
    
    
     Dim appword As Word.Application
     Dim doc     As Word.Document
     Dim fPath   As String
     Dim FName   As String
     Dim ReplSel As Boolean
     Dim i As Integer
    
    
     On Error Resume Next
     Set appword = GetObject(, "Word.Application")
     Error.Clear
     If Err.Number <> 0 Then
        Set appword = CreateObject("Word.Application")
     End If
     
     On Error GoTo 0
     
     fPath = "C:\415\Modelli\"
     FName = "Avviso369.dotx"
     
     appword.Visible = True
       
        appword.Activate
    
    
        ReplSel = appword.Options.ReplaceSelection
        appword.Options.ReplaceSelection = True
    
    
        Set doc = appword.Documents.Add(fPath & FName)
       
        doc.Activate
        
        If doc.Bookmarks.Exists(Tipo_Dif_Indag_1) Then
        doc.Bookmarks("avviso369bis").Select
    
    
        If doc.Bookmarks(Me.Tipo_Dif_Indag_1) = "Option1 " Then
        appword.Selection.TypeText Me."Lorem Ipsum etc..."
        
    Else
        appword.Selection.TypeText ""
    
    
    End If
    End If
    
    
    End Sub
    You only need to code for the True part... there's no Else part in your example, because it amounts to "do nothing"/ "insert nothing". Or am I misreading something?

  3. #3
    Spilloss77 is offline Novice
    Windows 11 Office 365
    Join Date
    Apr 2024
    Posts
    7
    How?
    I'm not an expert, can you help me to understand with an example?

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,945
    I think you have it backwards.
    You are checking for the bookmark when it could be Option 2?

    So I would swap the If checks around.
    Unless that bookmark is set as bold in the document, you would also need to bold the text.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Spilloss77 is offline Novice
    Windows 11 Office 365
    Join Date
    Apr 2024
    Posts
    7
    Quote Originally Posted by Spilloss77 View Post
    How?
    I'm not an expert, can you help me to understand with an example?
    In this way?

    Code:
        If doc.Bookmarks(Me.Tipo_Dif_Indag_1) = "difeso d'ufficio" Then
            appword.Selection.TypeText Me.avviso369bis
            
        If doc.Bookmarks.Exists(Tipo_Dif_Indag_1) Then
            doc.Bookmarks("avviso369bis").Select
     
        Else
            appword.Selection.TypeText ""
        End If
        End If
    It doesn't work!
    Any other suggestion?

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Try this please (make your you change the name of the combo to match yours):
    Code:
    If Me.cboYourComboName = "Option1" Then  'doc.Bookmarks(Me.Tipo_Dif_Indag_1) = "Option1 " Then
        If doc.Bookmarks.Exists(Tipo_Dif_Indag_1) Then 
    	doc.Bookmarks("avviso369bis").Select 
    	appword.Selection.TypeText "Lorem Ipsum etc..."
        End If    
    End If
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  7. #7
    Spilloss77 is offline Novice
    Windows 11 Office 365
    Join Date
    Apr 2024
    Posts
    7
    Hi Gicu... I edited your code with my comboname and Bookmark.

    If Me.Tipo_Dif_Indag_1 = "difeso d'ufficio" Then ' If doc.Bookmarks(Me.Tipo_Dif_Indag_1) = "difeso d'ufficio " Then
    If doc.Bookmarks.Exists(Tipo_Dif_Indag_1) Then
    doc.Bookmarks("avviso369bis").Select
    appword.Selection.TypeText "INFORMAZIONE DI GARANZIA E INFORMAZIONE SUL DIRITTO DI DIFESA (artt. 369 e 369-bis c.p.p.)"
    End If
    End If

    But it doesn't work. It doesn't go in error, but avviso369bis bookmarks doesn't show "Lorem Ipsum " text
    In your comment there is another If... ???

  8. #8
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Code:
    If Me.Tipo_Dif_Indag_1 = "difeso d'ufficio" Then ' If doc.Bookmarks(Me.Tipo_Dif_Indag_1) = "difeso d'ufficio " Then
    If Me.Tipo_Dif_Indag_1 is your combobox, I doubt that it would return a value that includes a trailing space. Try "difeso d'ufficio"

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    @Spilloss77:
    You check for the existence of one bookmark (and by the way you need to enclose its name in double-quotes: ...Exists("Tipo_Dif_Indag_1")) but you select another (doc.Bookmarks("avviso369bis").Select).
    Not sure which of the Ifs you think is extra, the second one on the first line doesn't count, it is just your original line for your reference. Then you have on if statement that checks for the value of your combo and a second nested one to check for the existence of the bookmark.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    Spilloss77 is offline Novice
    Windows 11 Office 365
    Join Date
    Apr 2024
    Posts
    7
    Hi, actually I want to check:
    - that the item "Difeso d'ufficio" has been chosen in the Tipo_Dif_Indag_1 combo
    - If it is true, then I have to select the Bookmarks avviso369bis and insert the text "Lorem Impsum etc..." in this last bookmark.

    Wooooowwwwww IT WORKS!!!!!!

    Code:
    If Me.Tipo_Dif_Indag_1 = "difeso d'ufficio" Then ' If doc.Bookmarks(Me.Tipo_Dif_Indag_1) = "difeso d'ufficio " Then
    If doc.Bookmarks.Exists("Tipo_Dif_Indag_1") Then
    doc.Bookmarks("avviso369bis").Select
    appword.Selection.TypeText "INFORMAZIONE DI GARANZIA E INFORMAZIONE SUL DIRITTO DI DIFESA (artt. 369 e 369-bis c.p.p.)"
    End If
    End If

    I have a question:
    - I can also insert 2 other bookmarks "avviso369bis1" and "avviso369bis2" because this work should make more text appear inside the word document.


    I attach a screenshot of the text that now appears at the top of the Word document. Is there a way to wrap up the text "(articles 369 and 369-bis c.p.p.)" at the moment is divided in two row
    Click image for larger version. 

Name:	Senza titolo.jpg 
Views:	19 
Size:	43.9 KB 
ID:	51685

  11. #11
    madpiet is online now Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    156
    If by "wrap up" the text, you mean to force it to show on a single line, then your only option is to make the font smaller.

    If you want to split the two so that "(artt. 369 e 369-bis c.p.p.)" appears on the next line by itself, you would need to insert a paragraph after the AppWord.Selection.TypeText line. something like AppWord.Selection.Paragraphs.Addappword.Selection.TypeText "INFORMAZIONE DI GARANZIA E INFORMAZIONE SUL DIRITTO DI DIFESA (artt. 369 e 369-bis c.p.p.)"

  12. #12
    Spilloss77 is offline Novice
    Windows 11 Office 365
    Join Date
    Apr 2024
    Posts
    7
    In this way, it doesn't work

    If Me.Tipo_Dif_Indag_1 = "difeso d'ufficio" Then ' If doc.Bookmarks(Me.Tipo_Dif_Indag_1) = "difeso d'ufficio " Then
    If doc.Bookmarks.Exists("Tipo_Dif_Indag_1") Then
    doc.Bookmarks("avviso369bis").Select
    appword.Selection.TypeText "INFORMAZIONE DI GARANZIA E INFORMAZIONE SUL DIRITTO DI DIFESA "
    appword.Selection.Paragraphs.Addappword.Selection. TypeText "(artt. 369 e 369-bis c.p.p.)"


    End If
    End If

    Error ...

    Click image for larger version. 

Name:	Cattura.JPG 
Views:	16 
Size:	39.0 KB 
ID:	51686

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,945
    I would have thought that it would be
    Code:
    appword.Selection.Paragraphs.Add appword.Selection.TypeText "(artt. 369 e 369-bis c.p.p.)"
    
    


    So a simple typo, but shows me you are not understanding the code?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  14. #14
    Spilloss77 is offline Novice
    Windows 11 Office 365
    Join Date
    Apr 2024
    Posts
    7
    I don't know the world of programming, but I often find myself creating small applications to help me with my work.
    And this is one of those cases.
    I hope it's not a problem.

    It doesn't work

    Code:
    If Me.Tipo_Dif_Indag_1 = "difeso d'ufficio" Then ' If doc.Bookmarks(Me.Tipo_Dif_Indag_1) = "difeso d'ufficio " ThenIf doc.Bookmarks.Exists("Tipo_Dif_Indag_1") Then
    doc.Bookmarks("avviso369bis").Select
    appword.Selection.TypeText "INFORMAZIONE DI GARANZIA E INFORMAZIONE SUL DIRITTO DI DIFESA "
    appword.Selection.Paragraphs.Add appword.Selection.TypeText "artt. 369 e 369-bis c.p.p."
    
    
    
    
    End If
    End If

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 12-15-2016, 12:29 PM
  2. MS Word template with bookmarks
    By jonnyh in forum Import/Export Data
    Replies: 2
    Last Post: 09-07-2016, 07:42 AM
  3. Access Form to Word Bookmarks
    By markod in forum Forms
    Replies: 5
    Last Post: 12-27-2010, 05:18 PM
  4. line skip in bookmarks
    By emilylu3 in forum Programming
    Replies: 0
    Last Post: 03-21-2006, 02:58 PM
  5. font of word bookmarks
    By emilylu3 in forum Programming
    Replies: 1
    Last Post: 03-09-2006, 12:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums