street9009
asked on
Office.Interop.Word Document - Detect Macros not working
Consider the following code:
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?
//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);
}
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?
Does the newFileName have a .docm or .dotm extension as required?
Sorry. Not fluent with C#. I gues that ! means 'not'
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.
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?
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?
perhaps test document.VBProject.VBCompo nents.Coun t
ASKER
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.VBP roject' does not contain a definition for 'VBComponents' and no extension method 'VBComponents' accepting a first argument of type 'Microsoft.Vbe.Interop.VBP roject' 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?
Crystal - I put that in but I'm getting an assembly error when I compile: 'Microsoft.Vbe.Interop.VBP
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?
ASKER
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
Test.doc
ASKER
Sounded logical, but I changed that setting and it still shows document.HasVBProject = false.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm getting an assembly error when I compile with that statement: 'Microsoft.Vbe.Interop.VBP roject' does not contain a definition for 'VBComponents' and no extension method 'VBComponents' accepting a first argument of type 'Microsoft.Vbe.Interop.VBP roject' 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?
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
Microsoft Visual Basic for Applications Extensibility 5.3
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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