0

I have a Word Macro VBA script that inserts a docx file in a word document:

Public Sub Macro(path As String)
Selection.InsertFile FileName:=path, Range:="", ConfirmConversions:=False, Link:=True, Attachment:= False

ActiveDocument.Close_ SaveChanges:=wdSaveChanges, _ OriginalFormat:=wdOriginalDocumentFormat

Aplication.Quit SaveChanges:=wdSaveChanges

I would like to create a python script that calls the Macro and pass as an argument the path of the file which will be inserted in the VBA script.

import win32com.client as win32

word = win32.DispatchEx('Word.Application')
word.Visible = True

doc = word.Documents.Open(r'path to file .docx')
   
word.Application.Run("Complete.Macro.Name", "path_to_file_docx")

 

But I am getting the following error:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352567), None)

I read that if the Sub receives an argument the VBA is not a Macro anymore. Does anyone know how to overcome this?

10
  • stackoverflow.com/questions/57065202/… stackoverflow.com/questions/16717635/… Commented Jul 16, 2020 at 16:28
  • Already tried this, but it is not working for a Word Macro. Commented Jul 16, 2020 at 16:37
  • " I am getting an error" is not a useful description of what happens when you run your code. Commented Jul 16, 2020 at 16:47
  • Sorry for that, already fixed Commented Jul 16, 2020 at 16:54
  • Does Complete.Macro.Name include the file name? Commented Jul 16, 2020 at 16:59

1 Answer 1

0

Using

import sys
import pywintypes

try:
    ...
except pywintypes.com_error:
    sys.exit(1)

Seems to solve the problem.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.