I'd like to allow my users to enter rich text (with bold, italic & underline, but probably nothing more than that) in an entry box on a VBA userform.
I don't want the user to have to install anything, so I need this to be something that leverages what Office or Windows already provide. The obvious candidate is the Windows built-in Rich Edit control.
I did a quick test and verified that I could create a window of this type from VBA, but what it then lacks is all the UI, etc. I could really use a leg-up as to how I then turn this into something usable.
I'm happy to consider alternative controls, so long as they're guaranteed to require no installation (other than as part of the Excel file containing the rest of my VBA code). It needs to work on Windows XP and up, and Office 2003 and up.
You could just try making a simple UI yourself. Add a command button to the form that will bold the selected text in the rich edit control. Or add a command button that will italicize or copy or paste. Your imagination is your limit.
I was checking Rich Edit Version 1.0 in Excel 2010, and I could access the text in the control.
For anyone searching for this still. As Gary McGill stated, "Microsoft InkEdit Control" is your best option.
Use the link below for a reference on the variables it can use:
https://www.thevbprogrammer.com/Ch10/10-06-RichTextBox.htm
With the Toolbox window open in Excel's Visual Basic editor, select Tools->Additional Controls from the menu. Check the box next to MSREdit Class. This will add the rich text control to your toolbox. From there you can add it to a UserForm.
I tested this control in both Office 2003 on Windows XP and Office 2010 on Windows 7. It looks like you'll still need to provide the UI for allowing the user to toggle bold, italics and underline.
It does accept a paste (via Ctrl+V) of some rich text that I copied from Word, but I couldn't figure out how to make it switch font formating while typing text into the control.
Putting this here just to rule it out...
It seems that the "Microsoft InkEdit Control", which is available from "Additional Controls" on the toolbox, is a superset of the Rich Text Edit control (as the name suggests, it also supports Ink).
It's hard to tell whether this is widely installed - it is on my XP/2003 machine, but not on my 2K/2K machine. (I've seen it said that it's installed with Vista and Win7, but clearly there are earlier versions too).
Anyway, I'm ruling this out because using the control results in a message at runtime to the effect that the ActiveX control is "unsafe" (presumably related to the well-known issue with the Rich Text Control itself).
Related
I just downloaded a color theme on that link (a vssetting file) and would like to know how to install it in VBA.
I have the file but no option to put it into VBA. How do I do that?
You can't use a .vssetting file, that's a Visual Studio thing - the VBE wouldn't know what to do with it.
Its colors are "configurable" through the Tools > Options menu; they're the same rather limited 16-bit colors from 1997... and there's no file you can import that will make a "theme" (a .reg file could act like it, perhaps)
There is a hack, though - you can hex-edit VBE7.DLL and change the available colors to whatever you like. Obviously the risk is that you completely wreck VBA in every single VBA host application at once, but hey shiny colors are worth it eh?
The only thing I'm seeing that's remotely close to "theming" the VBE (and doesn't involve rewriting VBA itself) is what we want to achieve in Rubberduck (an OSS VBIDE add-in project I manage) by version 3.0 (we just kicked off the v2.1.x cycle), when we inject our own AvalonEdit WPF code pane into the VBE, implement our own IntelliSense and take full control of the VBE code panes' appearance and functionality - but there's a lot of work to be done before we can even dream of shipping that wet dream of a feature - so if you know a bit of C# and WPF, by all means join the Rubberduck dev team and help us do it!
I have an issue I'm struggling with, and it bothers me to no end. However, I can't come up with a decent solution, so I'm turning to you folks for advice.
The problem is as follows:
I have a .xlsm file containing a few macros. The macros use Forms, and specifically they also use TreeView objects in them (through mscomctl.ocx). I have gone through the Trust Center, and set every setting to it's unsafest form (allow all ActiveX, allow all VBA, no 'safe mode' for web documents, etc etc...
I can open the file, but when I call the macro relying on forms, I get a warning message labeled 'Microsoft Forms', with the following error message:
This application is about to initialize ActiveX Controls that might be
unsafe. If you trust the source of this document, select Yes and the
control will be initialized using your document settings.
It both surprises me (I said to allow it all in the settings, didn't I?) and annoys me (well, not the first time, but me and my coworkers use it on a daily basis and it gets on my nerves).
Does anyone have a clue whatsoever as to:
1) Why this happens?
2) How I can avoid the message?
I'm open to both solutions on altering the Excel file, or settings I have to alter on local machines (<20 PC's).
Details:
Windows 8.1, running Excel 2013. The macro-code is 'safe', it's built by our company employees (myself included). Code is password-protected so can't be meddled with.
Try the below steps
1) Launch Excel
2) Click "File - Options"
3) Click "Trust Center"
4) Select "ActiveX Settings"
5) Modify "ActiveX Settings for all Office Applications" from the default ("Prompt me..." - see above) to "Disable all controls without notification"
We see same error in an Excel solution with a TreeView form-component on 64-bit Win7 and solution here is to re-register mscomctl.ocx
Bat-file code:
regsvr32 /U "C:\Windows\SysWOW64\MSCOMCTL.OCX"
regsvr32 /S "C:\Windows\SysWOW64\MSCOMCTL.OCX"
Regards
I am about to create a VB.NET Application that is able to automatically format Word documents or change specifical things in it. But the applications stops regularly working because Word (working in Background, not visible) opens popup Dialogs, e.g. if the document is a .odt file, or was downloaded from the internet, or even the "Save File" dialog.
Is there any possibilty to prevent Word from opening any of these dialogs?
I haven't found anything in the MSDN Developer Reference concerning this subject either.
Does anybody have an idea how to do this? Thanks in advance.
I haven't done this with Microsoft Word, but I have done something similar in doing COM automation with Microsoft Project from .NET. Below is the code fragment that turns off all sorts of annoying pop-ups from MS Project:
mApp.DisplayAlerts = False
where mApp is Microsoft.Office.Interop.MSProject.Application
I have a program that I'm writing, and upon pressing the tab key inside a textbox, I would like it to autocomplete that word from a list that may change at any time. I've taken a bit of a peek around, but I can't find anything useful - most things that I'm finding are about disabling intellisense in Visual Studio - not what I'm after. I need something at runtime.
Any ideas?
Is there a way to write VBA Code in Visual Studio. If not is there any other alternatives?
The best you can do is bend the office Visual Basic Editor (VBE) tool to your liking. If you stay in it's native environment you get the full power of error detection, Intellisense, live code running, etc.
My tips...
In the VBE go to Tools > Options > Editor tab.
Turn off 'Auto Syntax Check'. You still get code highlighted errors but no annoying popups.
Go to the Editor Format tab and change the Font to Consolas (Western), Size 11.
For code indenting install the awesome, free, Code Manager. It adds some sick keyboard shortcuts.
Make the Edit toolbar easily accessible for code commenting/uncommenting.
Use Rubberduck to add unit testing, source control, code inspections and refactoring functionality.
With those simple changes you end up with a half way decent, useful, and keyboard friendly environment to write your visually appealing code. :-D
VBA code for Excel can only be written inside Excel using the VBA IDE. VBA projects are stored as part of the Excel file and cannot be loaded into Visual Studio.
However, you can write VSTO (Visual Studio Tools for Office) managed add-ins for Excel using Visual Studio. The following MSDN page covers both developing with VBA and VSTO.
Excel for developers
You could also use the interop features of VBA to consume a (COM) object written in Visual Studio from your VBA code.
I've been looking for an answer to this question myself.
Best I've found myself is the option of exporting a Module ect from Excel with the code you've already written (or blank) and load that up in the Visual Studio Environment.
It doesn't offer much, but the highlighted text and auto indenting is nice and makes it much easier to read compared to the standard VBA environment.
Then once you're done just import it back into Excel.
There is a VSCode extension to do this.
It allows you to write code in VSCode and export it to Excel.
This extension is very useful when you develop in VBA.
Here's the link to download the XVBA extension
Edit :
As Peter Macej said in comment, this solution only works for Visual Studio Code not for Visual Studio
You can certainly add and edit a VBA file (.vb) in your Visual Studio solution, but the intellisense will be worthless/screwed up.
This extension for VScode would probably provide a much better experience: https://marketplace.visualstudio.com/items?itemName=spences10.VBA
If your goal is have your VBA code exposed to source control so you can track changes, then it's still worth it to include in your Visual Studio solution, but just store that VBA code in a plain text file and then use the Excel interop to load it into the appropriate module within the excel workbook, e.g.:
xlWorkbook.VBProject.VBComponents["ThisWorkbook"].CodeModule.AddFromFile(#"C:\PathToYour\VBAcode.txt");
And there are other methods to delete/replace code lines, etc....
You can try xlWings package for python and use it with VS Code https://youtu.be/xoO-Fx0fTpM