Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-01-2014, 07:10 PM
Hdata Hdata is offline Add text to DocProperty with VBA? Windows 7 32bit Add text to DocProperty with VBA? Office 2010 32bit
Novice
Add text to DocProperty with VBA?
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Thumbs up Add text to DocProperty with VBA?

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
Reply With Quote
  #2  
Old 02-01-2014, 08:30 PM
macropod's Avatar
macropod macropod is offline Add text to DocProperty with VBA? Windows 7 32bit Add text to DocProperty with VBA? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Note that you'll get an error output if the FirstTest1Custom Custom Document Property doesn't exist.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-02-2014, 03:30 AM
Hdata Hdata is offline Add text to DocProperty with VBA? Windows 7 32bit Add text to DocProperty with VBA? Office 2010 32bit
Novice
Add text to DocProperty with VBA?
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Default

Thanks Paul,
Cheers
Hdata
Reply With Quote
  #4  
Old 05-18-2014, 10:49 AM
Hdata Hdata is offline Add text to DocProperty with VBA? Windows 7 32bit Add text to DocProperty with VBA? Office 2010 32bit
Novice
Add text to DocProperty with VBA?
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Default

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?
Reply With Quote
  #5  
Old 05-18-2014, 11:09 AM
Hdata Hdata is offline Add text to DocProperty with VBA? Windows 7 32bit Add text to DocProperty with VBA? Office 2010 32bit
Novice
Add text to DocProperty with VBA?
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Default

Also, I would like to update my profile for Hdata. Please show me.
Thanks.
Reply With Quote
  #6  
Old 05-18-2014, 02:49 PM
macropod's Avatar
macropod macropod is offline Add text to DocProperty with VBA? Windows 7 32bit Add text to DocProperty with VBA? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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:
I would like to update my profile for Hdata. Please show me.
You do that via the UserCP link at the top of this page. See: https://www.msofficeforums.com/usercp.php
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 05-18-2014, 03:46 PM
Hdata Hdata is offline Add text to DocProperty with VBA? Windows 7 32bit Add text to DocProperty with VBA? Office 2010 32bit
Novice
Add text to DocProperty with VBA?
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Default

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. and thanks again.
Reply With Quote
  #8  
Old 05-18-2014, 03:55 PM
macropod's Avatar
macropod macropod is offline Add text to DocProperty with VBA? Windows 7 32bit Add text to DocProperty with VBA? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #9  
Old 05-18-2014, 04:12 PM
Hdata Hdata is offline Add text to DocProperty with VBA? Windows 7 32bit Add text to DocProperty with VBA? Office 2010 32bit
Novice
Add text to DocProperty with VBA?
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Default

Thanks again Paul, let me work with these alternatives.

Best regards,
Hdata
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add text to DocProperty with VBA? Change in font size of docproperty Sandy27 Word 9 01-03-2014 01:51 PM
Add text to DocProperty with VBA? Trying to Reference a ContentControl Using DocProperty 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:37 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft