FAQ Database Discussion Community


Using VBA to manage multiple styles definitions in the same Word document

vba,ms-word,word-vba,office-2013
TL/DR: I have a game plan on how to do this below; however, I am wondering if my plan is going to prove to be too complicated, and what additional considerations I need to take into account before diving into building this project. Although I am not an experienced programmer,...

Securing database passwords in a VBA application from users and QAs [duplicate]

xml,vba,security,word-vba
This question already has an answer here: How to securely store Connection String details in VBA 2 answers We have a Word VBA application that is installed in over a 1000 computers and users use it on a daily basis. This VBA addin connects to multiple databases whose credentials...

Deleting CommandButtons by name in VBA Word 2010

vba,ms-word,word-vba
In a word template, I have two command buttons namely [Add section] which is named "CommandButton1" [Done] - which is named "CommandButton2" I need both buttons to be deleted at the click of [Done] Here's my code so far Private Sub CommandButton2_Click() Dim i As Integer For i = ThisDocument.InlineShapes.Count...

VBA, how to paste word table as picture (enhanced metafile) to a power point?

excel,vba,excel-vba,word-vba,powerpoint-vba
I have an excel workbook that acts as a dashboard and run code to open multiple word files with one table, copies the table and then pastes it to a specific slide in a power point. I am trying to figure out how to copy table from word and paste...

saving/recalling VBA variables in Word 2013

vba,ms-word,word-vba
I'm using VBA in Word 2013 to score and return some basic info that the end user will input in the document in some legacy form fields. My code, which works fine if I have it all in a single sub, is: Option Explicit Sub ScoreLevel() Dim a As Integer...

Changing styles in Word using vba

styles,word-vba
I am very new to programming, so forgive my ignorance. I am trying to create specific headings in a document that does not have any or has different heading styles assigned. What precedes the text in the headings are numbers. The numbers are specific and essentially represent the content of...

embed object and display as icon in word document?

vba,ms-word,word-vba
I have a word document. In the word document I have command button. What I am after is when the user presses the command button it would open a dialog box so that the user can embed a file and display as an icon within the word document. How WOULD...

Why won't my macro run automatically in Word?

word-vba
This is my first time attempting VBA in Word although I've used it quite a bit in Excel. What I am trying to do is automatically execute my code after opening my Word template document. Here is the code I've placed in my Word template Module: Private Sub AutoOpen() Dim...

Word Vba Userform textbox onChange

vba,ms-word,word-vba
I have a userform have one textbox field and with ok and cancel button for myRun macro: Sub myUserForm() UserForm1.TextBox1.Value = Selection.Font.Size UserForm1.Show End Sub And for my Form marco: Private Sub TextBox1_Change() Dim myValue As String myValue = UserForm1.TextBox1.Value If Not IsNumeric(myValue) Then MsgBox "Please enter a number" MsgBox...

Grab a character using LEFT while ignoring specific characters

regex,vba,word-vba
Ok, so I understand you can grab any character using the LEFT function: Left(Right(A,3),1) (big thanks to Serenity for showing me this) What I'm trying to figure out is, if you have variables set like this: myTxt1 = "2513,82 alpha" myTxt2 = "999,71 somekindofexpression" myTxt3 = "55,7 orange" ... how...

VBA in Word code to delete or change code from ThisDocument

vba,ms-word,word-vba
I have some code in a Microsoft Word .docm file that disables the save function and pops up a message: Sub FileSave() MsgBox "Save is Disabled." & vbNewLine & "" & vbNewLine & "To save changes, use Save As." End Sub This might be tricky but what I want to...

Use of Global objects in vba Office macros

vba,ms-word,global-variables,word-vba
Please explain the following: What is the difference between Word.Application and Word.Global? In a VBA Word macro, are references to Global objects like Application and Selection always bound to Word or could they inadvertently refer to Excel objects if Excel is running in the foreground? If I want to open...

How to fill the rest of line with space() / string()?

excel,vba,ms-word,word-vba
I am generating a word document filled with user inputed values (userform input). Is there any way to fill the space after typed value with amount of whitespace required to fill the rest of the line? Or how to get the amount of characters left to the end of line?...

Using MS Word VBA how to Find and Replace highlighted text with the value of the highlight color

vba,ms-word,office-interop,word-vba
Suppose the following text in an MS Word 2007/2010 document, I have "testA" highlighted with blue and "testB" highlighted with green color: "This is testA and testB.". I want to programmatically replace testA with its background color index 2, and replace testB with its background color index 11. Ref: WdColorIndex...

PasteSpecial - Error 5342 - Specified data type is unavailable (Word 2010 code)

ms-word,word-vba,word-2010
I wrote a simple code in Word 2010 VBA (I am new in VBA), which simply takes some tables and one graph from Excel and paste them into Word as OLEobjects. Everythink works fine, except when the code tries to paste the chart from Excel into Word. I got the...

macro in excel for replacing exact text in word document

vba,excel-vba,ms-word,word-vba
I am trying to replace 100 test ID in my word document. I had found the below mentioned macro from this site and used. The problem I am facing is, this logic is not replacing the exact matching value. When I try to change Test ID AUTO_1 to AUTO_A, it...

How do I change a file format when saving a Microsoft Word Doc programattically?

vba,ms-word,word-vba
Should be a pretty easy fix. I'm currently modifying a Microsoft Word doc to prompt the user to do something when they save. After they select an answer, I want to save the document as a different file type, converting it specifically from a .docm to a .docx file. Sub...

VBA Macro to convert OLE to text results in “Word has encountered a prob.” error

vba,popup,word-vba
I am using the following VBA script to parse through a word document and open every OLE table object, copy the table, and paste back into word so that the data in the tables can be searchable. However, after the script runs for a while, Word gives me an error...

Need to capture current scroll position (not cursor position) in MS Word

vba,ms-word,word-vba
I need to get the current scroll position (I think that is the right term; i.e., where is my virtual lens pointed at the document, not where my cursor is) in a Word document, so that when I run a macro I can return to that position at the end....

Loop trought Content Controls Checkbox inside different tables

vba,ms-word,word-vba
I have document with multiple tables in it(as seen in the attached image), so I try to verify only the field with the status in the column "complete", those that are not selected (False)to work in it and continue reviewing one by one. So far my main problem is to...

Excel to word Align picture

excel,vba,excel-vba,ms-word,word-vba
I have a report that is built from Excel and outputs in Word, I also have a picture called "Picture 7". My question is once this is pasted into word from excel is there anyway to center align the picture? the picture is copied over as part of a range...

check in document from sharepoint after edit

vba,sharepoint,word-vba
In my word vba I open a file from sharepoint with this code Set oWord = CreateObject("Word.Application") Set oDoc = oWord.Documents.Open(path) Then I start editing the file. After all I save it back to sharepoint with this code oDoc.Save oDoc.Close oWord.Quit The problem now is that the document is still...

Word 2013 Mail Merge VBA “Argument not optional”

vba,ms-word,word-vba,mailmerge,word-2013
So I created a macro to allow my users to open a document select a .csv and it automatically merges the letters and opens a new document. I can open VBA and run this macro okay however, if I try adding this to the QAT as a button it gives...

Microsoft Word 2010 VBA: Using regex to find and replace

regex,replace,ms-word,word-vba,pci
I'm trying to allow our staff to quickly mask the middle 8 digits of credit card data in old documents. I figure a macro using regex to do a find and replace is the fastest way for them. Masking is a PCI DSS requirement, and there may be hundreds or...

VBA Performance: paralel or sequential analysis

performance,vba,ms-word,word-vba
I am creating a tool to help analyse revised documents. There are many checks that are performed on hundreds of files, about 10k characters in average. I have a class for each analysis, and those checks are mostly done on a sentence by sentence basis. Now, I am wondering if...

Open File dialog box to get Excel

excel,vba,ms-word,word-vba
I've written some Word VBA which takes an Excel file and updates Labels (ActiveX Control) in the Word file. The only thing is this Excel file will change path and filename each month. Instead of editing 2 variables each month, how do I add an Open File dialog box so...

How to construct a new Word Doc in VBA line by line without screen flashes

vba,ms-word,word-vba
The following code works to build a new Word doc line by line, but the screen flashes each time a line is inserted. I am basically generating a new document mostly comprised of a sorted list of snippets of text that I extract from an active document. I have a...

Word - Is it possible to change easily all links' source in a document?

vba,excel-vba,ms-word,ms-office,word-vba
I have 5 Word template linked to an Excel file to automatically generate new pre-formated reports. As I am working on improving it, I have created copies that will replace the old version once I'm done. And in each template, I have approximately 70 links, so almost 350 links in...

VBA-Word Extend Revision to the whole word

vba,word-vba
I'm analyzing revised documents. They all have change tracking enabled, and contain many revisions. These documents are required to have revision on the whole word, not part of it. As this revisions are used for QA, I cannot as well flag as revised any words that were not revised. So...

Macro Removal When Submitting Documents

vba,ms-word,word-vba
I edit and format large proposals and recently started working with macros to speed up the process. However, occasionally my proposal submissions have to be electronic. I am worried the file may be rejected if there are macros attached. If I copy and paste all the text into a new...

Class Object update using a form

forms,vba,ms-word,word-vba
I am creating a tool in VBA to create labels with information about transactions. I would like the user to use a form to give all the information and the labels will be automatically created. To do so I have created a transaction class called clsTransaction. Public PartNum As String...

Using .Find in Word Causes Infinite Loop

excel,vba,excel-vba,ms-word,word-vba
I created a Loop to find each iteration of some HTML code and return the e-mail data as a string. What we are looking for is: 'Jibberish HTML Code <p><b><font color="#000066" size="3" face="Arial">Delivery has failed to these recipients or groups:</font></b></p> <font color="#000000" size="2" face="Tahoma"><p><a href="mailto:[email protected]">[email protected]</a><br> 'Jibberish HTML Code <p><b><font color="#000066"...

MS Word Macro to replace all hyperlinks with a single word

vba,hyperlink,ms-word,word-vba
I prepare a MS Word file with a large amount of news articles, each followed by a full, long clickable hyperlink (e.g. http://www.newssite.com/this-is-the-name-of-the-article.html). I need to replace all the hyperlinks in the file with a single word: "Link", while keeping their url adresses and click functionality. I can use MS...

import folder of .txt files into word document

vba,ms-word,automation,word-vba
I've found a lot on importing folder of .txt files into excel, but not many on importing .txt files into word. I'm trying to get my macro to open all .txt files in a specific folder and import them into a single word document, with each .txt file having its...

Clear All styles except Headings with macro

word-vba
I want to write a macro to remove all style except headers from a document. I have this macro that remove all styles: Sub clearstyle() ActiveDocument.Select Selection.ClearFormatting end Sub How can I improve this to keep only headers style?...

Get access to MS Word macros from PHP

php,vba,ms-word,word-vba
The problem is following. I have a tool that generates .docm files. These files contain some macros. The code of macros has settings information, like URLs, etc. I want to somehow access from PHP this macros in order to make changes programmatically. This is especially necessary when the whole application...

Reading a TXT File with VBA suddenly doesn't work anymore

vba,ms-word,word-vba
I am in the process of converting some old Macros from Word 2003 to Word 2013. I thought I would only have to build a fancy Ribbon Bar and be done with it, but apparently not. The following code is supposed to read the whole content of a TXT File...

VBA for each loop locked when I revert revisions

vba,foreach,word-vba,revision-history
I want to get both the revised and original text from a document. I do it this way: Set wrdDoc = wrdApp.Documents.Open(fileName) For each sent in wrdDoc.Sentences if sent.Revisions.Count >=0 then after=sent.text sent.Revisions.RejectAll before=sent.text SaveRev(before,after) End if next Now that would be fine, except that malformed sentences like This is...

How to identify a particular text box?

textbox,ms-word,word-vba,word-2013
My VBA routine is deigned to alter text in a text box in the document's footer. Right now it is identified as follows: Dim R1 as Word.Range Set R1 = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range R1.ShapeRange(1).TextFrame.TextRange.Text = "xxxx" However, users may modify this template and possibly add other text boxes. How do I guarantee...

On Error works in first and doesn't work in second instance. Bug?

vba,word-vba
I have a very strange problem here. Here's the code: reqLang = "ENG" Select Case reqLang Case "CRO", "ENG" 'first loop ------------------------------------- On Error GoTo reqLangVisible i = 1 'Loop until ccCROENG's are all hidden and then go to reqLangVisible. Do ActiveDocument.SelectContentControlsByTag("ccCROENG")(i) _ .Range.Font.Hidden = True 'hides all CCs i...

Change insert line color with VBA macro

vba,word-vba,word-2010
Code below was created (not by me, and saved as *.dotm) in Microsoft Word 97-2003, when default "insert shape/line" was black. Used as procedure template with specific cover page, header, outline style, etc. When the *.doc files are saved to *.docx, and the "SignoffLine" macro is activated, the inserted line's...

Ambiguous procedure error MS Word 2010 VBA

forms,validation,ms-word,word-vba
I have several plain text content controls in a Microsoft Word 2010 document. Through VBA code, I am using the Document_ContentControlOnExit event to validate the information upon exiting each text box content control. The code for the first text content control works fine. However, when I use the same code...

Public folders in vba

vba,word-vba,outlook-vba
I am struggling to find out how i can create public folders from a word macro, for time being i am debugging right in outlook. The problem is that my macro will be run by several user and hence i can not hardcode in "public folders [email protected]" So is there...

Manipulating numbers obtained from a VBA search

vba,ms-word,word-vba
I'm trying to craft a script in Word that will allow me to programmatically update times stored in a word document as minutes and seconds. The times are written like this, [01:10] and I would like to find each number, and increment it by a specific, consistent number of minutes...

How to add a group to a (VBA) custom ribbon without xmlns/idQ?

vba,excel-vba,word-vba,ribbon,powerpoint-vba
Situation: Users have an existing custom ribbon that is added via a .PPAM (combination of VBA macros and Ribbon customization XML). We want to add a group to that custom ribbon via our managed COM add-in For custom ribbons from other managed COM add-ins this is feasible via the xmlns:foo="bar"...

Count loop and report current iteration

vba,ms-word,word-vba
So the Word macro I'm making pops up a userform where you enter a quantity x (1 of x) and it duplicates a row based on the quantity entered. The copy paste works fine, but I need to report the number of the copy based on the quantity. So for...

Get number of row records

vba,ms-word,word-vba
I would like to append data using word as front-end userform and export the data to specific csv file when submit button is clicked. While firstly I need to know which row I should append the data. Here is the following code and I got error message for nrow =...

VBA enumeration to avoid copy/paste

vba,word-vba
I'm not sure how to name this, so feel free to update if you can make it more descriptive. Here's what I'm trying to do. I have a very repetitive code that has a very predictive pattern for variables that would make each instance different. 'Make sure decimal character is...

.copychart from Excel to Word errors unless both applications are visible

vba,excel-vba,excel-2010,word-vba,word-2010
Using .copychart in pasting from Excel to Word errors in my code below. If both applications are visible, as I've had them while programming, then it works well. Is there a way to hide the applications (let them run in the background) and still use .copychart or do I need...

Saving as PDF in word using VBA causes changes to the document

vba,pdf,ms-word,word-vba
I use the following code to quickly convert an active word document to PDF and save to desktop. It works very well but when I go to exit the document immediately afterwards it always asks if I want to make changes, when none should have been made. Is there a...

Macro cannot acces files on the network

vba,ms-word,word-vba
I am attempting to edit some Word files in a network path, put the Macro doesn't start. If i change the path to a local path (C: or D:), it works fine. I have full access to the network path. Directory = "S:\PROJETS\GT R\test\" FType = "*.doc" ChDir Directory FName...

VBA in Word: Programmatically add content control with a style

vba,ms-word,word-vba
When programatically adding a rich text content control to a Word .docm using VBA, is there a way to set a style for the contents? As a comparison, if I create a content control manually using the Word Developer toolbar, I can select "Use a style to format contents" in...

Macro in Word to Underline each paragraph in document less than X characters long

vba,ms-word,ms-office,word-vba
I have a word doc of several pages. There are many lines in the document that are short headings, then the carriage return, then a descriptive paragraph. Not front page news. eg Condition Subsequent A condition subsequent is often used in a legal context as a marker bringing an end...

How to reduce title to one line in MS Word using VBA

vba,ms-word,word-vba,word-2010
I am creating documents using VBA in Word with data being pulled from Excel. Unfortunately the titles are sometimes so long that they get wrapped in Word. I need the titles to remain on one line. I plan on doing this by simply changing the font size until the text...

Getting Compile Error: User-defined type not defined when working to make an old 32-bit template into a Word 2012 64-bit template

vba,templates,compiler-errors,32bit-64bit,word-vba
I have a Photo Template that grabs photos from files and places them in their individual boxes in a word document. The file use to be a Word 2003 32-bit file that would only run in our old computer. I have transformed the file to 64-bit to use in our...

Copy the contents of a document(s) through FileDialogbox picker, to a new one

vba,ms-word,word-vba
With MS_Word 2010 I have been trying to achieve the way to copy the contents(whole) of one file to a new one retrieving the file name of the original and adding it to the new one with the suffix "Copy". All this process has a reason, since the Original document...

copy formatted text into access using vba

ms-access,ms-word,access-vba,ms-access-2010,word-vba
I need to save formatted text from Word in an Access Database. So far I've managed to figure out how to store formatted text in an Access Field (Create a Memo Field in a Table and set the Text Format as Rich Text). Searching SO I have not yet come...

“Rich text controls cannot be applied here” when trying to add multiple nested content controls

vba,word-vba,word-interop
I have a VSTO word add-in which is supposed to let the user add potentially nested content controls at/around the current selection. I am having difficulty getting content controls to be added, even under circumstances when I think they should be able to be added. The following VBA code replicates...

Find revision and report the step number where the revision occurs

vba,word-vba
I'm working on a code that will find revisions in a Word document, and report the revision and number from the list (the step number) in a separate table (which is already set up and good to go). (See the attached picture). So for example, any revision in row 3...

Find out background color of highlighted text in MS Word document using C# or VBA

c#,vba,ms-word,office-interop,word-vba
Following finds the highlighted text in a Word document (even if the different text at different places is highlighted with different colors). How can we find out the background color of each highlighted text. I'm using C# but VBA will also be ok: With ActiveDocument.Range.Find .Highlight = True While .Execute...

Grab a character from a word based on specified parameters

vba,word-vba
So say I have a couple of words of various length, and I want to have a MsgBox display the third character from the end for each of them. How would I do that with WordVBA? For example: Stack would return "a" 123.45 would return "." Agent001 would return "0"...

VBA using excel data to search word document & pasting result into a table

vba,excel-vba,word-vba
So I want to be able to search a word document (roughly 300 pages) and find certain phrases (one word or two words seperated by a space) (eg: Nationwide/Phrase 2/Phrase 3) which I have in column 'A' of a separate excel document (C:/Test.xlsx). Then this 'phrase' would be coiped and...

How to manipulate a Word 2013 document opened in read only mode

c#,vba,ms-word,word-vba,readonly
I have to open Word documents for automation in read only mode. Then I have to remove some certain pages from that document, regarding some configuration the user did in the user interface of the program. Manipulation does work for footers and headers, but this particular code doesn't work. It...

Replace hyperlink with text from linked document

vba,word-vba
I have a three worddokuments. The first hast the following structure: Text Hyperlink Text Hyperlink I try to accomplish the following marcro: Open document 1, loop throu the hyperlinks. Open the hyperlinked documents and insert the text in the documents where the hyperlink in document 1 is. What I accomplished...

How to copy specific text?

vba,ms-word,word-vba
I have hundreds of word files to work one at a time, and each one has a line like this (not at the same relative place): Proc. nº 0032545-15.2012.8.19.0053 I need a code that copy only the serial number, in this case 0032545-15.2012.8.19.0053, then I can use it to paste...

Delete row when fields are empty

excel,vba,excel-vba,ms-word,word-vba
I am trying to use Visual Basic so that I can populate word templates with data from excel. I have a macro that fills in fields in a Word document table from a table in Microsoft Excel. So far, if the excel table is smaller than the word table, the...

How do I specify “where” when using the insert method for build blocks in word 10

vba,ms-word,word-vba
I'm trying to add a custom building block at the click of a button in MS word 10. Below is the code currently attached to my activeX button. Private Sub CommandButton1_Click() Dim objTemplate As Template Dim objBB As BuildingBlock ' Set the template to store the building block Set objTemplate...

Debug function altough macros disabled

vba,ms-word,word-vba
I was given a template for report (.dotm) with alot of vba code behind that gets data from a mssql database and writes the data at some bookmarks. Unfortunately, the code has an error and I have to fix it. Since I have never done vba before, I'm a bit...

ActiveX textbox value looping

vba,textbox,activex,word-vba
I'm trying to perform a loop on several activeX textboxes named q1, q2, q3 ... q11. Here's what I tried, but it didn't work: For i = 1 To 11 myValue(i) = ActiveDocument.q & i.Value Next I also tried "q" & i, (q & i), ("q" & i), etc, but...

MS Word Mail Merge and Split Documents saving, Header and footer issue

vba,merge,word-vba,mailmerge
I am using the below Macro to split the mail merged into separate documents. What I need is it to split into separate documents keeping the whole page including the header and footers and saving as in the first merged field on the page, which is the first piece of...

How to choose a different header image in MS Word 2013 using a macro/button

vba,templates,ms-word,word-vba
I'd like to create a Word stationary template with ability to cycle through different colored logos in its header. My company uses a logo in five different colors and I would like to create a single template with a button that would allow me to cycle through the different colored...

How to sort an array of strings containing numbers

arrays,vba,ms-word,word-vba
I'm having some trouble correctly sorting an array numerically based on strings that contain numbers: So, the sort works for all strings up until I get into 2 digit numbers. For example, if the array contains "Issue 2:" "Issue 5:" and "Issue 3:" it correctly sorts the strings to be...