word-vba

Event change of table in Word

醉酒当歌 提交于 2019-12-24 01:52:34
问题 Private Sub Document_Change(ByVal Target As Range) Set table = ActiveDocument.Tables(1) If Not Intersect(table, Target) Is Nothing Then Target.AutoFormat ApplyColor: Red End If End Sub I have the following code, but it does not seem to work in VBA Word. Can anyone help me out? 回答1: I suppose that you copied the code from Excel VBA and tried to rebuild it a bit. There are a few differences between Excel and Word VBA. Intersect does not exist in Word VBA. DocumentChange event in Word works

Insert text before and after selection and set style of new text

*爱你&永不变心* 提交于 2019-12-24 01:17:51
问题 I can insert text before and after the selection using: Selection.InsertBefore "start" Selection.InsertAfter "end" But I have no control over the style of the inserted text. How can I set the new, inserted text to a specific style (and leave the original selected text as it is)? 回答1: Below are two separate code to handle Insert After and Insert Before. Once you insert the text then depending on where is it inserted you have to select the inserted text and then change the style. Sub

Assign even odd page numbers in word

霸气de小男生 提交于 2019-12-23 17:59:58
问题 I am trying to write a vba macro for word, where the document will have page numbers, but the alignment of odd page number will be different from the even page. The code that I am presently trying is changing the alignment of both the odd and even pages,which is not desired ActiveDocument.Sections(i).PageSetup.OddAndEvenPagesHeaderFooter = False ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter.LinkToPrevious

Correlate Range.Text to Range.Start and Range.End

依然范特西╮ 提交于 2019-12-23 17:52:56
问题 I'm using regular expressions to search against the plain text returned by the following property: namespace Microsoft.Office.Interop.Word { public class Range { ... public string Text { get; set; } ... } } Based upon the matches I want to make changes to the formatted text that corresponds to the plain text. The problem I have is that the indices of characters in the .Text property do not match up with the .Start and .End properties of the Range object. Does anyone know any way to match

Deleting a range from a table with merged cells

寵の児 提交于 2019-12-23 15:26:52
问题 Basically, what I'm trying to accomplish is this: Delete all rows from a table starting from where the cursor is in the table to the end of the table. The problem is that this table contains vertically merged cells, so when I try to do something like this: For i = Selection.Tables(1).Rows.Count To Selection.Cells(1).RowIndex Step -1 Selection.Tables(1).Rows(i).Delete Next It complains that individual rows cannot be accessed because the table contains vertically merged cells. I've also tried

Trigger MS Word macro after save event

南笙酒味 提交于 2019-12-23 09:36:17
问题 My MS Word 2007 template has a footer with the filename in it. The user is going to open the template and do a "Save As..." to make their document. I want the filename shown in the footer to update immediately to the new filename. Is there an AfterSaveEvent or something that I can use as a hook to start my VBA script that does the update? Or is there a much easier way? 回答1: Just create a macro like this (I believe it works better if included in the Normal.dot) Sub FileSaveAs() ' ' FileSaveAs

Does VBA contain a comment block syntax? [duplicate]

余生长醉 提交于 2019-12-23 06:49:01
问题 This question already has answers here : How to comment and uncomment blocks of code in the Office VBA Editor (9 answers) Closed 3 years ago . In VBA is there a short way to comment out a block of code the same way java uses /*...*/ ? 回答1: Although there isn't a syntax, you can still get close by using the built-in block comment buttons: If you're not viewing the Edit toolbar already, right-click on the toolbar and enable the Edit toolbar: Then, select a block of code and hit the "Comment

Searching for words in word, but ignoring tables

穿精又带淫゛_ 提交于 2019-12-23 06:11:13
问题 I have the fantastic macro below which Searches for words (listed in an excel file) Copies each instance Pastes into a new word document together with it's location from the original document This has been created and amended by various people and I am truly greatful!!. One thing that I was wondering if possible is: If in the word document which you're searching there are tables, can you make the macro to ignore tables? or would it be better to say 'If the word is found and is in a table

VBA to generate a default file name when Save As is selected in Word 2016

允我心安 提交于 2019-12-23 05:29:17
问题 I have VBA that, in Word 2010, will default a file name into the Save As screen when the user selects the Save As file option. Word 2016 (Office 365) has a different interface with a screen to select a save location (OneDrive, This PC,etc) prior to the "standard" Save As dialog box appearing which seems to have broken the code to generate a specified default file name. The code (with title simplified) is Sub FileSaveAs() With Dialogs(wdDialogFileSaveAs) .Name = "MyTitle" .Show End With End

MS Word SaveAs, check “Maintain Compatibility” via VBA

本小妞迷上赌 提交于 2019-12-23 03:29:13
问题 There's a check box "Maintain compatibility with previous versions of Word" in "SaveAs" dialogue box, by recording a macro and have the check box ticked, I got ActiveDocument.SaveAs2 FileName:= _ "C:\Users\jondoe\123.dotm", _ FileFormat:=wdFormatXMLTemplateMacroEnabled, LockComments:=False, _ Password:="", AddToRecentFiles:=True, WritePassword:="", _ ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _ SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _ False,