Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

vbscript - use vba/vbs to open xlsx with password and save into a new xlsx

Have a use case. There is a password-encrypted xlsx. The password is for reading the workbook only, and I cannot make any edit or remove the password.

I need an xlsx without password so downstream processing can take the info from there. What I'm thinking is to open a new workbook and save the data there, but it always got stuck at the step where it's trying to open the encrypted workbook. Any idea?

Sub OpenXlsx()
set objExcel = Create.Object("Excel.Application")
path = "c:/myfilepath.xlsx"
newpath = "c:/12myfilepath.xlsx"
' next line Not Working. It's able to open the 
' excel, but Excel is still stuck at the screen 
' to enter password, and 'excel is waiting for another 
' application to complete an ole action'
set wb = objExcel.Workbooks.Open(path, Password = "123")
set wb1 = objExcel.Wookbooks.Add()

wb.Sheets(1).cells.copy
wb1.Sheets(1).cells.paste PasteSpecial:=xlPasteValues
wb1.SaveAs(newpath)
wb.Close
wb1.Close
objExcel.Quit

End Sub
question from:https://stackoverflow.com/questions/65641466/use-vba-vbs-to-open-xlsx-with-password-and-save-into-a-new-xlsx

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since the extension is xlsx it's the Microsoft Excel Open XML Format. That is essentially a .zip file. Try this:

  1. Copy the original file to a new folder so you don't tamper the original
  2. Rename the file to a .zip extension
  3. Extract the content with winzip or the like
  4. In the extracted content, open xlworksheetssheet1.xml as a text file with notepad or the like
  5. Search for <sheetProtection
  6. Remove all from <sheetProtection to and including the terminating /> and save.
  7. Save the content as a .zip
  8. Rename to .xlsx
  9. Open the file. Password should be gone.

Excel 2016 file passworded.xlsx renamed as passworded.zip opened in WinZip:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...