Documents.Open return value is null

Last month, a problem arise, the solution records about ~

 

[Problem] can not Powershell command for documents related to the operation of Office documents (such as the command to open the document) on (SM69) implementation of the SAP system via an external server OS OS commands.

[Phenomenon] execute OS commands outside of the program is suspended, has yet to respond to (the process can not end).

[Solution] Create folder: C: \ Windows \ SysWOW64 \ config \ systemprofile \ Desktop 

 

PowerShell program features: Password protection from the designated Word document.

PowerShell program code executed as follows:

. 1  the Param (
 2    [String] $ filePath ,            # Word document address (value given by the program SAP) 
. 3    [String] $ NewPassword          # to the Word document password set (assigned a value by the SAP program) 
. 4  )
 . 5  
. 6  $ ErrorActionPreference = " the STOP " 
. 7  $ Word = $ null 
. 8  
. 9  the try {
 10      # start Word application 
11     

12 is      $ Word .Visible = $ to false 
13 is  
14      # open Word document designated 
15      $ DOC = $ Word .documents.Open ( $ filePath )
 16      $ DOC .Activate ()
 . 17  
18 is      # set password 
. 19      $ DOC .Protect (. 3, [REF] $ to true , [REF] $ NewPassword )
 20 is  
21 is      # close the document 
22 is      $ DOC .Close ()
 23 is      the Write-the Host 0
 24  
25  } {the catch
 26 is      $ error [0 ]
 27      the Write-the Host 255
28 
29 }finally{
30     Write-Host "finally"
31     #Start-Sleep 2
32 
33     #关闭Word应用
34     $Word.Quit()
35 
36 #"BulletProofOnCorruption"
37 #(Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Office\16.0\Word\Options\').BulletProofOnCorruption
38 #"BulletProofOnCorruption"
39 
40 
41     [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Word) | Out-Null 
42     [System.GC]::Collect()
43     [System.GC]::WaitForPendingFinalizers()
44     #Remove-Variable $Word
45 }

 

【Solutions】

By inserting $ error [0] >> C: /Document/ps_error.txt; (path can customize, need to ensure the presence of a folder path)

Open C: /Document/ps_error.txt, you can check when the program is executed where the problem is:

1 catch{
2       $error[0] > C:/Document/ps_error.txt;
3       Write-Host 255
4         }

 

I am here to display the error log is $ doc.Activate () return value is empty (null).

Since the $ doc = $ Word.documents.Open ($ filePath), that is to say:

$ Word.documents.Open ($ filePath) returns a null value

The program is suspended on the occasion, since the Explorer can be seen in Microsoft Word application has started, it is possible to determine the value of $ Word is no problem.

 

By inserting additional $ filePath> C: /Document/ps_filePath.txt; (path can customize, need to ensure the presence of a folder path)

Determine the value of $ filePath it does have to be given in.

$filePath > C:/Document/ps_filePath.txt;

 

So the question will be out on documents.Open, the command is not executed properly.

Check a lot of information on this functionality (Microsoft Office applications carried out by non-interactive client application "automation") it does not seem to be supported by Microsoft.

https://support.microsoft.com/zh-cn/help/257757/considerations-for-server-side-automation-of-office

In many similar issues online ( Documents.Open returns A null ),

The most effective solution is to create a virtual desktop folder:

C:\windows\syswow64\config\systemprofile\Desktop

32-bit system, then probably

C:\windows\system32\config\systemprofile\Desktop

 

Although it is not clear what the principle, but the problem successfully resolved, it is also not found any other negative effects.

 

In addition, if SAP ERP function is useful to WWI, then, WWI (SP41 above) will automatically generate the Desktop folder.

 

Estimated non-SAP products to use this type of functionality (automated Microsoft Office applications by non-interactive client application) will encounter the same phenomenon,

Perhaps you can try this approach.

Guess you like

Origin www.cnblogs.com/yifengzhangsz/p/12073934.html