#1
|
|||
|
|||
Would Like to test if Full Name is NOT Present in document
I would like to determine if there is NO Full Name in a document.
Please find one attachment, a few lines from the document. I run a Word VBA Macro on this many times every day. In the attachment you may see two, (2) different ways I get the document. Some with a Full Name, others with none. I would like to determine if there is NO Full Name on the document. Something like this: If the Full Name is there Do Something Else Do SomethingElse The funny formatting symbols and strange formatting is giving me trouble. I don't know if this columns or a table or what. Thanks in advance for helping me with this. Attachments |
#2
|
||||
|
||||
There is always a fullname but if a file is new and has never been saved then there won't be a path.
Code:
Sub SavedNotSaved() If ActiveDocument.Path = "" Then 'file is new and not saved with filename Debug.Print "New unsaved doc" Else Debug.Print "It's been saved" End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Guessed, Thank you for your reply. But this document is not saved anywhere, it comes from an application that I work with; the original input document that is not in Word Format. I copy the whole enchilada - original information by highlighting it and Cntrl + C.
With all the information in the clipboard, I turn to a second monitor where Microsoft Word is waiting. I immediately start a macro which pastes all the information and arranges it in a Word document. The Macro saves lots of time and effort by re-arranging it all. But I need to be able to test when the full name is present or not. I thought I would highlight and SELECT the area where the Full Name is supposed to be. (The VBA MACRO can easily highlight and SELECT the area where the Full Name is supposed to be). If I could test the SELECTION for Alpha or Alphanumeric that would work. But from my searching there is only a ISNUMERIC. There is no ISALPHANUMERIC test. |
#4
|
||||
|
||||
It appears that you mean Full Name to be content inside a table next to a cell that is labelled "Employee's Full name who is being Deleted: :" - Is that correct?
The document you posted contains two tables with that label. The first table has an empty cell beside it, the second table contains "John Jones" in that cell. It is not apparent whether that means the file does or doesn't contain a full name. Nor is it apparent whether there will be more tables in the document with other structures that also need to be examined by a macro.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Guessed, thank you for your reply.
I have attached another document, the whole document. This is just like my macro pastes in the Word document, before it starts rearranging it. So this is a table, you say. I know nothing about tables in Word. But I used to write programs in COBOL with tables, so I can learn it fast. Is there any way we could write code to say something like. "Look at the cell next to the cell that contains: Employee's Full name who is being Deleted: : ?? Or does the code have to say something like: Look at cell X1 ?? |
#6
|
|||
|
|||
Guessed, Yes, Full Name is content inside a table next to a cell that is labelled "Employee's Full name who is being Deleted: :"
|
#7
|
||||
|
||||
The following should work. There is however nothing in that cell in your sample.
Code:
Function GetDeleted(oDoc As Document) As String Dim oTable As Table Dim oCell As Range Dim i As Long Dim sName As String Set oTable = oDoc.Tables(1) For i = 1 To oTable.Rows.Count If InStr(1, oTable.Cell(i, 1).Range.Text, "Deleted:") > 0 Then Set oCell = oTable.Cell(i, 2).Range oCell.End = oCell.End - 1 sName = oCell.Text Exit For End If Next i GetDeleted = sName Set oTable = Nothing Set oCell = Nothing End Function Sub Test() Dim sText As String sText = GetDeleted(ActiveDocument) If sText = "" Then MsgBox "There is no name in the cell", vbCritical Else MsgBox sText, vbInformation End If End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#8
|
|||
|
|||
GMayor, Thank you for your reply. Yes, in that sample document there is nothing in that cell. In the documents I must work with, sometimes a name is there and sometimes there is not.
I would like to test only the one cell, just to the right side of "Employee's Full name who is being Deleted: :" (I think I see how you do this in the code.) So the whole idea here, is to test when there is a name there and when there is nothing there. Thank you for the code, I will try to understand your code and test it to see how it works. I will get back to you in a few days, The family thinks I should take a few days off for Thanksgiving. |
#9
|
|||
|
|||
GMayor,
It works. Omigosh! I can understand what it is doing, but I never could have written it. It steps through the entire table looking for "Deleted:" But it only loads sName when there is a match. I did not know VBA could do a FOR - I have been using FOR in PowerShell, so I recognize it. It is just a loop. This saves a lot of time. And I will be able to use this in other ways. I will experiment because I want to test for the condition when there are only spaces in that cell too. They might put spaces in the cell too. Many Thanks. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Creating a full page document from a ragged one. | Balliol | Word | 5 | 04-28-2021 07:46 PM |
Full screen view to specific document | matapagi2019 | Word VBA | 0 | 03-17-2019 10:42 AM |
Detecting that previous character doesn't exist (i.e., present character is first in document) | Robert K S | Word VBA | 15 | 08-01-2016 09:33 AM |
Master document missing images present in Subdocument | Dart82 | Word | 2 | 05-05-2014 08:35 AM |
how to add test in word document | tekle | Word | 2 | 05-24-2010 01:26 PM |