Search code examples
objective-cmacosms-wordpasswordsscripting-bridge

OBJ-C : Error in opening MIcrosoft Password Protected Doc


*I need to add password in Microsoft word, pdf and ppt doc. For that i have integrated 'MicrosoftWord.h' file and used SBApplication. And used below code to open the password protected doc. self.wordApp = [SBApplication applicationWithBundleIdentifier:@"com.microsoft.word"]; self.wordApp.delegate = self; [self.wordApp activate];

if ([self.wordApp isRunning]) {
    MicrosoftWordDocument *activeDocument = [self.wordApp activeDocument];

    [activeDocument openFileName:FilePath
              confirmConversions:NO
                        readOnly:NO
                addToRecentFiles:NO
                passwordDocument:@""
                passwordTemplate:@""
                          Revert:NO
                   writePassword:@"1"
           writePasswordTemplate:@""
                   fileConverter:MicrosoftWordE162OpenFormatDocument97];
} 

But it's giving me below error while opening password protected doc. Cannot open Error Domain=NSOSStatusErrorDomain Code=-1708 "errAEEventNotHandled: the AppleEvent was not handled by any handler " UserInfo={ErrorNumber=-1708} Please help me in below two cases. 1. How to open password protected Microsoft word doc using objective-c ? 2. How to create password protected word doc using objective -c ?*

I Have resolved this, Please read answer.

Now, I am stuck at another problem. All I need to create Header file for Microsoft Power Point,Microsoft Excel and Adobe Reader. I have referred below Stack Over Flow Link.

Scripting bridge and generate Microsoft Word header file

I have created PowerPoint Header file using

 $ sdef /Applications/Microsoft\ Office\ 2011/Microsoft\ PowerPoint.app | sdp -fh --basename MicrosoftPowerPoint 

But it is giving me error while building the app.

ERROR: MicrosoftPowerPoint.h:3028:2: Redefinition of enumerator 'MicrosoftPowerPoint4006ShapeRange'

Any one please help me, After creating Header file does it requires to add any script or settings in the app. I need to integrate this module in the same mac app in which i have integrated "MicroSoftWord.h".

Thanks in advance.


Solution

  • I Have encrypted word doc with password. Below is the solution.

    - (IBAction)openWordDoc:(id)sender{
    
    NSOpenPanel *openDlg = [NSOpenPanel openPanel];
    [openDlg setCanChooseFiles:YES];
    [openDlg setCanChooseDirectories:NO];
    [openDlg setAllowsMultipleSelection:NO];
    [openDlg beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
        if (result == 1) {
            NSMutableString *fileUrl = [[NSMutableString alloc] initWithString:[[openDlg URL] description]] ;
            NSRange range = [fileUrl rangeOfString:@"file://"];
            NSString *filePath = [fileUrl stringByReplacingCharactersInRange:range withString:@"file://localhost"];
            [self openWordDocument:filePath];
    
        }
    }];
    
    }
    
    - (void) openWordDocument:(NSString *)FilePath{
    NSLog(@"file Path =%@",FilePath);
    self.wordApp = [SBApplication applicationWithBundleIdentifier:@"com.microsoft.word"];
    self.wordApp.delegate = self;
    [self.wordApp activate];
    
    if ([self.wordApp isRunning]) {
        MicrosoftWordDocument *activeDocument = [self.wordApp activeDocument];
    
    
       [activeDocument openFileName:FilePath
                  confirmConversions:NO
                            readOnly:NO
                    addToRecentFiles:NO
                    passwordDocument:@"1"
                    passwordTemplate:@""
                              Revert:NO
                       writePassword:@""
               writePasswordTemplate:@""
                       fileConverter:MicrosoftWordE162OpenFormatDocument97];
        activeDocument.password= @"1";
    
        [activeDocument protectProtectionType:MicrosoftWordE234NoDocumentProtection noReset:NO password:@"1" useIrm:NO enforceStyleLocks:YES] ;
    
          [activeDocument saveAsFileName:FilePath fileFormat:activeDocument.saveFormat   lockComments:FALSE password:@"1" addToRecentFiles:NO writePassword:@"" readOnlyRecommended:YES embedTruetypeFonts:NO saveNativePictureFormat:NO saveFormsData:NO textEncoding:NO insertLineBreaks:FALSE allowSubstitutions:NO lineEndingType:MicrosoftWordE311LineEndingCrLf HTMLDisplayOnlyOutput:NO maintainCompatibility:NO];
    
        [activeDocument closeSaving:YES savingIn:[NSURL URLWithString:FilePath]];
    
    }
    
    }
    

    Thanks.