word-vba

Word vba - Determine which button was pressed?

Hi, I'm wondering if there's a simple way for a Word macro to determine which button was just pressed? I have a document template with several button which should all fire a macro. The thing is, I want to create ONE macro which is called in each button. I don't want tons of macros for each button. Now, this macro, when the button is p...

How can I programmatically update the fields, like FileName, in the pageheader in an MS Word 2007 document?

The following code only updates the fields outside the page headers. ActiveDocument.Fields.Update I also asked the question on MSDN Forums ...

How do I check programmatically if any document properties of a MS Word 2007 document has changed?

For example, I want the Title fields in the body and the page headers of the document to be updated automatically whenever the Title field in the document properties panel is changed. I know how to update the fields, but I want to know the name of the event that will tell me when the document properties have changed. Your help will be a...

With VBA, how can I check if, in Windows Explorer, the file extensions for known file types are hidden?

Without opening Windows Explorer. I want to check for this from Word VBA. Thanks. EDIT: This code works now: Set WshShell = CreateObject("WScript.Shell") If WshShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\HideFileExt") = 0 Then MsgBox Prompt:="In Windows Explorer, set the folder op...

Font of all headings in a Word Document

Hi, I am trying to retrieve the font name and size of all the headings from a word document. Any idea how to get it? ...

Spurious "User-defined type not defined" error in Microsoft Word VBA

I have a Microsoft Word template with some code and some references, that has been working fine for months but has just started throwing up a spurious "User-defined type not defined" error whenever I open it or try to compile it. I know it's a spurious error because I haven't made any significant changes to the code. In fact, I've rolle...

Existing Word Document into temporary one

I have a Word document (template) that I want to programmatically modify and than display to the user. I have been able to open the document and modify it but I realized that I was working on the template, while I would like to create a temporary file and work on it. Have you got any suggestion? I tried: myDocument.Contect.Text = Doc...

Checkbox control not working in Word UserForm (Word 2007)

I'm fairly new at this (VBA programming) so pls forgive me if my coding seems simplistic. I have a Word UserForm (Word 2007) with some checkboxes and 2 command controls - Ok and Cancel. When the form is activated from the macro menu or from an assigned icon the checkboxes don't work. However, the 2 command controls work. What I mean b...

How can I copy and paste a range of tables in Word?

EDIT: If you have an example in VBA, I'll take it. I'm just trying to understand how to use the Range object with the Tables collection to copy and paste multiple tables without looping. Put another way, how can I specify a range of 1..lastTable using the Tables collection? If I can see a working VBA example of this, I'll work on the VBA...

In Word, how can I alter mail merge data programmatically with VBA?

I have a Word document that is used as the source document for a mail merge. I can edit the document, but not the data being used for the merge. I need to transform some of the data in the data source (specifically, I need to take numbers (e.g. 342) and add their value in words (e.g. "three hundred forty-two (342)")). I can write a VB...

how to create Table-of-contents (TOC) via macro

I need to create table - of-contents after mailmerge by macro as word does not support updating TOC field from TC fields. Is it possible to automate creating TOC ? ...

Using VBA for Word, how do I create a range of table cells?

I'm trying to learn how to handle Range objects in Word VBA with regards to MS Word tables. Using the Range object help, it would seem I can create a range of cells as long as the cells are contiguous, however I cannot seem to get the syntax for specifying the Start and End points of the range using cells. For example: Set rngCells = ...

Why doesn't the wdBorderVertical exist when using a range of cells instead of a selection?

FYI, I'm using Perl and Win32::OLE, but the error is a Word VBA one. Using Perl's Win32::OLE module, I'm trying to create a table in Word and format certain elements of it. I created the table (15 x 3) and successfully created a range object pointing to the cells from (2, 1) to (14, 3), i.e. all cells except the top and bottom rows. I ...

A more complex word macro then I'm used to (i.e. I can't record it)...

I am trying to write a word macro that Selects everything that is Heading 2 (or any specific style), copies it and pastes it into an Excel document. Macro recording doesn't work here and I'm stumped as to the syntax for the necessary commands. Is it possible to do? P.S. It would be really great if it could select whatever style the curs...

set NewStyle doesn't work

Using VBA in MS Project 2003 and working with a Word Document I try to create a new style using Function CreateStyleHeadingTask(NameStyle As String) As Style Set CreateStyleHeadingTask = Nothing If Not wdDoc Is Nothing Then With wdDoc Set CreateStyleHeadingTask = .Styles.Add(Name:=NameStyle, Type:=wdSt...

Selecting and tagging specific formatting in word documents

Hi guys, being in need to export a DOC file to TXT and then reimport it without losing bold and underline formatting I'd need a macro to do that. My idea is to select each group of words in bold (could be a sentence or a single word in sentence) and then write say <$> before it and <$$> after it. When back in word the reverse should ha...

VBA Word Macro doesnt recognize whitespaces or wildcards

Hi guys, I have a VBA Word Macro that gets words from .txt list and color highlight them in a word .doc document. The problem is that the script ignores whitespaces and wildcards from the .txt list. I insert the word "wit" but it also changes words like witHIN , witCHE, etc. I tried inserting wildcards in the .txt file, like or usi...

Get me started programming and debugging Microsoft Office automation

I'm using Microsoft Office 2003 and creating a bunch of template documents to standardize some tasks. I asked this on Superuser.com and got no response so I'm thinking it's too program-y and hoping I'll have better luck here. I need to automate a work flow that uses a bunch of Office (mostly Word) templates. What I want is to have "My...

How-to: Run existing Word VBA Macros from C# Ribbon Addin

Background: I have an extensive set of specialized VBA macros used in Word for document formatting purposes. In Word 2003, these macros were activated from a customized toolbar. I have recently transitioned to Word 2007 and would like to be able to run these existing VBA macros from a new Word Ribbon created with VS 2010. I have created ...

WORD 2007 w/VBA

Hi Folks, I have a question regarding a WORD 2007 form used to gather information from users. I had it working in WORD 2003 then pulled it over into WORD 2007 and got it working but couldn't leave well enough alone and decided to write it from scratch in WORD 2007. I have cursed that decision ever since.... What I have is some VBA th...