Link to home
Start Free TrialLog in
Avatar of street9009
street9009Flag for United States of America

asked on

Office.Interop.Word Document - Detect Macros not working

Consider the following code:

//Open the document in Word
var document = word.Documents.Open(sourceFile.FullName);

  //If the project has VBA macros, do not do save as
  if (!document.HasVBProject)
  {
	//Save the document with the new name
	document.SaveAs2(newFileName, WdSaveFormat.wdFormatXMLDocument,
		CompatibilityMode: WdCompatibilityMode.wdWord2010);
}

Open in new window


I have run across several Word docs that cause this to crash. When I open the document manually and do save as (in the new format), it tells me that it can't save as docx because it contains macros.

Is there another way to do this detection or to determine why this isn't working?
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Does the newFileName have a .docm or .dotm extension as required?
Sorry. Not fluent with C#. I gues that ! means 'not'
Avatar of street9009

ASKER

Hey Graham,

What I'm trying to do with that code is leave files with macros alone (will deal with those separately). So newfilename has docx, not docm. That's when the program crashes.

You're correct, ! means not.
In simple tests in VBA , I haven't been able to make that method give the wrong result.
Do the failing documents actually have any macro code, so that we can say that the test is failing?  Otherwise is it false detection when the document is being saved?
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

perhaps test document.VBProject.VBComponents.Count
Graham - I'm not sure if they have any actual code or not, but when you open the document, it has the security warning that macros have been disabled and gives you the option to enable them. I don't however see any code in the documents themselves.

Crystal - I put that in but I'm getting an assembly error when I compile: 'Microsoft.Vbe.Interop.VBProject' does not contain a definition for 'VBComponents' and no extension method 'VBComponents' accepting a first argument of type 'Microsoft.Vbe.Interop.VBProject' could be found (are you missing a using directive or an assembly reference?)      

I added the reference (15.0 as it requested) but I feel sure I'm missing a "using" statement. Do you know what that should be?
Do you have any examples that are non-confidential (or can be made so) that you can post here?
Sure - give this one a shot. I just opened it and cleared all content, but it still shows the warning for macros and I verified the code still says HasVBProject = false.
Test.doc
you probably have to allow access to the VBA project object model
User generated imageYou can get to this from Options, Trust Center Settings
Sounded logical, but I changed that setting and it still shows document.HasVBProject = false.
SOLUTION
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm getting an assembly error when I compile with that statement: 'Microsoft.Vbe.Interop.VBProject' does not contain a definition for 'VBComponents' and no extension method 'VBComponents' accepting a first argument of type 'Microsoft.Vbe.Interop.VBProject' could be found (are you missing a using directive or an assembly reference?)      

I added the reference (15.0 as it requested) but I feel sure I'm missing a "using" statement. Do you know what that should be?
in VBA when I read the VBA project, I add:
Microsoft Visual Basic for Applications Extensibility 5.3
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you're correct, the best course of action is to just trap the exception and move on. I will be attempting this. Thanks to both of you for your help!
you're welcome ~ happy to help