VSTO: Specify Word Document During Debugging Development - vba

In Visual Studio VSTO, how can I specify the document to open each time I run the project to debug it?
By default, it always opens a blank document and of course I want to test against features that would already be present in a document.

I tried as Cor_Blimey suggested but it opens only the specified when something changes (haven't figured what yet). In addition breakpoints don't work at least in VS2013 implementing the upper solution.
So what I did is to open the specific debugging/testing file each time the add-in is started up.
It works excellent, breakpoints are functional, no blank workbook is loaded and changes in a sheet a available in the next debugging session.
In order to avoid that the file is opened in the released add-in I put it in #if DEBUG.
More information about that method are here but it has as to be used with precaution as described here.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
#if (DEBUG)
this.Application.Workbooks.Open("C:\\Users\\c563368\\Documents\\Visual Studio 2013\\Projects\\...\\debug.xls");
#endif
}
But there is one disadvantage, as long as you debugging environment (visual studio) is running, Office will always open the add-in build from the debug folder.
You can avoid this by running an the office application as external program, as described here.

You need to use the command line you can set to run on a successful build (there is no way to only get it to run on Debug (as in F11) and you cannot set it on a per Configuration basis. However, there is a good workaround to get it to only do things on the Debug configuration etc.
The basic behaviour is:
Open the solution. Open the project's properties. Go to Build Events. In Post-Build Event Command Line enter in the path to Word (e.g. "C:\Program Files (x86)\Microsoft Office\Office14\Winword") (or if it is in your %Path% then just Winword) and pass in the path to the document you want opened as an argument. This will open Word and the document on every successful build (you can set the trigger to being all builds, whether successful or otherwise etc)
What I prefer to do, however, is simply point it to a batch file, passing in the details about the build event as arguments to the batch file. Then, within the batch file, I run the logic to decide if it should launch Word, open a document etc.
For example, you can point it to
$(ProjectDir)buildScript.bat "$(ConfigurationName)"
Then within the batch file have something like
if %1=="Debug - Excel" "C:\Program Files (x86)\Microsoft Office\Office14\excel.exe" "%~dp0\testbook.xlsx"
This will run a batch file called buildScript in the project directory. The first argument (%1 to access in the batch file) will be the configuration. You can therefore set the batch file to launch Word and pass in the document as the argument if the config is e.g. "Debug" but not if it is "Release", thereby sidestepping the limitation within VS2012 Post-Build Event command line.
Edit:
A list of switches for Word can be found at http://support.microsoft.com/kb/210565
What you need to instruct Word to do will depend on the type of addin you are making:
If it is a standard COM addin then, so long as the DLL is registered and you have set the registry entries (or selected it in the Word addin settings) to open the addin then it should open when Word opens.
If it is an addin document, however, then the procedure is different -> try playing with the commnd switches to instruct Word to open the particular addin document.
I am more familiar with Excel COM addins, so you will have to experiment with the specificities of a Word addin. But the basic principles are to use the post-build event commnd line, coupled with the right switches and arguments to Winword.
Hope that helps.

The simpliest way to achive it is to replace .docx / .xlsx file in solution location

Related

Unable to de-highlight on PowerPoint 2019 through VSTO plugin

Do not have enough reputation to comment on the answer by "Aleksey Malov" to query at un-highlighting text.
Preconditions: PowerPoint 2019 or 365. Presentation must be opened with window.
Original shape with highlight:
I'm opening a new PPT file from the VSTO plugin by saving a copy of original PPT file and then performing the following operations on selected shape object. I'm trying to do this using PowerPoint VSTO plugin with Office 2019. This works for me during debugging through Visual Studio but not through the installed plugin.
if(Application.CommandBars.GetEnabledMso("TextHighlightColorPickerLicensed"))
Application.CommandBars.ExecuteMso ("TextHighlightColorPickerLicensed")
My queries:
What does "Presentation must be opened with window." mean?
Why does it not work in the installed plugin?
If I attach the running PowerPoint process in Visual Studio even with Release config, I get expected result where highlight is removed. Any reasons why?
What does "Presentation must be opened with window." mean?
In the code you are relying on the CommandBars object which may not exist until the window is created. To get the code running correctly a window must be initialized.
Why does it not work in the installed plugin?
Well, you need to add some logging statements to see why YOUR code fails. Only looking at the log files you may find any difference.
If I attach the running PowerPoint process in Visual Studio even with Release config, I get expected result where highlight is removed. Any reasons why?
There can be multiple reasons. One of them is that VS could be run with admin privileges which causes all child processes to be run under the same security context. Files may not be accessible without admin privileges and etc.

How do I open a file with a specific application rather than its default application?

I am trying to open files using a specified executable; just like as if you were to right mouse click on a file then scroll to "Open with"
I tried what kaymaf said and reviewed the docs, but I cannot seem to get this to work.
Dim FI As New FileInfo(GetFileNameFromListViewItem(ListViewCollection.SelectedItems(0)))
Dim GetExif As Process = System.Diagnostics.Process.Start("C:\Users\*username*\Downloads\exiftool.exe", FI.FullName)
This just ends up open the executable and rather than opening the file with the executable.
You would like to open a file with your program using the Windows context menu; and do you want to get an entry in that menu? If that is not correct, the answer can be deleted.
I found this in a German forum, and they refer to this site:
This is the translated text:
One possibility would be that you register your file extension and your program in the system to open this file extension. As soon as the system knows everything, you only need to right-click on the file(s) and in the context menu, in addition to the standard entries, another menu item for opening these files is displayed. If you select this menu item, your program will start automatically if it has not yet started, and you can read out / determine the path to this file or several files in your program and process it accordingly. How it all works is described here: ookii.org/Blog/opening_files_via_idroptarget_in_net
On this page there is also a sample for download (start text files with your own program via an additional entry in the context menu / display paths to the files). It is not a VB, but it should be translatable without any problems. Corresponding information on the page and the comments should be observed.

Debug Design Mode in Visual Studio not breaking

So I am currently unable to open a Form and get the following error:
System.ComponenetModel.Design.ExceptionCollection was thrown.
or:
The designer loader did not provide a root component and has not indicated why
Usually the way to solve this is to open another instance of Visual Studio with the same project, attach the debugger to the other Visual Studio and try to open the form.
However, that does not seem to be working. The debugger does not break when the error is hit, and attempting to 'Break All' at any other point does not show me the source code and just the screen saying:
Your app has entered a break state, but there is no code because all threads were executing external code (typically system or framework code).
Attempting to put break points also results in:
The breakpoint will not currently be hit. No symbols have been loaded for this document
Am I missing a setting? Incorrect Setup?
I have been battling this for half a day and thoroughly search the interwebs with no luck thus far. My designer is also fairly complex and lengthy which means trial and error is an absolute last ditch option.
First attempt to resolve:
you may have tried it already. Right click the Solution in solution explorer, click "clean solution", this deletes all the compiled and temporary files associated with a solution.
Do a rebuild of the solution and try to debug again.
Second attept to resolve:
Start debugging, as soon as you've arrived at a breakpoint or used
Debug > Break All, use Debug > Windows > Modules. You'll see a list of all the assemblies that are loaded into the process. Locate the one you want to get debug info for. Right-click it and select Symbol Load Information. You'll get a dialog that lists all the directories where it looked for the .pdb file for the assembly. Verify that list against the actual .pdb location. Make sure it doesn't find an old one.
In normal projects, the assembly and its .pdb file should always have been copied by the IDE into the same folder as your .exe. The bin\Debug folder of your project. Make sure you remove one from the GAC if you've been playing with it.
third attept to resolve:
Disable the "Just My Code" option in the Debug/General settings.
there are might be other causes to your problem, i picked them from here. you may try other solution to try resolve your issues. Fixing "The breakpoint will not currently be hit. No symbols have been loaded for this document."

Updating distributed Word VBA macros - is it possible, how would you do it?

I have a VBA macro which runs in Word 2003. It is one module and the code attached to a userform, and it goes to about 30 client sites. When I first rolled this out I sent someone round to each site to place a .dot template in the Word Startup folder of each of 30 boxes, which then makes the macro appear as a button on the toolbar for each user.
All the users are internet connected.
The location of the .dot template varies from machine to machine, so "installers" would be out of play I presume?
I am anticipating that the macro made need to change from time to time. Is there any way I could get the user to press a button to get the macro to update itself?
Whether its push button or auto run checking for update, I would presumably need to know how to determine where the macro is running from (path) without a document necessarily having been saved, and how to find path to the Startup folder tools>options>file locations but programatically .
This can be done.
create 2 .dot files, loader.dot (checks for updates) and worker.dot (the main macro you are using and want to be able to update automatically).
the first loader.dot should go into C:\Program Files\Microsoft Office\OFFICE11\STARTUP
This is a trusted location and the macro will be loaded into any Word document. Use an AutoExec() subroutine to ensure it runs whenever a word doc is loaded.
worker.dot can go in any fixed installation location e.g. C:\yourapp\worker.dot
The logic in loader.dot is remove any reference to worker.dot (I had to enable trust of VBA model in macro security settings) check for an update using an http call, if new version available download it overwriting worker.dot, then add reference from file specifying worker.dot
I'm not sure that the user-presses-a-button model is ideal.
I'd look into building a "loader" template. Its job would be to run at startup and look for updates to the template/macro you already have and download/install/whatever such files when they change. Then load whatever's installed locally for use.
You should also think about whether it's a problem if an update is available but someone doesn't use it (perhaps they started Word before the change was uploaded, or they're offline). Also, do you need to know what version people are using? Have your loader report back, via HTTP, email or some other method (Twitter?)
Any such loader needs to either very stable (otherwise you just replace chasing around replacing the target template with chasing around replacing the loader) or able to update itself when necessary.

Where does Outlook store the VBA code files?

I had a VBA project in outlook with a few email macros - but after a PC crash they are all gone and all I see is a fresh 'Project1' when I hit Alt+F11
I'm not a VBA programmer, but had a collection of handy macros for email sorting etc. I would not like to have to code them again. Anyone know where the code files should be on the filesystem so that I might rescue the code?
This page has some really good insight on where Outlook keeps all its stuff. It suggests the following:
All Outlook macros are stored in a single file named VbaProject.otm in the user's %appdata%\Microsoft\Outlook folder, which will be a hidden folder on most systems.
Now, the problem is that if you do not see them now, then you probably won't be able to restore them from that location: there is probably either an "empty" project there or no project at all, but if that folder is being backed up, you might be able to restore it.
Moving forward, you might consider exporting your macros periodically in case this happens again, either through the VBA IDE (right-click and select Export File...) or using one of the tools mentioned in the linked article (like the Office Profile Wizard).
Ok. things to try to fix this...
I assume after the problem occured you tried logging back into the same windows user account, and the same Outlook profile.
Create a new windows login to the machine in question.
Login to this account and open Outlook, this will create a new outlook profile. make note of the profile name (to find this: Control Panel > Mail applet > Show Profiles...)
Now Exit Outlook, and make sure it is not running (check for outlook.exe in task manager).
Open Windows Explorer.
Copy (don't cut) the existing VbaProject.OTM file. (if it has any other name than that, first rename it to VbaProject.OTM, then copy).
Navigate to C:\Documents and Settings\USERNAME\Application Data\Microsoft\Outlook
(or use the environment variable notation %appdata%\Microsoft\Outlook for vista/win7)
Rename the existing VbaProject.OTM to VbaProject.OTM.OLD
Paste the VbaProject.OTM from step 5 into this folder.
Reopen Outlook and test (i.e. Alt + F11).
Good luck with recovery.
If this doesn't work do you remember adding self signing certificate at all? If so have you got a copy of the cert? you can try reinstalling it into the certificate manager (certmgr.msc)
copy/install it to the Certficiates - Current User\Personal\Certificates hive.
I just found this note from Sue Mosher (outlook VBA guru):
"AFAIK, once an .otm file is corrupted, it can't be recovered. That's why I recommend that people who rely on VBA code export their modules or backup the entire file. "
All macros are embedded into an OTM file, under the following location:
C:\Users\(***Your User Name***)\AppData\Roaming\Microsoft\Outlook\VbaProject.OTM
To restore, replace this file with the older one, it should work
Ran into this same problem.
First reviewed the .otm file mentioned by #Anonymous Type and #Dave DuPlantis
Not corrupted... hmm
I checked my Ribbon in Outlook for the Developer tab. It was missing and simply re-adding it to my Ribbon fixed my problem.