Macro to insert "// TODO (MyName: current date)" at current caret location? - vba

As we all know by now, Microsoft (in their infinite "wisdom"...) have dropped macro support from Visual Studio 2012.
Unfortunately for me, I make extensive use of simple macros in Visual Studio 2010. These macros are very simple indeed; for example, doing things like inserting a GUID at the current caret location.
I would like to have the same functionality of all of these simple macros in VS2012, but for now I'd settle for one of my most-used ones: My "InsertTodo()" macro. All this does is to insert my initials and the current date at the current caret location, like so:
// TODO(MRW:2012-09-11):
It leaves the caret at the end of the inserted text, ready for me to type my TODO comment.
I have assigned this to the Alt+T key, for easy use.
The macro code in VS2010 is extremely basic (no pun intended ;):
Sub InsertTodo()
DTE.ActiveDocument.Selection.Text = String.Format("// TODO(MRW:{0}): ", DateTime.Now.ToString("yyyy-MM-dd"))
End Sub
My question is simple: How do I replicate this in Visual Studio 2012?

You can write a VS addin as described here:
http://www.codewrecks.com/blog/index.php/2012/08/24/converting-visual-studio-macro-to-visual-studio-plugin/
Or you can use a Powershell/NuGet hybrid as described here:
http://www.wintellect.com/CS/blogs/jrobbins/archive/2012/03/30/using-nuget-powershell-to-replace-missing-macros-in-dev-11.aspx

Related

How to Accept All Changes In Selection?

I am trying to accept all changes in a selection, in Word 2013.
I used the macro recorder and it generated the following code (it can also be found at https://learn.microsoft.com/en-us/office/vba/api/word.revisions.acceptall)
Selection.Range.Revisions.AcceptAll
However, it will not work unless I physically select each letter
But if I use the built in buttons in the 'Changes' group within the 'Review' tab, I don't have to physically select the entire letter. I just need to select any part of the change and it will accept the change.
Why does the macro version not perform like the UI option?
That is odd behavior. The UI must be doing something like the below, but whoever built the macro recorder missed it.
Public Sub AcceptSelection()
Dim rev As Revision
For Each rev In Selection.Range.Revisions
rev.Accept
Next rev
End Sub
That code will accept any revisions in the selection, even partial ones.

How to create a program to convert unit measurements

Using Microsoft access, visual basic.
I'm having a big problem doing this task.
What I have done: Created a table on access where I have put measurements in (from meters):
mile = 10000meters, nautic mile = 1862meters, English mile=1652, kilometers = 1000 meters and all the way down to Millimeters.
What I have created for input:
1 box takes an Integer to be converted and a 1 box specified with an initial unit.
What I have created for Output:
1 box shows the Integer of result with 1 box specified the chosen unit of the output.
Can anyone please, please help me with the codes?
Honestly I'd never really noticed the CONVERT function until today but here's a quick demo of how I'd slap together a "conversion tool" in Excel.
If you want to do the same thing in Access, the premise is the same, but it will be a bit more work since you'll have to design the form from scratch instead of using a worksheet, which is kind of meant for this kind of job.
Using Excel functions in Access
Before you are able to use Excel's CONVERT function in Access, you'll need to reference the Microsoft Excel Object Library.
In Access, open any VBA Module.
GoTools > References
Check the box next to Microsoft Excel 16.0 Object Library. (The version number will vary if you have an older version of Office.)
Then you can call most Excel functions from Access VBA or queries with WorksheetFunction (the same way you would use them in Excel VBA).
For example:
MsgBox WorksheetFunction.Convert(3.7, "m", "ft")
...displays a message box with the number of feet in 3.7 metres.
The calculations will be the easy part; a couple lines of VBA in the On Change or On Exit events will trigger the calculation.
The most time-consuming part will likely be perfecting the placement and formatting of the controls on the form, which is by no means difficult (and there are several tutorials online that can provide the basics if necessary.)
Lastly, keep in mind that there are no doubt a plethora of existing conversion tools available for free download with a little Googling... (I'm confident that you're not the first person who wanted to use MS Office to convert measurements.) 😉
More Information:
Microsoft Docs : WorksheetFunction.Convert Method
Microsoft Docs : List of Worksheet Functions Available to Visual Basic
Office Support : Create a form in Access
QuackIt: Microsoft Access Tutorial
Blueclaw : Access Event Procedures
You can download the demo xlsx used above from JumpShare here.
For both comboboxes, bind them to column 2, faktorTilMeter, and set the ColumnWidths to, say: 2,542cm;0cm.
Then, assign this expression as ControlSource for your output textbox:
=TextboxInput/ComboboxFrom*ComboboxTo

Open XML SDK 2.5 document validation: The 'smtClean' attribute is not declared

Our current work project involves opening a Microsoft PowerPoint file (.pptx format), changing some text and chart values, and then presenting the edited version to the end user.
This works rather well so far, but I'm puzzled by what happens when I try to validate the document afterwards. Using the DocumentFormat.OpenXml.Validation.OpenXmlValidator class, I run the Validate function with the PresentationDocument passed in as the only parameter.
Dim document As PresentationDocument = PresentationDocument.Open(templateFilePath, True)
Dim validator As OpenXmlValidator = New OpenXmlValidator()
Dim errors = validator.Validate(document)
For Each errInfo As ValidationErrorInfo In errors
Debug.Print("Error: """ & errInfo .Description & """")
Debug.Print("XPath: " & errInfo .Path.XPath)
Next
Validate() returns an array filled with instances of ValidationErrorInfo. Just about all of these give the same error description when debugging:
The 'smtClean' attribute is not declared.
The XPath for each error looks like this (numbers vary; there appears to be one error per piece of text):
/p:sldLayout[1]/p:cSld[1]/p:spTree[1]/p:sp[4]/p:txBody[1]/a:p[1]/a:fld[1]/a:rPr[1]
Every TableCell has a Paragraph, with child element Run, and this Run has child elements RunProperties and Text. I modify the Text in my scripts, but I do not touch anything else.
Searching for 'smtClean' gave me an MSDN entry for RunProperties which shows 'smtClean' as one of the possible values to be set, but if I create a new instance of DocumentFormat.OpenXml.Presentation.Drawing.RunProperties the 'smtClean' attribute is not available.
Looking around, I found threads where people mentioned merged documents to be one possible cause, but these errors occur even in an unmodified presentation with only a single slide and table in it. Using the Open XML SDK 2.5 Productivity Tool to Validate the base document, I get the same result.
The errors also occur no matter which format I ask the Validator to test for - both the 2007, 2010 and 2013 version of the PowerPoint format return the same amount of errors.
Finally: The file itself works just fine in PowerPoint, even after being modified. I am curious about why the validator returns so many errors, however.
Thanks in advance for any help.
we process Office Documents and remove this Attribute in all Types (Word, Powerpoint, Excel) without Side-Effects. Eric White has identified this as Bug: smtpClean attribute not supported
It is fixed in the current OpenXml SDK on Branch Office2016: https://github.com/OfficeDev/Open-XML-SDK/tree/Office2016
Regards...
Smart tags were deprecated in Office 2010, and the SDK v2.5 validator doesn't support smart tag elements and therefore marks them as invalid.
Please see this MSDN article for more information.
The current developer of the productivity tool says in this thread that the smtClean validation error was a bug in some situations and has been fixed in v3 of the tool.
v3 (the Office 2016 productivity tool) can be found here, however I'm not sure how compatible it is with older versions of Office.

In VB.net is there an option to force generation of the counter variable in the "Next" statement?

Visual Basic .net automatically inserts a "Next" statement when you enter a "For" statement; e.g., if I type:
For i as integer = 1 to 10
a Next will automatically be inserted, so that the code looks like:
For i as integer = 1 to 10
Next
It is optional to put the counter variable ("i" in the above) in the Next statement, so that it would read:
Next i
I'd really like to make this the default, as it really helps when one has nested For statements. I can't find anything in the Visual Studio settings to do this; maybe it's partly buried somewhere in Intellisense. I thought perhaps someone out there has already figured this out.
You will want to modify the .snippet file associated with the pattern. For the one you are looking for it is, by default:
VisualStudioInstallDirectory\VB\Snippets\1033\common code patterns\conditionals and loops\ForEachNext.snippet
You can also track down the exact location by looking in
Tools > Code Snippets Manager..., select Visual Basic for the language and browse to Code Patterns - If, For Each, Try Catch, Property, etc - the file location will be listed there.

Print and Control arrays in VB2010/2013?

I have homework that constitudes of creating a loop in Visual Basic (The course is about the 2010 version, however, I've also tested with 2013 and 6.0), that prints (Using the print command) "Loop" 10 times, in a for loop. Pretty simple stuff.
However, I can't get the "print" command to work in Visual Basic 2010/2013. Works fine in VB6. Does the command not exist in newer versions?
The relevant chunk of code is literally:
Print "test"
VB 2010/2013 adds brackets around "test" and refuses to run it, saying "test" cannot be cast to integer (Saying print is about printing to files, not forms)
VB6 Exectues it fine.
Also, relevant, do control arrays exist in VB10/VB13? My understanding is they were removed, however the second way to do it (In case you don't want to use print) is using a control array.