Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 08-17-2021, 08:36 PM
gmayor's Avatar
gmayor gmayor is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Quote:
Originally Posted by gorkac View Post
Is this that you just wrote possible to do with?
https://www.gmayor.com/insert_content_control_addin.htm
The code I posted uses bookmarks as requested. If you want to use content controls then

Code:
Private Sub CommandButton2_Click()
Dim oCC As ContentControl
Dim sVia As String, lPK As String, sTermPart As String, arr() As String
    sVia = ComboVia.Text
    lPK = textPK.value

    Select Case sVia
        Case Is = "A-68"
            If lPK < 85 Then
                sTermPart = "Castejon|Tudela"
            ElseIf lPK < 99 Then
                sTermPart = "Corella|Estella"
            Else
                sTermPart = "Fontellas|Tafalla"
            End If
        Case Is = "NA-3010"
            If lPK < 25 Then
                sTermPart = "Cortes|Tudela"
            ElseIf lPK < 35 Then
                sTermPart = "Ribaforada|Tafalla"
            Else
                sTermPart = "Novillas|Estella"
            End If
    End Select
    If UBound(arr) > 0 Then
        arr = Split(sTermPart, "|")
        For Each oCC In ActiveDocument.ContentControls
            Select Case oCC.Title
                Case Is = "termino"
                    oCC.Range.Text = arr(0)
                Case Is = "partido"
                    oCC.Range.Text = arr(1)
            End Select
        Next oCC
    End If
lbl_Exit:
    Set oCC = Nothing
    Exit Sub
End Sub

__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #17  
Old 08-18-2021, 01:55 AM
Guessed's Avatar
Guessed Guessed is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I've looked over all this thread and think the solution is growing in complexity with considerable repetitive code to still be written.

I suggest taking a reset and look at this from a different perspective. Attached is an example which aligns with what I think the OP wanted. It avoids the userform in favour of mapped Content Controls and either CC properties or macros to populate the CC values.
Attached Files
File Type: docm DemoHighwayCCs.docm (37.8 KB, 5 views)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #18  
Old 08-18-2021, 02:20 AM
gorkac gorkac is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2019
Advanced Beginner
Help with bookmarks in word.
 
Join Date: Jul 2021
Posts: 62
gorkac is on a distinguished road
Default

Thank you very much, I will try it. I can't put the code like this "If lPK <85 Then ..." because they are roads and their miles (kilometros). Depending on which miles we talk between, one "termino" or another will be written. PK are "kilometros" (unit of measurement on European roads, you use the mile).
There are roads ("carreteras") that do not start at zero, but can start at mile (kilometro) 12.400 and end at mile (kilometro) 36.700, for example.
Reply With Quote
  #19  
Old 08-18-2021, 04:28 AM
gorkac gorkac is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2019
Advanced Beginner
Help with bookmarks in word.
 
Join Date: Jul 2021
Posts: 62
gorkac is on a distinguished road
Default Perfect

This code works perfect. One question, is it possible to put decimal values? Instead of "If lPK > 81 And lPK < 85 Then..." something like that "If lPK > 81.720 And lPK < 85.240 Then..."



Quote:
Originally Posted by gmayor View Post
You probably need something like the following, but if there are lots of these values as seems to be the case you would be better putting the codes and their values in an Excel worksheet and reading them from the worksheet.

Code:
Private Sub CommandButton2_Click()
Dim sVia As String
Dim lPK As String
    sVia = ComboVia.Text
    lPK = textPK.Value
    Select Case sVia
        Case Is = "A-68"
            If lPK > 81 And lPK < 85 Then
                Rellenar "termino", "Castejon"
                Rellenar "partido", "Tudela"
            End If
            If lPK > 84 And lPK < 99 Then
                Rellenar "termino", "Corella"
                Rellenar "partido", "Estella"
            End If
            If lPK > 98 And lPK < 113 Then
                Rellenar "termino", "Fontellas"
                Rellenar "partido", "Tafalla"
            End If
        Case Is = "NA-3010"
            If lPK > 20 And lPK < 25 Then
                Rellenar "termino", "Cortes"
                Rellenar "partido", "Tudela"
            End If
            If lPK > 24 And lPK < 35 Then
                Rellenar "termino", "Ribaforada"
                Rellenar "partido", "Tafalla"
            End If
            If lPK > 34 And lPK < 44 Then
                Rellenar "termino", "Novillas"
                Rellenar "partido", "Estella"
            End If
    End Select
lbl_Exit:
    Exit Sub
End Sub
Reply With Quote
  #20  
Old 08-18-2021, 03:48 PM
Guessed's Avatar
Guessed Guessed is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Are you planning on coding all the information or read it from a table?

It is not a stretch to deal with decimal places or starting distances but you need to decide on how all the data is going to be sourced.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #21  
Old 08-18-2021, 04:00 PM
gorkac gorkac is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2019
Advanced Beginner
Help with bookmarks in word.
 
Join Date: Jul 2021
Posts: 62
gorkac is on a distinguished road
Default

I code all the information, I have already started and it works fine.
How do I implement error checks? If I have a value that does not exist, how do I not fill in what I want?
Example:
Case Is = "NA-3010"
If lPK > 20 And lPK < 25 Then
Rellenar "termino", "Cortes"
Rellenar "partido", "Tudela"
End If
If lPK > 24 And lPK < 35 Then
Rellenar "termino", "Ribaforada"
Rellenar "partido", "Tafalla"
End If
If lPK > 34 And lPK < 44 Then
Rellenar "termino", "Novillas"
Rellenar "partido", "Estella"
End If
End Select
As I control in the case that the user enters for example the lPK 67 (does not exist)
Reply With Quote
  #22  
Old 08-18-2021, 04:14 PM
Guessed's Avatar
Guessed Guessed is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You need to join the If statements to isolate the invalid ranges. Look back over the provided code for instances of ElseIf to see the structure you need to show invalid distances. The basic premise is as follows.

If test then
' do something
ElseIf test2 then
' do something else
Else
' what happens when the distance isn't valid
End If
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #23  
Old 08-19-2021, 12:21 PM
gorkac gorkac is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2019
Advanced Beginner
Help with bookmarks in word.
 
Join Date: Jul 2021
Posts: 62
gorkac is on a distinguished road
Default

What IDE or code editor do you use for vba word?
Reply With Quote
  #24  
Old 08-19-2021, 03:18 PM
Guessed's Avatar
Guessed Guessed is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

For me it is Word's own VBA Editor - opened from Word by pressing Alt-F11
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #25  
Old 08-20-2021, 10:02 AM
gorkac gorkac is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2019
Advanced Beginner
Help with bookmarks in word.
 
Join Date: Jul 2021
Posts: 62
gorkac is on a distinguished road
Default

Yeah yeah, I work with that one. I was asking if you knew something better
Reply With Quote
  #26  
Old 08-21-2021, 12:04 PM
gorkac gorkac is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2019
Advanced Beginner
Help with bookmarks in word.
 
Join Date: Jul 2021
Posts: 62
gorkac is on a distinguished road
Default

Hi guys, one last help. As the procedure is very long, I get an error.
I'll give you the file, let's see how I can divide the "carreteras" routine into several parts. Notice, I still have to insert half of the information.

Thanks in advance.
Reply With Quote
  #27  
Old 08-21-2021, 12:07 PM
gorkac gorkac is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2019
Advanced Beginner
Help with bookmarks in word.
 
Join Date: Jul 2021
Posts: 62
gorkac is on a distinguished road
Default The document.

I forgot to upload the document
Attached Files
File Type: dotm PLANTILLA NUEVA SIP automatica (2).dotm (164.5 KB, 6 views)
Reply With Quote
  #28  
Old 08-21-2021, 12:25 PM
gorkac gorkac is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2019
Advanced Beginner
Help with bookmarks in word.
 
Join Date: Jul 2021
Posts: 62
gorkac is on a distinguished road
Default

I see that the problem is that in the same procedure, I have written more than 6000 lines of code.
How can i divide it?
Reply With Quote
  #29  
Old 08-21-2021, 12:48 PM
gorkac gorkac is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2019
Advanced Beginner
Help with bookmarks in word.
 
Join Date: Jul 2021
Posts: 62
gorkac is on a distinguished road
Default

Sorry, too long procedure is in the form
Reply With Quote
  #30  
Old 08-21-2021, 04:44 PM
Guessed's Avatar
Guessed Guessed is offline Help with bookmarks in word. Windows 10 Help with bookmarks in word. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I provided what I thought were quite valid ways to simplify the coding effort and streamline the operation of what you were trying to do. You've ignored those suggestions to date. If you want to solve your issue, go back through this thread and spend some time reading and understanding what was already offered.

The absolute shortest way to do the coding is to read the values from an Excel spreadsheet. Coding all the values as you have been doing is simply not efficient and useful only for demonstration purposes.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving a word doc as a pdf with bookmarks MimiCush Word 10 03-26-2018 01:51 PM
Help with bookmarks in word. Deleting only all bookmarks contents not the bookmarks adilprodigy Word VBA 1 10-11-2017 01:31 PM
Help with bookmarks in word. Form updating Bookmarks - writes to the bookmarks multiple times PeterPlys Word VBA 13 01-14-2015 06:41 AM
Help with bookmarks in word. selecting ms word bookmarks using vba dnc Word VBA 4 05-10-2013 04:58 PM
word 2007 bookmarks Dawn Word 0 07-14-2009 01:00 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:56 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft