Walkthrough: Call code from VBA in a Visual Basic project. 6 minutes to read. Contributors. In this article This walkthrough demonstrates how to call a method in a document-level customization for Microsoft Office Word from Visual Basic for Applications (VBA) code in the document.
The procedure involves three basic steps: add a method to the ThisDocument host item class, expose the method to VBA code, and then call the method from VBA code in the document. Applies to: The information in this topic applies to document-level projects for Excel and Word. For more information, see. Although this walkthrough uses Word specifically, the concepts demonstrated by the walkthrough also apply to document-level projects for Excel. This walkthrough illustrates the following tasks:. Creating a document that contains VBA code.
Trusting the location of the document by using the Trust Center in Word. Adding a method to the ThisDocument host item class. Exposing the method to VBA code. Calling the method from VBA code. Note Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see.
Prerequisites You need the following components to complete this walkthrough:. An edition of Visual Studio that includes the Microsoft Office developer tools.
For more information, see. Microsoft Word Create a document that contains VBA code The first step is to create a macro-enabled document that contains a simple VBA macro.
The document must contain a VBA project before you create a Visual Studio project that is based on that document. Otherwise, Visual Studio cannot modify the VBA project to enable VBA code to call into the customization assembly. If you already have a document that contains VBA code that you want to use, you can skip this step. To create a document that contains VBA code. Start Word. Save the active document as a Word Macro-Enabled Document (.docm) with the name DocumentWithVBA.
Save it in a convenient location, such as the desktop. On the Ribbon, click the Developer tab. Note If the Developer tab is not visible, you must first show it. For more information, see. In the Code group, click Visual Basic. The Visual Basic Editor opens.
In the Project window, double-click ThisDocument. The code file for the ThisDocument object opens. Add the following VBA code to the code file. This code defines a simple function that does nothing. The only purpose of this function is to ensure that a VBA project exists in the document. This is required for later steps in this walkthrough.
Sub EmptySub End Sub. Save the document and exit Word. Create the project Now you can create a document-level project for Word that uses the macro-enabled document you created earlier.
To create a new project. Start Visual Studio. On the File menu, point to New, and then click Project. If your IDE is set to use Visual Basic development settings, on the File menu, click New Project. In the templates pane, expand Visual Basic, and then expand Office/SharePoint. Select the Office Add-ins node.
In the list of project templates, select the Word 2010 Document or Word 2013 Document project. In the Name box, type CallingCodeFromVBA. The Visual Studio Tools for Office Project Wizard opens. Select Copy an existing document, and, in the Full path of the existing document box, specify the location of the DocumentWithVBA document that you created earlier. If you are using your own macro-enabled document, specify the location of this document instead. Click Finish.
Visual Studio opens the DocumentWithVBA document in the designer and adds the CallingCodeFromVBA project to Solution Explorer. Trust the location of the document Before you can expose code in your solution to VBA code in the document, you must trust VBA in the document to run. There are several ways to do this. For this walkthrough, trust the location of the document in the Trust Center in Word.
To trust the location of the document. Start Word. Click the File tab. Click the Word Options button. In the categories pane, click Trust Center. In the details pane, click Trust Center Settings.
In the categories pane, click Trusted Locations. In the details pane, click Add new location.
In the Microsoft Office Trusted Location dialog box, browse to the folder that contains the CallingCodeFromVBA project. Select Subfolders of this location are also trusted. In the Microsoft Office Trusted Location dialog box, click OK.
In the Trust Center dialog box, click OK. In the Word Options dialog box, click OK.
Add a method to the ThisDocument class Now that the VBA project is set up, add a method to the ThisDocument host item class that you can call from VBA code. To add a method to the ThisDocument class. In Solution Explorer, right-click ThisDocument.vb, and then click View Code.
The ThisDocument.vb file opens in the Code Editor. Add the following method to the ThisDocument class.
This method creates a table with two rows and two columns at the beginning of the document. The parameters specify the text that is displayed in the first row. Later in this walkthrough, you will call this method from VBA code in the document. Public Sub CreateTable(ByVal firstColumnHeader As StringByVal secondColumnHeader As String) Me.Paragraphs(1).Range.InsertParagraphBefore Dim table1 As Word.Table = Me.Tables.Add(Me.Paragraphs(1).Range, 2, 2) With table1.Style = 'Table Professional'.Cell(1, 1).Range.Text = firstColumnHeader.Cell(1, 2).Range.Text = secondColumnHeader End With End Sub. Build the project.
Expose the method to VBA code To expose the CreateTable method to VBA code in the document, set the EnableVbaCallers property for the ThisDocument host item to True. To expose the method to VBA code. In Solution Explorer, double-click ThisDocument.vb. The DocumentWithVBA file opens in the designer. In the Properties window, select the EnableVbaCallers property, and change the value to True. Click OK in the message that is displayed. Build the project.
Call the method from VBA code You can now call the CreateTable method from VBA code in the document. Note In this walkthrough, you will add VBA code to the document while debugging the project. The VBA code you add to this document will be overwritten the next time that you build the project, because Visual Studio replaces the document in the build output folder with a copy of the document from the main project folder. If you want to save the VBA code, you can copy it into the document in the project folder. For more information, see. To call the method from VBA code.
Press F5 to run your project. On the Developer tab, in the Code group, click Visual Basic.
The Visual Basic Editor opens. On the Insert menu, click Module. Add the following code to the new module. This code calls the CreateTable method in the customization assembly. The macro accesses this method by using the CallVSTOAssembly property of the ThisDocument object. This property was automatically generated when you set the EnableVbaCallers property earlier in this walkthrough.
Sub CreateTable Call ThisDocument.CallVSTOAssembly.CreateTable('Employee Name', 'Start Date') End Sub. Press F5. Verify that a new table was added to the document. Exit Word without saving your changes. Next steps You can learn more about calling code in Office solutions from VBA in these topics:.
Call code in a Visual C# customization from VBA. This process is different from the Visual Basic process.
For more information, see. Call code in a VSTO Add-in from VBA. For more information, see. See also Feedback.
Convert PDF to Word on Mac with PDFelement Step 1: Change PDF to Word on Mac Drag and drop your PDF file into PDFelement's main window to open the file. Once the file has been opened in PDFelement, click on the 'Convert' button to enable the 'Convert' function.
You can then click the 'To Word' button to choose Microsoft Word as the output format. You can also select the range of pages that you would like to convert by clicking on the 'All pages' button in the 'Options' tab. Once you are happy with all the changes, click 'Convert' and your document will be converted within a few seconds. Step 2: PDF to Word Conversion with OCR in macOS (Optional) To convert a scanned PDF file, click on 'Convert' and select 'To Word' in the menu bar.
Ensure that you enable the OCR slider on the right menu, and then click on the 'Options' tab if you want to customize the conversion. Go back to the 'Convert' tab and select 'Convert' to start the process. Change PDF to Word on Mac with PDF Converter Pro Another way to convert PDF to Word on Mac is to use. This program is effective because it comes with many features that allow you to create PDFs, convert PDFs to different formats including Word, HTML, Excel and others, and perform OCR. But perhaps the biggest advantage of the PDF Converter Pro for Mac is that it can be used to convert batch files very easily and also convert protected files. Here's how to use PDF Converter Pro for Mac to change PDF to Word on Mac. Import PDF Files Download and install the program on your computer.
Once this is complete, launch the program and click on the '+' button at the top of the main window to import the PDF file you want to convert. Step 2: Select the Conversion Options Click on the 'Format' icon to select the output format (in this case you'll want to select a Word Document). You can also choose the 'Page Range' button to select the pages you would like to convert. Step 3: Convert PDF to Word on Mac When finished, click 'Convert' to begin the process. You can choose to cancel the process at any time.
In a few seconds, the PDF will be converted into Word. Preview Can't Convert a PDF to Word on Mac If you are more interested in using a free PDF editor, you can try using. However, despite the fact that it is an inbuilt Mac PDF editor, it tends to fall short when it comes to functionality and features. Below are a few of its best features.
It allows users to edit, annotate, add images, and highlight sections of the text in a PDF file, but is a bit limited in terms of functionality. It enables users to combine files, and it is free to use.
However, it can't convert PDF to Word in macOS, and it only supports converting PDF into JPEG, PNG, and TIFF. Pros. It's a built-in app on Mac devices. It enables users to combine files. It's free to use. It provides basic editing features.
Cons. It has limited functionalities. It lacks OCR capabilities. It is difficult to use for document conversion. PDF Expert Can't Convert PDF to Word on Mac This is yet another excellent PDF app for Mac. Despite the fact that it comes with fewer features than PDFelement, it's affordable and easy to use.
Its best features include software speed, professional annotation tools, document signing tools, an impressive sharing feature, and form filling capabilities. It is also compatible with most Apple devices (Macs, iPads, and iPhones).
Mac os x 10.11 download. Outstanding MichelPM!!!
However, it does not support converting a PDF document to Word format on Mac. Learn more details about. Pros. It can be used on a number of Apple devices. It has split-screen capabilities. It also offers easy file transfer options. Cons.
It is limited in features and functionalities compared to PDFelement. It has limited OCR capabilities. It has limited image editing tools. It is not user-friendly for document conversion. Convert PDF to Word on Mac for Free with Google Docs Another option is the widely used program, Google Docs. It can also help you export PDF to Word on Mac. Here are the steps to follow:.
1. Login to your Google account on the Google Docs website.
Upload the PDF document that you want to convert to Word. Go to the 'File' tab and choose 'Download as' 'Microsoft Word(.docx)' to export this PDF file to a word file on your MacBook. How to Export PDF to Word on Mac with Automator There is also another free method available for Mac users to convert PDF to word.
This is the built-in Mac app Automator. It can help you convert a PDF file to plain or rich text format. Search for 'Automator' in your Application and open it. Search for 'Extract PDF Text', then select 'Rich Text' (RTF) or 'Plain Text' (TXT) on the right side to save your PDF. You can also set other options as needed. Click the 'Run' button, and choose a PDF file to convert to a text document directly.
Why to Choose the Best PDF to Word Converter on Mac PDFelement for Mac is regarded as the best and most recommended option for PDF to Word conversion on Mac. That's because this application has been intricately crafted to help you solve all of your PDF file editing needs with ease, convenience, and flexibility. Benefits of PDFelement for Mac include. Superior OCR capabilities that simplify the process of transferring any images or scanned PDF files into easily editable and searchable text formats.
Numerous word document editing features like spell check, line mode, and paragraph mode. The ability to create and convert PDF files into any other document format like Word. Increased data security features in the form of permissions, passwords, digital signatures, text redaction, and so on.
Makes it easy to leave comments, notes, and highlight text on PDF files. It has a charming and user-friendly interface. It supports Mac OS X 10.10, 10.11, 10.12,10.13, and 10.14 Mojave.