![]() |
#1
|
|||
|
|||
![]()
In the past, I have added selected text to bookmarks programmatically with VBA as indicated in Sub Macro11(). Works great.
My first try with Custom Document Properties! I would like to add selected text from a word document to DocProperty Programmatically with VBA. Not sure if this is possible, maybe my syntax needs work? Any Help. Code:
Sub Macro11() With ActiveDocument.Bookmarks .Add Range:=Selection.Range, Name:="test1bm" .DefaultSorting = wdSortByName .ShowHidden = False End With End Sub Code:
Sub Macro13() With ActiveDocument .Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _ "DOCPROPERTY FirstTest1Custom ", PreserveFormatting:=True End With End Sub |
#2
|
||||
|
||||
![]()
I'm not sure what your first code block has to do with add a DOCPROPERTY field to a document. The correct syntax to add a DOCPROPERTY field is:
Code:
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _ Text:="DOCPROPERTY FirstTest1Custom", PreserveFormatting:=True
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Thanks Paul,
Cheers Hdata ![]() |
#4
|
|||
|
|||
![]()
I first posted my question above regarding Document Properties, and you correctly indicated how to add that I would get an error if the Custom Document Property did not exist.
OK, what about DocVariable field. Can I add a DOCVariable field to a document programmatically with VBA if the field does not previously exist? Using the ActiveDocument.Fields.Add Range:=Selection.Range? |
#5
|
|||
|
|||
![]()
Also, I would like to update my profile for Hdata. Please show me.
Thanks. |
#6
|
||||
|
||||
![]()
As I indicated in my first reply, the code you posted had nothing to do with adding a DOCPROPERTY field to a document. I also showed the correct syntax to add a DOCPROPERTY field to a document.
The process for adding a DOCVARIABLE field to a document is the same as for adding a DOCPROPERTY field to a document; simply change DOCPROPERTY to DOCVARIABLE. However, that still doesn't add the DOCVARIABLE to the document! neither does it update one. DOCVARIABLE and DOCPROPERTY fields can do no more than reflect what is in the document properties and variables to which they refer. For more on Document Variables, see: Document.Variables property (Word) | Microsoft Docs For more on Custom Document Properties, see: Document.CustomDocumentProperties property (Word) | Microsoft Docs Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
Thanks, your instructions are very clear. So may I ask another question.
I must say that I like the way bookmarks can be programmatically added without prior existence, and updated to a work document, the negative to bookmarks is they can be easily deleted in the word text. Do you know of a solution, maybe a class module? Maybe a private solution not part of this forum, Microsoft Tech Support? Other Tech support? please advise. ![]() |
#8
|
||||
|
||||
![]()
Adding code to a document, whether via a class module or something else, is unlikely to prevent bookmark deletion. Yes, bookmarks are fragile, being easily deleted and/or having content either included or excluded unintentionally but they're not the only 'containers' you can use in Word, plenty of which are more robust than bookmarks. Depending on what you're trying to do, you could use a table, Style, Section, content control or field, for example, to hold the same content. And, as already discussed, there's document properties and variables.
As for 'private' solutions, MS doesn't engage in those, so contacting MS for one won't get you very far. Often, too, you'll find users with a better knowledge of how to do some things than the MS technical experts.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
![]()
Thanks again Paul, let me work with these alternatives.
Best regards, Hdata |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Sandy27 | Word | 9 | 01-03-2014 01:51 PM |
![]() |
SuzeG | Word VBA | 4 | 12-17-2013 03:40 PM |
Using DocProperty Field Codes multiple times in the same document | trueimage | Word | 2 | 09-18-2013 04:44 PM |
Field DOCPROPERTY LastSavedTime format problem | ragesz | Word | 4 | 10-29-2012 06:58 AM |
My plain text post got converted to rich text in a reply, how to convert it back? | david.karr | Outlook | 0 | 01-05-2012 09:46 AM |