#1
|
|||
|
|||
Help with bookmarks in word.
Hi.
I am creating a form for Word that works with multiple bookmarks, a combo, etc. It consists of: 1 form: with code to fill in the bookmarks and clean them. Several modules and 1 combobox for VIA. The Word sheet has markers: denomination, term and party. The combo will load 1 data "VIA" and a numerical value "PK". for example: for "VIA" = NA-3010 and for "PK" = 12 So for each VIA. I also need that, if I choose between PK 0 and 12, in the "termino" bookmark put a data: CORTES If I choose between PK 12.1 and 20, in the "termino" bookmark put another different data: BUÑUEL and if I choose between PK 21 and 32, in the "termino" bookmarks indicate: FUSTIÑANA and so on. To make matters worse, the "match" bookmarks can also vary the data, depending on all of the above. I would need the simplest code, because I am not going to work only with two conditions, I explain: There are highways, for example the N-121, which has several "termino", some of these terminos depend on one party or another. Sorry to bother but I got stuck. I upload the file. Thanks in advance. |
#2
|
||||
|
||||
Your attachment does not contain any code?
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Sorry
The pdf "TERMINOS Y KILOMETROS" is what I need to pass to the Word form, but I only filled in the VIA and the Pk completed the bookmark named denominacion, termino and partido.
it is understood? Sorry for my english. |
#4
|
||||
|
||||
Frankly I have no idea what it is that you are trying to do, not helped by my unfamiliarity with Spanish.
Your 'What I Want' document seems to bear no relationship to your template, and trying to work with a PDF file of data is not really practicable. I would start by opening that PDF in Word and save it as a document. You can then populate your userform combobox as follows: Code:
Dim oDoc As Document Dim oTable As Table Dim oRng As Range Dim sName As String Set oDoc = Documents.Open(ThisDocument.Path & "\TERMINOS Y KILOMETROS.docx") For Each oTable In oDoc.Tables Set oRng = oTable.Cell(1, 1).Range oRng.End = oRng.End - 1 sName = Split(oRng.Text, " ")(0) cbovia.AddItem sName Next oTable As for the rest it is anyone's guess what you want. However if you have the converted data document open you can interrogate the tables to get what information you want for your document. I would suggest that you use content controls, rather than bookmarks which are easily deleted.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
Sorry, FORGET the pdf document "TERMS AND KILOMETERS" and the word document "WHAT I WANT".
Let's start over. I have a word document named , when one user open it, this form appears: The user will enter a "VIA" and a "PK". Example: if in the "VIA" combo user enter A-68 and in the textbox: 12, I need that in the bookmarkers "termino", "denominacion" and "partido", it is filled automatically with and put "Tudela" in the marker "termino"; "Cortes-Buñuel" on the "denominacion" bookmark and "Tudela" on the "partido" bookmark. I leave some photos to show the idea. |
#6
|
|||
|
|||
When the user clicks "Aceptar", the bookmarks are automatically filled in in word.
This is what I need to program. Help me please. |
#7
|
||||
|
||||
OK. A-68 and 12 come from the userform. Where do the other pieces of information 'denominacion'. 'termino' and 'partido' come from?
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#8
|
|||
|
|||
That is what I want to program.
|
#9
|
|||
|
|||
In the combobox "CargarCarreteras" I already have the items I need to fill the "VIA" bookmark.
In the Module "formulario" I have the code created by you to fill markers, I have called it "Rellenar"(in English it is written "Fill"): #Public Sub Fill(strMarkerName As String, strValue As String). 'Gorka Cuairan. Function that fills markers Dim Rng As Range With ActiveDocument On Error GoTo lbl_Exit Set Rng = .Bookmarks(strMarkerName).Range Rng.Text = strValue Rng.Bookmarks.Add strMarkerName End With lbl_Exit: Set Rng = Nothing Exit Sub End Sub The idea is that in the Module "termino", I enter the values I need, but I don't know how to do it and I don't know how to pass it to the variable strValue of the previous procedure. The same for the Modules "denominacion" and "partido". |
#10
|
||||
|
||||
The 'Fill' macro will write the values to the document.e.g. from your document
Code:
Private Sub CommandButton2_Click() 'BOTON Limpiar' Dim i As Long Dim strNombre As String For i = 1 To ActiveDocument.Bookmarks.Count strNombre = ActiveDocument.Bookmarks(i).Name Select Case strNombre Case Is = "via" Rellenar strNombre, cbovia.Value Case Is = "kilometro" Rellenar strNombre, txtpk.Text Case Is = "partido" Rellenar strNombre, "partido value" Case Is = "termino" Rellenar strNombre, "termino value" End Select Next i lbl_Exit: Exit Sub End Sub Or if you follow my suggestion and replace the bookmarks with content controls (Insert Content Control Add-In will do that) then Code:
Private Sub CommandButton2_Click() 'BOTON Limpiar' Dim oCC As ContentControl For Each oCC In ActiveDocument.ContentControls Select Case oCC.Title Case Is = "via" oCC.rage.Text = cbovia.Value Case Is = "kilometro" oCC.Range.Text = txtpk.Text Case Is = "partido" oCC.Range.Text = "partido value" Case Is = "termino" oCC.Range.Text = "termino value" End Select Next oCC 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 |
#11
|
|||
|
|||
I'm sorry I didn't answer before, I've been away for a few days.
I am not able to obtain the desired result, I insist that you can give me a practical example: I am confident that you teach me to program one of the examples EXAMPLE 1 or EXAMPLE 2, I would do the rest. Thank you very much for your patience. |
#12
|
||||
|
||||
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
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#13
|
|||
|
|||
Oh, wow, I wouldn't know how to thank you for so much help. I'll get down to business with the code. If I have any further questions, I will let you know. Many thanks
|
#14
|
|||
|
|||
Quote:
https://www.gmayor.com/insert_content_control_addin.html? |
#15
|
||||
|
||||
You can simplify your code to make it faster to implement and maintain.
For instance, I would get rid of the Carreteras module and replace the UserForm_Activate macro in the userform's module with this to streamline that part. Code:
Private Sub UserForm_Initialize() Dim sVia As String, arr() As String sVia = "AP-15:AP-68:A-1:A-10:A-12:A-15:A-21:A-68:PA-30:PA-31:PA-32:PA-33:PA-34:N-111:" & _ "N-113:N-121:N-121-A:N-121-B:N-121-C:N-135:N-232:N-240:N-240-A" arr = Split(sVia, ":") Me.cbovia.List = arr End Sub If you are going to enter all that data directly in VBA code, try to do it with as little duplication or complexity as possible. Graham's code could also be trimmed a bit along these lines Code:
Private Sub CommandButton2_Click() 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, "|") Rellenar "termino", arr(0) Rellenar "partido", arr(1) End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
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 |
Deleting only all bookmarks contents not the bookmarks | adilprodigy | Word VBA | 1 | 10-11-2017 01:31 PM |
Form updating Bookmarks - writes to the bookmarks multiple times | PeterPlys | Word VBA | 13 | 01-14-2015 06:41 AM |
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 |