I would like to find and replace a certain pattern in word using VBA. More specifically I would like to find every instance of "Table" followed by a whitespace followed by one or more digits followed by another whitespace and replace it with "Table", the same number, a period and a tab. I tried using the Find and Replace function of VBA with the "MatchWildcard" property set to true. Finding the pattern has been no problem, however I can't figure out how to replace the text, so that the number stays the same. My idea was to use the same wildcard in the replacement string, but in this case it just spells out the wildcard as text. Therefore my question is whether there is any way to use the matched value in the replacement string.
toFind = "Table [1-9]# " & vbTab
Dim replacementStr As String
replacementString = "Table [1-9]#." & vbTab
myRange.Find.Execute FindText:=toFind, replaceWith:=replacementStr, _
Replace:=wdReplaceAll, MatchWildcards:=True
Should produce: "Table 1 " -> "Table 1. "
Does produce: "Table 1 " -> "Table [1-9]#. "
For further detail on using wildcards see: https://wordmvp.com/FAQs/General/UsingWildcards.htm
toFind = "(Table [1-9])# " & vbTab
Dim replacementStr As String
replacementStr = "\1." & vbTab
myRange.Find.Execute FindText:=toFind, replaceWith:=replacementStr, _
Replace:=wdReplaceAll, MatchWildcards:=True
I'm currently building a solution using VBA to copy my existing query (API query) and change its parameters depending on the values in columns from the original query.
I'm using the p1 variable that contains a looped column values to be used in API URL
The result should be a multiple sheets with one query each, vba code should loop through a certain column in my first query and then pass this values into each new query.
I encountered a problem. I don't know if its regional formatting issue or not, but i cannot really paste the query into Queries.Add method. I was pretty sure i formatted the "" correctly but:
AS you can see editor flashed this part of code in red (in Notepadd++ with VBA formatting its fine)
Here is my code:
mFormula =
"let" & Chr(13) & "" & Chr(10) & " Source = Json.Document(Web.Contents(""https://rejestr.io/api/v1/krs/"" & p1 & ""/relations"", [Headers=[Authorization=""xxxxxxx""]]))," & Chr(13) & "" & Chr(10) &" #""Converted to Table"" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error), " & Chr(13) & "" & Chr(10) & " #""Expanded Column1"" = Table.ExpandRecordColumn(#""Con" & _
"verted to Table"", ""Column1"", {""address"", ""business_insert_date"", ""ceo"", ""current_relations_count"", ""data_fetched_at"", ""first_entry_date"", ""historical_relations_count"", ""id"", ""is_opp"", ""is_removed"", ""krs"", ""last_entry_date"", ""last_entry_no"", ""last_state_entry_date"", ""last_state_entry_no"", ""legal_form"", ""name"", ""name_short"", ""nip"", ""regon"", ""type"", ""w_likwidacji"", ""w_upadlo" & _
"sci"", ""w_zawieszeniu"", ""relations"", ""birthday"", ""first_name"", ""krs_person_id"", ""last_name"", ""organizations_count"", ""second_names"", ""sex""}, {""Column1.address"", ""Column1.business_insert_date"", ""Column1.ceo"", ""Column1.current_relations_count"", ""Column1.data_fetched_at"", ""Column1.first_entry_date"", ""Column1.historical_relations_count"", ""Column1.id"", ""Column1.is_opp"", ""Column1.is_rem" & _
"oved"", ""Column1.krs"", ""Column1.last_entry_date"", ""Column1.last_entry_no"", ""Column1.last_state_entry_date"", ""Column1.last_state_entry_no"", ""Column1.legal_form"", ""Column1.name"", ""Column1.name_short"", ""Column1.nip"", ""Column1.regon"", ""Column1.type"", ""Column1.w_likwidacji"", ""Column1.w_upadlosci"", ""Column1.w_zawieszeniu"", ""Column1.relations"", ""Column1.birthday"", ""Column1.first_name"", ""Column1.krs_person_id"", ""Column1.last_name"", ""Column1.organizations_count"", ""Column1.second_names"", ""Column1.sex""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Renamed Columns3"""
Also, is it entirely possible to dynamically pass multiple variables from first query to create multiple queries to API service? Maybe someone already did something similar?
Many thanks
Alex
You need a line continuation marker (_) in the mFormula = line:
mFormula = _
When I try to change some styles (e.g. textcolor) in a revisions object I get a Run-Time error '5852' (requested object is not available). I've used ActiveDocument.Revisions(i).Style ... Other properties are available (author, creator, type, ...). Btw: I really want change the style from the revision (not the ordinary text). Tested in Word 2010 & 2016
edit: a bit more detailed:
I try to change the color of the revisions that is displayed. Word sets it automically by default. But you can't choose which color Word assigns to reviewers (See this link)
Unfortunately this is exactly what I want to do. So I tried it with VBA and actually there is the property style available for the revision object, but I get an error 5852 in line with ActiveDocument.Revisions(i).Style
Sub test()
i = 1
While i <= ActiveDocument.Revisions.Count
Debug.Print "Revision " & i & " Author: " & ActiveDocument.Revisions(i).Author 'works
Debug.Print "Revision " & i & " Creator: " & ActiveDocument.Revisions(i).Creator 'works
Debug.Print "Revision " & i & " Date: " & ActiveDocument.Revisions(i).Date 'works
Debug.Print "Revision " & i & " FormatDescription: " & ActiveDocument.Revisions(i).FormatDescription 'works...
Debug.Print "Revision " & i & " Range: " & ActiveDocument.Revisions(i).Range 'work
Debug.Print "Revision " & i & " Style: " & ActiveDocument.Revisions(i).Style 'error 5852
Debug.Print "Revision " & i & " Type: " & ActiveDocument.Revisions(i).Type 'works
i = i + 1
Wend
End Sub
Does anyone know why this error occurs?
As you say, it's not possible to set the color for revisions per author, explicitly. It simply is not possible.
I believe that Revisions.Style is not for changing how a revision looks (color or anything else). I believe it's meant to return Style information, such as the name of the style and the various properties (analog to Range.Style). The error is because Word hasn't recorded such information, probably because it doesn't recognize the related style types...
There are two related(?) Revision.Type enums: wdRevisionStyle and wdRevisionStyleDefinition. In a quick test - formatting using a style, creating a style and changing a style definition - I was not able to return either of these revision types. But according to the Language Reference they mean:
wdRevisionStyle 8 Style changed.
wdRevisionStyleDefinition 13 Style definition changed.
In my tests, the type of revision returned for applying a style is wdRevisionParagraphProperty and changing a style definition is not recorded as a Revision.
For Revision.Style to return information I believe you'd need to test whether the Revision.Type is either of the above and only then would there be a valid object in Revision.Style.
I'm looking at Word 2010.
I am trying to update a record using Subform. When I update the first time it updates properly but when I try to update the same record again I am getting the error:
Run-time error '-2147352567 (80020009)': The value you entered isn't valid for this field
The following is the form.
When I click edit, the information from the selected record is populated into the respective text-boxes. Once I update the information and click update, the record gets successfully updated for the first time.
When I try to update the same record again I get the mentioned error.
Here is the VB script that runs on clicking edit.
Private Sub cmdEdit_Click()
'Check if data exists in the list
If Not (Me.frmschoolsub.Form.Recordset.EOF And Me.frmschoolsub.Form.Recordset.BOF) Then
'get data to text box control
With Me.frmschoolsub.Form.Recordset
Me.Schooltxt = .Fields("School_Name")
Me.Desctxt = .Fields("Description")
Me.Deantxt = .Fields("Dean")
Me.Adeantxt = .Fields("Associate_Dean")
'store id of student in tag
Me.Schooltxt.Tag = .Fields("School_ID")
'change caption of button to update
Me.cmdAdd.Caption = "Update"
Me.cmdEdit.Enabled = False
End With
End If
End Sub
When I click on Debug it highlights the following line.
Me.Schooltxt = .Fields("School_Name")
Can you help me in identifying what is the issue here.
I figured that after the each update, I am losing the position of record. I added the following statement after update and Requery
Me.frmschoolsub.Form.Recordset.MoveFirst
Following is the code snippet.
Else
CurrentDb.Execute "Update School " & _
" SET School_Name ='" & Me.Schooltxt & "'" & _
", Description ='" & Me.Desctxt & "'" & _
", Dean ='" & Me.Deantxt & "'" & _
", Associate_Dean='" & Me.Adeantxt & "'" & _
"where School_ID=" & Me.Schooltxt.Tag
End If
'Clear the Fields
cmdClr_Click
'Refresh the table
frmschoolsub.Form.Requery
Me.frmschoolsub.Form.Recordset.MoveFirst
This fixed the issue.
This error is happening that a form field cannot be referenced to a textbox like you did.
You can do it as below.
With Me.frmschoolsub.Form.Recordset
Me.Schooltxt = Forms![<<if you have main form >>]![frmschoolsub].form![School_Name]
I try to change the title of the task fields Text1 to Text30 in Microsoft Project 2007 using VBA.
Here is what I do manually:
In Gant Chart Task Table click on table header and add a column. In the popup I can select which task property to add, in my case “Text1” and I can enter a title, for example “my text1”.
But I don’t care about the table. I want to give the title to the text field.
I want to export Text1 to Text30 to an XML file and like to export the title of the field as well, so I need to get the title and I like to set it, because even if it isn’t used in a table it shallbe exported.
Here is what I wrote just for testing:
Private Sub setfieldtitletryout()
Dim i As Integer
Dim c As Long
For i = 1 To 30
c = FieldNameToFieldConstant("Text" & i, pjTask)
Debug.Print "Text" & i; " has constant " & c
Debug.Print " Name of Text" & i; " is " & FieldConstantToFieldName(c) ' well what a surprise...
SetFieldTitle(c, ListOfNames(i)) ' Oviously doesn't work, because the function doesn't exist :-(
Debug.Print " Title of Text" & i; " is " & FieldConstantToFieldTitle(c) ' unfortunately doen't exist too
Next
End Sub
Here is what I checked but did not ready help me…
http://msdn.microsoft.com/en-us/library/bb221504(office.12).aspx
http://msdn.microsoft.com/en-us/library/bb221254(office.12).aspx
I’d be glad to get this fixed!
Thanks in advance for helping out!
Cheers
B
Well I did it :-)
Private Sub setfieldtitletryout()
Dim i As Integer
Dim c As Long
For i = 1 To 5
c = FieldNameToFieldConstant("Text" & i, pjTask) ' get constant of custom field by name
Debug.Print i & ". Rename title of Text" & i
Debug.Print " Name of Text" & i; " is '" & FieldConstantToFieldName(c) & "'"
CustomFieldRename FieldID:=c, NewName:="Titel of Text " & i 'Rename/set custom field title
Debug.Print " Title of Text" & i; " is '" & CustomFieldGetName(c) & "'" ' get title of custom field
Next
End Sub
http://msdn.microsoft.com/en-us/library/ms453877(v=office.12).aspx
Help on CustomFieldRename
Help on CustomFieldGetName