I want create macro for replacing. But my problem is how to use regular expression in Visual Basic's macro for Visual Studio?
document.Selection.ReplacePattern("test{[^']+}test", "testAAAAtest")
Doesn't work.
First does the RegEx actually match something? Try it in the Find Dialog first.
Second you will need to tell the Replace Pattern how to match - It's essentially the same as doing a Find/Replace.
Here is something to get you started: (Note the vsFindOptions.vsFindOptionsRegularExpression)
Public Sub ReplaceRegEx()
DTE.UndoContext.Open("RegEx Replace")
Dim textSelection As TextSelection = DTE.ActiveDocument.Selection
textSelection.ReplacePattern("test{[^']+}test", "testAAAAtest", vsFindOptions.vsFindOptionsRegularExpression)
DTE.UndoContext.Close()
End Sub
Related
I'm using VBA on Microsoft Word.
I've create the SAPI.SpVoice object:
Set speech = CreateObject("SAPI.SpVoice")
And I'm using it to read the selected paragraph:
Selection.Next(wdParagraph).Select
speech.Speak Selection.Text, 3
It works great. Now, when "speaking" is finished, I want the program to automatically move to the next paragraph and read it. i.e. to run the above two lines of code, again.
When I did something similar in C# I used the SpeechSynthesizer.SpeakCompleted event to invoke the "Move to the next paragraph and read it" function. I'm looking for the equivalent in VBA.
Any ideas?
Found the solution. Apparently, the solution itself is simple but it didn't work since I had the wrong reference in VBA.
In VBA > Tools > Reference there were two appearances of "Microsoft Speech Object Library".
One was referencing to C:\Windows\System32\Speech_OneCore\common\sapi_onecore.dll
The other was referencing to C:\Windows\System32\Speech\Common\sapi.dll
The 2nd one is the right one.
After fixing this, I've just used the simple integrated event:
Private Sub speech_EndStream(ByVal StreamNumber As Long, ByVal StreamPosition As Variant)
'Do stuff
End Sub
Today I opened a visual basic project which has several lines that declare some strings, which contain comma separated data. I amended one of the strings, rebuilt and closed the project. When I came to test it, there was an error. When I looked at the code, I discovered that the strings on lines after the one I had amended had also changed - they had spaces inserted after the commas.
Has anyone seen normal strings incorrectly having formatting applied by the IDE in visual studio 2015?
It seems like the string is being formatted as if it were code. I would suspect this has something to do with String Interpolation, though these are legacy projects that do not use it.
Edit, FWIW the code is similar to this, I've renamed the fields and headings to try keep it short.
fullColumnMappingsBuilder.Append(",field1,field2,field3,field4,field5,field6,field7,field8,field9,field10")
fullColumnHeadingsBuilder.Append(",heading1,heading2,heading3,heading4,heading5,heading6,heading7,heading8,heading9,heading10")
If clientCode = "ML" Then
fullColumnMappingsBuilder.Append(",mlField")
fullColumnHeadingsBuilder.Append(",mlHeading")
End If
defaultColumnMappingsBuilder.Append(",morefields1,morefields2,morefields3,morefields4")
defaultColumnHeadingsBuilder.Append(",moreHeadings1,moreHeadings2,moreHeadings3,moreHeadings4")
My edit was to simply add in field11 and heading11, and save. But the code ended up looking like this:
fullColumnMappingsBuilder.Append(",field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11")
fullColumnHeadingsBuilder.Append(",heading1,heading2,heading3,heading4,heading5,heading6,heading7,heading8,heading9,heading10,heading11")
If clientCode = "ML" Then
fullColumnMappingsBuilder.Append(", mlField")
fullColumnHeadingsBuilder.Append(", mlHeading")
End If
defaultColumnMappingsBuilder.Append(", morefields1, morefields2, morefields3, morefields4")
defaultColumnHeadingsBuilder.Append(", moreHeadings1, moreHeadings2, moreHeadings3, moreHeadings4")
The problem is that the IDE has inserted spaces after the commas on lines that I did not edit.
Well, I think I know how I did it. If you start with something like this:
Public Class Class1
Sub New()
Dim string1 As String = "Hello there world asdfj asldfkja sad "
Dim string2 As String = "some code like words dim private as object sub"
End Sub
End Class
I think I must have taken the first double quotes character off the declaration of string1, and then moved the position of the text cursor to a different line. In previous versions of visual studio, the IDE would have marked the first line as being in error.
Now we have multi-line strings, it thinks you have a multi-line string running from the quotes at the end of the first line up to string2 As String", and that the following text is code, and so it gets changed (in this case the keywords are capitalized).
Dim string2 As String = "some code Like words Dim Private As Object Sub"
I'll just have to be more careful when editing strings.
Might not have been your fault.
I've seen VS 2015 automatically make changes very similar to the above example:
Dim string2 As String = "some code like words dim private as object sub"
... changing to:
Dim string2 As String = "some code Like words Dim Private As Object Sub"
This was on both multi-line and single-line strings, in procedures I had not touched at all, in a .vb file where I was changing strings in other procedures. Only reason I caught VS doing this was because I did a Compare before check-in.
In Visual Studio formatting Strings as VB.NET code it suggests un-checking Tools > Options > Text Editor > Basic > Advanced > Pretty listing (reformatting) of code, but am reluctant to turn off all the good things that does.
Feels like a bug in VS 2015 to me. Have never seen this in 16 years of using prior versions of VS, before 2015.
I'm having difficultly with the using statement in Visual Basic. Does anyone know how this should be written?:
Using (Dim doc As WordprocessingDocument = WordprocessingDocument.Open(filename, True))
//blah blah
End Using
The code works fine without the using and its obviously as syntactic error. "Dim" is highlighted, and an expression is expected apparently. Sorry if this a bit basic, but the info on vb using statements is not clear and its obviously doesn't work in the c# style.
There's two things wrong.
First, you must remove the Dim keyword. The Using keyword replaces the Dim keyword. Both Dim and Using have the same effect of declaring a new variable, just in different ways.
Secondly, you must remove parentheses. The very first thing after the Using keyword must be the variable name.
Using doc As WordprocessingDocument = WordprocessingDocument.Open(filename, True)
' blah blah
End Using
Is there any way to align text into the center in msgbox in VB or VBA? Does VB have any functionality to do the same?
No. The MsgBox() function is simply a wrapper for the Windows MessageBox() function and as such has no stylistic control over the dialog beyond the icon.
If you want to change it any further than this, you will need to create your own window and show that instead.
On Windows Vista+ you can use TaskDialogs that allow a lot more control.
no, but you can cheat by using spaces.
msgbox(" your message")
VBA
Some notes: http://access.mvps.org/access/bugs/bugs0035.htm AND http://www.tek-tips.com/viewthread.cfm?qid=435428 However, it is not so difficult to build your own message box, which solves all your problems.
When you are building your strings you could pad them at the beginning and end with spaces to achieve a target length. If you're using excel the worksheet function rept is handy for this.
function pad_n_center(byval mystring as string, lenmax as integer) as string
dim pad_by as integer
dim pad as string
pad_by = (lenmax - len(mystring))/2
'some more code to finesse that?
pad = worksheetfunction.rept(" ",pad_by)
pad_n_center = pad & mystring & pad
end function
As mentioned before if the msgbox still doesn't look good you can use textbox shape object (or other objects) to get the desired effect.
I have converted a VB6 project to VB.NET and am left with a large number of inline 'warning messages' such as "UPGRADE_WARNING: Couldn't resolve default property of object varJonSkeet" that I would like to get rid of. Is there a way to do this within Visual Studio 2008? Will it be easier to remove the warning messages with regex? I would prefer to do the removals one file at a time, but it isn't a dealbreaker.
Quick and simple fix if using VS 2008, use Find and Replace :
Open up the find options
Click 'use' and then select 'Wildcards'
In the 'Find what' put "'Upgrade Warning*" and leave the 'Replace with' blank
Click 'Replace All'
The best way to get rid of warnings is to address the suspicious code that the warnings complain about. That is, change the code such that it is no longer warning-worthy. Don't just seek to disable the generation of warnings altogether.
You'll need to provide more details about the specific warnings you're concerned about, and the accompanying code. But remember to search previous answers here first.
I see the warnings are actually text literally in your code, not messages issued in the compiler output. The way to get rid of those is to search for the keyword (UPGRADE_WARNING, I guess), consider whether the issue that it warns about has been addressed or is still a valid concern, fix the problem if there is one, and the delete that warning line. For example, does varJonSkeet have a default property, and if not, does it need one? Should you use to a non-default property instead? (You're not really asking how to delete a line of text, are you?)
If you've already gone through the whole file and determined that none of the warnings are valid, there's a quick way of removing all the warning lines.
grep -v UPGRADE_WARNING input_file.vb > output_file.vb
ren output_file.vb input_file.vb
If you don't already have grep on your system, then you don't have a complete development environment yet. Go get a copy. The -v option tells it to invert the search results, thus printing all lines that don't contain the search pattern. Those get written into the new file. Then replace the old file with the new one.
I believe he is saying that he wants to remove the inline comments from his code.
Fastest way is to perform a find in files for UPGRADE_WARNING: and remove them by hand.
Or,
You could create a new .Net program to iterate through each .vb file in your source directory and read them in using a StreamReader and then write them out 1 line at a time to the same file and as you go omit any lines containing UPGRADE_WARNING:.
If you do the second way you will be that much better for having done some more vb.net coding.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FileName As String = "c:\form1.vb"
Dim SourceFile As System.IO.FileInfo = New FileInfo(FileName)
Dim SourceTextStream As System.IO.TextReader = SourceFile.OpenText()
Dim SourceFileContent() As String = Split(SourceTextStream.ReadToEnd(), vbCrLf)
SourceTextStream.Close()
Dim CurrentSourceLine As String
Dim CurrentSourceLineNumber As Long
Dim DestStream As StreamWriter = New StreamWriter(FileName)
Dim LogStream As StreamWriter = New StreamWriter(FileName + ".log")
For Each CurrentSourceLine In SourceFileContent
CurrentSourceLineNumber += 1
If InStr(CurrentSourceLine, "UPGRADE_WARNING") = 0 Then
DestStream.WriteLine(CurrentSourceLine)
Else
' Write to Log File
LogStream.WriteLine("Line Skipped at number: " + CurrentSourceLineNumber.ToString())
End If
Next
DestStream.Close()
LogStream.Close()
End Sub
Rewrite the project in VB.NET. Getting rid of the warnings might is only a means to get to the goal which (i presume) is a working program.
The VS 2008 method mentioned above does not work with VS 2010 and later.
for VS 2010 and later follow these steps to remove upgrade warnings...
CTRL + F
Expand the "Find..." drop-down and select "Find In Files...".
Select the "Replace in Files" tab.
Find what: [^\S\r\n]'UPGRADE_WARNING:.*\r?\n
Replace with: BLANK
Expand "Find Options".
Check "Use Regular Expressions".
Begin your search/replace/replace all.
Explained: Using regex, the [^\S\r\n] tells regex to ignore the white-space at the beginning of the line, then obviously the 'UPGRADE_WARNING: is the start of the warning comment were looking for, then the .* tells regex to ignore everything else up to the *\r?\n, then the *\r?\n tells regex to also match the line-break at the end (without this part would leave a blank line where each warning would have been).