My task: I have multiple forms in one Word document and it has to be filled out with the same information such as company name, address, tax number, etc.
My experience with VBA is very limited so I used bookmarks and wrote some code:
Private Sub OKbutton_Click()
Dim FirmaName As Range
Set FirmaName = ActiveDocument.Bookmarks("FirmaName").Range
FirmaName.Text = Me.TextBox1.Value
Dim FirmaNameRio As Range
Set FirmaNameRio = ActiveDocument.Bookmarks("FirmaNameRio").Range
FirmaNameRio.Text = Me.TextBox1.Value
Me.Repaint
stinfo.Hide
The issue or challenge for me is, that I want to be able to "dynamically" change the data in the pop-up form. Now it works the way every time I press OK-button, it adds new data behind the previous, which is undesirable. If I make a mistake I want to change it only in the pop-up window, not in the document.
So is there any way to program it that the information typed in the pop-up window can rewrite the previous information?
It´s not necessary to use bookmarks or whatsoever. It just has to work, that it´s easy for other employees to fill out these forms and save some time by skipping mechanical copy-pasting.
Use code like:
Sub UpdateBookmark(StrBkMk As String, StrTxt As String)
Dim BkMkRng As Range
With ActiveDocument
If .Bookmarks.Exists(StrBkMk) Then
Set BkMkRng = .Bookmarks(StrBkMk).Range
BkMkRng.Text = StrTxt
.Bookmarks.Add StrBkMk, BkMkRng
End If
End With
Set BkMkRng = Nothing
End Sub
which you would call with code like:
Call UpdateBookmark "FirmaName" Me.TextBox1.Value
And, since you're using bookmarks, instead of writing the same value multiple times to different bookmarks, simply insert a cross-reference to the first bookmark whose value is to be repeated and, when you've updated all the bookmarks, use a single code line to update all the cross-references:
ActiveDocument.Fields.Update
Related
I'm trying to create a userform that will add text to the Word document based on a mixture of inputs coming from the userform. It could be checkboxes, lists, or textboxes where one writes content. As I was looking around for codes to preserve the bookmarks that I use to transfer the inputs into Word text, I came across one that allowed me to successfully preserve the bookmark for future editing. I thought I could use the same method for multiple bookmarks, but it seems not to work. With an example, now.
I have TextBox1, TextBox2 (in the userform), Bookmark1, Bookmark2 (in the Word document), and CommandButton1 to give the ok to transfer info from the userform to the Word document. I want to make TextBox1's content appear where Bookmark1 is, and TextBox2's content where Bookmark2 is. Code I tried is:
Private Sub CommandButton1_Click()
Dim BMRange01 As Range
Set BMRange01 = ActiveDocument.Bookmarks("Bookmark1").Range
BMRange01.Text = Me.TextBox1.Value
ActiveDocument.Bookmarks.Add "Bookmark1", BMRange01
Dim BMRange02 As Range
Set BMRange02 = ActiveDocument.Bookmarks("Bookmark2").Range
BMRange02.Text = Me.TextBox2.Value
ActiveDocument.Bookmarks.Add "Bookmark2", BMRange02
End Sub
When I tried with a single bookmark (from the first "Dim" to the last BMRange01) it worked just fine, and I could edit and re-use the bookmark multiple times. When I try with two of them, however, only one of them is preserved (or rather, deleted and created back again). Specifically, the second one seems to be preserved, while the first one (Bookmark1) is deleted.
Make your life a bit easier and create a separate sub to handle the work. Eg:
Private Sub CommandButton1_Click()
ReplaceBookmarkText ActiveDocument, "Boomark1", me.TextBox1.Value
ReplaceBookmarkText ActiveDocument, "Boomark2", me.TextBox2.Value
End Sub
Sub ReplaceBookMarkText(doc As Document, bmName As String, txt)
Dim rng As Range
Set rng = doc.Bookmarks(bmName).Range
rng.Text = txt
doc.Bookmarks.Add bmName, rng
End Sub
I'm currently trying to program a CATIA macro to search through a specific text:"DD/MM/YYY" on a 2D CATIA drawing sheet and replace that same text with a user inputted text. (Basically to update the text box)
I'm currently new to VBA scripting language and have zero to no experience in doing this. I've researched extensively on this but found no codes close to achieving the problems that I am trying to solve.
Textbox contents to be replaced by user
what I wanted the CATIA macro to do
I'm quite sure that your date text string has a specific name in the title block, so search for that specific text string name and assign another value.
If you have a lot of drawings to do this task, you can do it in batch mode, open one by one drawings in a folder, replace the date, save drawing, close document...no input from designer, just assign the new date value inside your new macro.
This short snippet will search all Texts entities and try to replace with a fixed string:
Sub Catmain()
Dim oDoc As Document
Dim oView as DrawingView
Dim oText As DrawingTexts
Dim txt_to_src As String
Dim txt_to_place As String
Dim n As Integer
n = 0
Set oDoc = CATIA.ActiveDocument
Set oSheets = oDoc.Sheets
Set oViews = oSheets.ActiveSheet.Views
Set oView = oViews.ActiveView
Set oTexts = oView.Texts
txt_to_src = "STACK OVERFLOW."
txt_to_place = "REPLACED"
For Each srcText In oTexts
If srcText.Text = txt_to_src Then
srcText.Text = txt_to_place
n = n + 1
End If
Next
MsgBox n & " text frames have been replaced"
End Sub
This only searches all texts in the active view of the active sheet of the opened document.
Consider to use a more specific check criteria such Instr (check if a string is contained into another string), the equality used is just a representative check.
You'll probably need to cycle all views of a Sheet (i.e. all Items of oViews collection), and all Sheets of a document (i.e. all items of oSheets collection). Then extend to cycle all opened DrawingDocuments if you want.
Remember that an empty document with a title block already has 2 Views (background and Main) so if your drawing has, say, just 1 Front View, the script has to cycle through 3 views.
I'm still a beginner with VBA and I'm learning a ton from stackoverflow and from general googling.
I'm hitting a wall on this very general task : I'm trying to show a text giving a very general explanation of a chart when it is selected / hovered-over.
The way I was thinking of approaching this was to create a tab with all my chart names (which I already have for other tasks) and create a little text for each of them. A cell (the VBA part) would contain the selected chart name that I could use to do a simple vlookup to fetch the explanation.
I tried to look on google how to do this and I'm usually pretty successful with forums and such, but there are sooooo many information on how to name a chart name based on a cell that I can't seem to find information on how to name a cell based on a chart name.
Edit : was cut off while typing by my newborn waking up, my bad completely forgot to come back and add my attempted code !!!
Sub Test_Chart_Name()
Dim T As String
T = ActiveChart.ChartTitle.Text
Range("AM41").Value = T
End Sub
So far it works when I run it, I do believe I should be able to make it run automatically whenever I select a new chart but right now the wrong behavior is that it display the chart title instead of the name I assigned to it (ie it paste 'Pay per month in dollars' instead of 'Monthly_pay'.
Here's how I approached the request. I wrote a macro that looks up the description of the chart, and displays it in a message box. For each chart you want to run it with, right click the chart, click Assign Macro from the pop-up menu, and select the macro. When you click on the chart, the macro runs. You can also run the macro anytime from Developer tab > Macros or the shortcut Alt+F8.
I set up a lookup range on the active sheet (it could be anywhere) with chart names in the first column and descriptions in the second.
Sub PopUpChartDescription()
Dim rTable As Range, rCell As Range
Dim sName As String, sDescription As String, sCaller As String
On Error Resume Next
sCaller = Application.Caller
If Len(sCaller) > 0 Then ' macro called by clicking on a chart
' activate the chart or it is deactivated after the macro runs
ActiveSheet.ChartObjects(sCaller).Activate
DoEvents
DoEvents
End If
On Error GoTo 0
If Not ActiveChart Is Nothing Then ' so ActiveChart is something, eh?
sName = ActiveChart.Parent.Name
Set rTable = ActiveSheet.Range("DisplayTable") ' my lookup range
Set rCell = rTable.Columns(1).Find(What:=sName) ' find the chart name
If Not rCell Is Nothing Then ' so rCell containing chart name was found
sDescription = rCell.Offset(, 1).Text
' show the description
MsgBox sDescription, vbInformation, "Chart Description"
End If
End If
End Sub
You could try the code below:
Sub Test_Chart_Name()
Dim T As String
T = ActiveChart.Parent.Name
Range("AM41").Value = T
End Sub
Hope that helps!
Thank you ahead of time for all the help!
I am decent with VBA in Excel. But learning the differences for Word is close to learning it all from the beginning again! So please, if the solution you provide only works in WORD, please clarify so I can learn the differences.
So my goal has two major parts. I believe I solved for part 1, but can not figure out part 2. It's possible that part 1 will need to change for part 2 to work, but I tried my best over the past few weeks to figure it out and now I am finally reaching out for help.
GOAL:
For the purpose of a "Preview Panel" textbox within a Userform so that users can see a live result building from their control choices on the same userform. Additionally, if user changes any control choice, the live preview will update, maintaining proper order.
Example text seen in Preview Panel after User's first pass through Userform:
Original Choice CB 01; Original Choice CB 02; Original Choice CB 03; etc.
Example text seen in Preview Panel if user updates:
Original Choice CB 01; NEW Choice CB 02; Original Choice CB 03; etc.
SOLVED SO FAR: I have figured out how to pull control values into a WORD Doc Section.Last.Range, and then populate the PreviewPanel Textbox from that WORD Doc section. Here's the code I am using to do so:
Sub UpdateWordDoc_AfterUpdate_EVERYcontrol()
Dim docWordObj As Document
Dim cntrl As MSForms.Control
Dim SrcRng As Word.Range
Dim DestRng As Word.Range
Dim MyRng As Word.Range
Set docWordObj = Application.ActiveDocument
Set SrcRng = docWordObj.Sections.Last.Range
SrcRng.MoveEnd wdCharacter, -1
Set DestRng = docWordObj.Sections.Last.Range
DestRng.Collapse Direction:=wdCollapseEnd
For Each cntrl In userform_MemoTemplate.Controls 'Me.Controls
If TypeName(cntrl) = "ComboBox" Then
With DestRng
.InsertAfter cntrl.Name & ": " & cntrl.Value & "; "
.Collapse Direction:=wdCollapseEnd
.MoveEnd wdWord
End With
End If
Next cntrl
userform_MemoTemplate.txtbx_MemoPreview.Value = SrcRng
End Sub
NEED HELP:
If a user reviews what they have entered, and decides to change combo box input, changes to the input need to be updated at the destination range IN SAME ORDER as fields appear on the userform. The only way I can imagine this, is simply reloading ALL fields via a loop.
Can I trigger such a loop when an event changes without repeating the code in all 20+ fields that will be on the Userform?
Thank you again! Can't wait to learn how to solve this!
Henri
I have quite a large word document (> 400 pages) with lots of cross references to headings. So far, I have always referred to the title of the heading, but now I would like to change that and refer to the page the heading resides on.
I didn't find a solution to this via the GUI (except manual treatment, of course), so I was looking into writing some VBA. Unfortunately, I have only found a way to list all targets that can be cross referenced (via GetCrossReferenceItems), but I need a way to access the actual cross reference field.
Can you help me with that? Is a cross reference field the same as a hyperlink?
Cross-references are fields in a Word document, and can be accessed via the Fields collection (ActiveDocument.Fields). You can loop through them like any other collection and check their types to see if it's one you want to work on. It looks like cross-references to text are type 3 (wdFieldRef) and cross-references to page numbers are type 37 (wdFieldPageRef). Changing fields can be a little tricky; the following should get you started:
Sub ChangeFields()
Dim objDoc As Document
Dim objFld As Field
Dim sFldStr As String
Dim i As Long, lFldStart As Long
Set objDoc = ActiveDocument
' Loop through fields in the ActiveDocument
For Each objFld In objDoc.Fields
' If the field is a cross-ref, do something to it.
If objFld.Type = wdFieldRef Then
'Make sure the code of the field is visible. You could also just toggle this manually before running the macro.
objFld.ShowCodes = True
'I hate using Selection here, but it's probably the most straightforward way to do this. Select the field, find its start, and then move the cursor over so that it sits right before the 'R' in REF.
objFld.Select
Selection.Collapse wdCollapseStart
Selection.MoveStartUntil "R"
'Type 'PAGE' to turn 'REF' into 'PAGEREF'. This turns a text reference into a page number reference.
Selection.TypeText "PAGE"
'Update the field so the change is reflected in the document.
objFld.Update
objFld.ShowCodes = True
End If
Next objFld
End Sub