VBA
9 TopicsTable cell value from VBA
Hello, I have a question. When I get the value of a cell from a table in Word, it always brings it with an unknown character. If we activate "Show All" we see that there is a respective symbol for each cell, although each part of a document has its own symbol, like space, section break, page break, paragraph, etc. In fact, I get, for the example of this post, the value of a paragraph and it gives it to me clean, without any character. So, I don't understand what is that character that brings the value of the cell, do you know what is that character and do you know what other way there is to extract the value cleaner without that character? I show you in the screenshot the solution I gave to this problem (it is as a comment), but I would like to know if you have another way to solve this issue.Solved1KViews0likes2CommentsVBA code to bring the active document in front
I have a subroutine that produces and edits six different documents. My current problem is that the documents all stay in the background. But, I want them to be in front of all other windows using only VBA and without APIs. I've looked for code to do that and they either use APIs or create a new document. I tried this code: Sub Generate_Motion() Call Initiations Documents.Add Template:=pathInputDoc & "\Template.dotx" Dim wdApp As word.Application Dim word As word.Document Set wdApp = GetObject(ActiveDocument, "Word.Application") wdApp.Visible = True With ActiveDocument ... End Sub but it gives me an error. Is there way to bring an already-open document in front using only VBA code?4.9KViews0likes3CommentsSOLVED - Expand/Collapse specified heading(s) based on criteria (VBA code)
Hello all, I spent an entire evening trying to figure out the VBA code to collapse all headings in my document upon opening except for my "Notes" header (and only if I had a form control checkbox checked saying to do so). The only thing I found on the internet was the Range.Expand and Range.Collapse functions which did not work at all, no matter how much I played with it. I did manage to get the result I wanted using the SendKeys function with a collab of.Activate but it was really wonky. However, when I was playing around with it and searching through the arguments, I found.CollapsedState which could only follow after .Paragraphs(). I tested it and it worked! Polished Code: Private Sub Document_Open() On Error GoTo ErrHandler ThisDocument.ActiveWindow.View.CollapseAllHeadings Dim check_box As ContentControl Set check_box = ThisDocument.Range.ContentControls(Index:=1) If check_box.Checked = True Then Call ExpandNotes ErrHandler: End Sub Sub ExpandNotes() On Error GoTo ErrHandler1 Dim IsFound As Boolean 'ThisDocument.ActiveWindow.NewWindow.Activate 'OUTDATED METHOD 'ActiveWindow.Close 'OUTDATED METHOD IsFound = FindParagraph(ThisDocument.StoryRanges(wdMainTextStory), "Heading 1") ErrHandler1: End Sub Public Function FindParagraph(ByVal SearchRange As Word.Range, ByVal ParaStyle As String) As Long On Error GoTo ErrHandler2 Dim ParaIndex As Long For ParaIndex = 1 To SearchRange.Paragraphs.Count If ThisDocument.Paragraphs(ParaIndex).Range.Style = ParaStyle Then FindParagraph = ParaIndex If ThisDocument.Paragraphs(ParaIndex).Range.Text Like "*Notes*" Then ThisDocument.Activate ThisDocument.Paragraphs(ParaIndex).CollapsedState = False 'ThisDocument.Paragraphs(ParaIndex).Range.Select 'OUTDATED METHOD 'SendKeys "{RIGHT}", True 'OUTDATED METHOD 'SendKeys "~", True 'OUTDATED METHOD Exit Function End If End If Next ErrHandler2: 'Function built off of the original code (by freeflow): _ https://stackoverflow.com/questions/61209283/vba-word-find-a-paragraph-that-has-a-specific-style End Function It would be cool to use a date function for criteria at some point too, to expand headers that are within today's date. Notes: •The commented-out code followed by 'OUTDATED METHOD was the first working but wonky attempt. Lines can be removed. • The "check_box" variable is for the only checkbox I have in the document, which is a form control (not an ActiveX control). • You can change the "Heading 1" (ByVal as String for theFindParagraph function) under the "ExpandNotes" sub to the style name that is applicable to your headers. • You can change the "*Notes*" to whatever text your header is. You can also expand/collapse multiple headers if you include an Or statement(s) in that If. • The code is under the "ThisDocument" (Microsoft Word Objects) module for my workbook (not for "Normal"). • Edit/add/remove from the code to your needs.Solved3.6KViews0likes1CommentVBA issue
I have a VBA code and instructions on how to add this into word. The code will take the comments from the word document and add them into a new excel sheet. Whenever I run the code I get the error message Compile error: Sub or function not defined and highlighted in yellow is:Private Sub Document_New() This is at the end of the code. I have no experience with VBA or codes and would just like to know if there is an easy way for this to be sorted, thanks! Edited, code aded in below: Sub ExportComments() 'Dim xlApp As Excel.Application 'Dim xlWB As Excel.Workbook Dim i As Integer, HeadingRow As Integer Dim objPara As Paragraph Dim objComment As Comment Dim strSection As String Dim strTemp Dim myRange As Range Set xlApp = CreateObject("Excel.Application") xlApp.Visible = True Set xlWB = xlApp.Workbooks.Add 'create a new workbook With xlWB.Worksheets(1) ' Create Heading HeadingRow = 1 .Cells(HeadingRow, 1).Formula = "Comment" .Cells(HeadingRow, 2).Formula = "Page" .Cells(HeadingRow, 3).Formula = "Paragraph" .Cells(HeadingRow, 4).Formula = "Comment" .Cells(HeadingRow, 5).Formula = "Reviewer" .Cells(HeadingRow, 6).Formula = "Date" strSection = "preamble" 'all sections before "1." will be labeled as "preamble" strTemp = "preamble" If ActiveDocument.Comments.Count = 0 Then .Cells(2, 1).Value = "No comments found" MsgBox ("No comments") Exit Sub End If For i = 1 To ActiveDocument.Comments.Count Set myRange = ActiveDocument.Comments(i).Scope strSection = ParentLevel(myRange.Paragraphs(1)) ' find the section heading for this comment 'MsgBox strSection .Cells(i + HeadingRow, 1).Formula = ActiveDocument.Comments(i).Index .Cells(i + HeadingRow, 2).Formula = ActiveDocument.Comments(i).Reference.Information(wdActiveEndAdjustedPageNumber) .Cells(i + HeadingRow, 3).Value = strSection .Cells(i + HeadingRow, 4).Formula = ActiveDocument.Comments(i).Range .Cells(i + HeadingRow, 5).Formula = ActiveDocument.Comments(i).Initial .Cells(i + HeadingRow, 6).Formula = Format(ActiveDocument.Comments(i).Date, "dd/MM/yyyy") .Cells(i + HeadingRow, 7).Formula = ActiveDocument.Comments(i).Range.ListFormat.ListString Next i .Cells(i + HeadingRow, 1).Value = "DONE" End With Set xlWB = Nothing Set xlApp = Nothing End Sub Function ParentLevel(Para As Word.Paragraph) As String ' Finds the first outlined numbered paragraph above the given paragraph object Dim ParaAbove As Word.Paragraph Set ParaAbove = Para sStyle = Para.Range.ParagraphStyle sStyle = Left(sStyle, 4) If sStyle = "Head" Then GoTo Skip End If Do While ParaAbove.OutlineLevel = Para.OutlineLevel Set ParaAbove = ParaAbove.Previous Loop Skip: strTitle = ParaAbove.Range.Text strTitle = Left(strTitle, Len(strTitle) - 1) ParentLevel = ParaAbove.Range.ListFormat.ListString & " " & strTitle End Function Private Sub Document_New() End Sub2.5KViews0likes9CommentsHow to disable "RemoveDocumentInformation"?
I use ActiveDocument.RemoveDocumentInformation (wdRDIDocumentProperties) in VBA to remove personal data. Once activated it kills also any personal data in future in the document. So if I want to get rid of this function I know only how to deactivate it manually but I do not find any VBA command to deactivate it. Can you help my?618Views0likes1CommentWord Challenge: How to divide a Word Document in several in an easy way
Hello, On my YouTube channel where I present Office tutorials (In Spanish), they asked me how to easily divide Word documents into several. I searched the internet and only found solutions where the document was converted to PDF for splitting or where the split had to be done with fixed page size. I have found a way to do it and I am preparing a video to show how to do it. But before, I have released the following challenge where I ask to look for ways to do this split. I hope you like it and participate: https://youtu.be/AVh5d0EfHAs595Views0likes0CommentsForm-DocVariable-Building Block Quagmire
I think this one is low-hanging fruit, but my brain has gone full circle, multiple times and I know the solution is easy, but it is elusive. Here's the setup: I have a Userform that collects information thatn is passed onto a document via DocVariables. So, person fills it out and a template is filled in that can be copy/pasted into an email, ORI put a button in the file that would copy/paste the email (as a section) into a newly created Outlook Mail object, which would make the faciilitator's job that much easier, by literally putting one click away from generating an email and sending it from Outlook in a perfectly and wonderfully controlled environment. Everything works swimmingly - I have the content copying over wonderfully into the body, set some static "Subject" Text, etc. But, when trying to pass the .To variable, I get locked up by the DocVariable Field codes sneaking into the To: field. Here's what happens. I get the variable in from the UserForm as "ParticipantEmail" Variable ParticipantEmail is stored in a Building Block called "DCVPartEmail" I use the BuildingBlock to pass the Variable to the .To field in Outmail as DCVPartEmailBB. But then, in the "To:" line in the outlook email, I get EVERYTHING from the DocVariable - *{ DocVariable ParticipantEmail \*MergeFormat} *<TheInputEmailAddress@Email.com>* Obviously, I only want the "TheInputEmailAddress@email.com" to show up in the To: Field. That's all I want to happen - I don't even need the Particpant Email to show up in the actual Word Doc, although I can hide it to pass it, if needed. But all I want is for the Participant email to go from the UserForm to the To: field in an OutMail creation, as just the email. No extras! Check out Module 3 and page 10 for more context Thanks in advance!906Views0likes0CommentsUserform Listbox HELP
Greetings Everyone, I need some HELP! Please bear with me if I ask too many questions, as I am fairly new to VBA. Anyways,I created a userform in Word 2016 to auto-populate a template but I would like to create a drop-down where a user could select text from the drop-down and insert the selected text in the Word template. How would I go about doing so? I was able to create a listbox in the userform with the following code below, however how do I get the item selected to populate in the word template??? Private Sub UserFrom_Initialize() With ListBox1 .AddItem "VIA US MAIL ONLY" .AddItem "VIA Electronic Mail Only" End With lbl_Exit: Exit Sub End Sub Thank you in advance for anyone's assistance!!Solved942Views0likes1Comment'Calling' a function from within a Word document
I know this is probably really basic but this will be my first real dive into Word VBA, I am quite experienced in Excel VBA, though. I am having difficulty getting my head around how Word 'knows' that a 'block of text' is code, rather than just text of the document. As my first 'excursion', within Word VBA, I am seeking to split a phrase, when it is passed, through mail merge, from an Excel spreadsheet to a Word document. With apologies for i. the fact that my interface to this forum does not permit me to see all the proper formatting options for code and ii. the following is more of a 'storyboard' version of code, rather than the proper thing. The relevant Excel spreadsheet is used to support a group of mail merge letters and this 'issue' is only relevant to one letter, so I do not wish to redesign the spreadsheet; I would prefer to handle the 'issue' within Word, through VBA code. I will focus on two mail merge fields, within this question. The 2 mail merge fields are called, for the sake of simplicity here, i) Relative_Name and ii) Relative_Relationship. Relative_Relationship contains, for example, the value "Second Cousin Twice Removed of the Husband of my Second Cousin Once Removed", and, just to make up a name, Relative_Name contains "Walter Gerard Montague". Now when I simply include the fields in the other letters I simply type - Based on reviewing the online tree which has brought you to my attention, I can see that your tree includes «Relative_Name». Your relationship to me:«Relative_Relationship». and I would end up with a final merged text that reads - Based on reviewing the online tree, which has brought you to my attention, I can see that your tree includes Walter Gerard Montague. Your relationship to me: Second Cousin Twice Removed of the Husband of my Second Cousin Once Removed. On this occasion, though I want to split the contents of the field Relative_Relationship into two halves, through passing it to a VBA macro, so that I would end up with - Based on reviewing the online tree, which has brought you to my attention, I can see that your tree includes Walter Gerard Montague. Walter Gerard Montague is the Husband of my Second Cousin Once Removed and you are his Second Cousin Twice Removed. Now for the sake of the following let's call the VBA function, which I propose to write, Relationship_Phrase_Split. Now here is the nub of my question, how does Word know I am invoking the subroutine when I write, in the mail merge document, something that maybe looks like - Based on reviewing the online tree, which has brought you to my attention, I can see that your tree includes «Relative_Name». Relationship_Phrase_Split(«Relative_Relationship», maybe some other fields from the Excel spreadsheet to control how the splitting is done, e.g. «Gender»). I have only italicised, in the above, just to highlight where I am needing assistance to understand how to cause Word to process the code. I am not asking for help on the split function, only the correct syntax for calling it, please. I have made the document a .docm extension. With thanks in anticipation. Philip Bendigo, VioctoriaSolved3.9KViews1like2Comments