locked
macro to set Signature RRS feed

  • Question

  • I want build template to my documents.

    I want add macro, when I press him - it add a Signature on the left top of page.

    If I set the Signature, and save the doc, when I open it again, only If am I begin the macro it replace the Signature.

    1. How am I put a flag in the documents to check if there Signature in doc?

    2. How am I mark this paragraph (the Signature) and replace it?

    Monday, February 27, 2012 9:24 AM

Answers

  • How to parse date and time in VBA

    Sub WhatsNow()
        Dim Today, nDay, nMonth, nYear, nHour, nMin, nSec
        Today = Now
        nDay = Day(Today)
        nMonth = Month(Today)
        nYear = Year(Today)
        nHour = Hour(Today)
        nMin = Minute(Today)
        nSec = Second(Today)
    End Sub
    Hope this helps

    Kind Regards, Rich ... http://greatcirclelearning.com

    • Marked as answer by hila_d Wednesday, February 29, 2012 9:32 AM
    Tuesday, February 28, 2012 4:59 PM

All replies

  • Hello,

    Wrap the signature in a bookmark, see Bookmarks.Add() and get Bookmark.Range to add/replace the actual signature. If the bookmark is missing in the document, this means there's no signature.

    I've just tested this code (starting the bookmark name with an underscore creates a hidden bookmark):

    Word.Selection selection = WordApp.Selection;
    object range = selection.Range;
    Word._Document document = WordApp.ActiveDocument;
    Word.Bookmarks bookmarks = document.Bookmarks;
    Word.Bookmark bookmark = bookmarks.Add("_MyBookmark", ref range);
    Marshal.ReleaseComObject(bookmark); bookmark = null;
    Marshal.ReleaseComObject(bookmarks); bookmarks = null;
    Marshal.ReleaseComObject(document); document = null;
    Marshal.ReleaseComObject(range); range = null;
    Marshal.ReleaseComObject(selection); selection = null;
    And here's another one:
    Word._Document document = WordApp.ActiveDocument;
    Word.Bookmarks bookmarks = document.Bookmarks;
    bookmarks.ShowHidden = true;
    object bookmarkName = "_MyBookmark";
    if (bookmarks.Exists(bookmarkName.ToString()))
    {
        Word.Bookmark bookmark = bookmarks.get_Item(ref bookmarkName);
        System.Diagnostics.Debug.Print("bookmark found");
        // your stuff here
        Marshal.ReleaseComObject(bookmark); bookmark = null;
    }
    Marshal.ReleaseComObject(bookmarks); bookmarks = null;
    Marshal.ReleaseComObject(document); document = null;
    Hope this helps.

    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Monday, February 27, 2012 10:13 AM
  • thank about your answer.

    what am I doing if I want add to Signature  the date of today?

    How am I convert Selection.InsertDateTime DateTimeFormat to String?

    Monday, February 27, 2012 1:06 PM
  • This is a procedure, not a function. According to the description, it inserts "the current date or time, or both, either as text or as a TIME field". It doesn't return anything. So, there's nothing to convert. Try using System.DateTime.Now.ToString() or ToShortDateString(), ToLongDateString() etc.

    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Monday, February 27, 2012 1:35 PM
  • I still get error.

    I try:

    Dim rng As Range
        Set rng = ActiveDocument.Bookmarks("Signature ").Range
       
      
        rng.Text = mySignature &" "& Selection.InsertDateTime DateTimeFormat:="d áMMMM yyyy", InsertAsField:= _
        False, DateLanguage:=wdHebrew, CalendarType:=wdCalendarWestern, _
        InsertAsFullWidth:=False
        
       
        ActiveDocument.Bookmarks.Add "dateMark", rng

    Tuesday, February 28, 2012 3:38 PM
  • Selection.InsertDateTime is a Sub (not a Function), it doesn't return anything and you cannot using it in this way. Have you tried converting DateTime.Now to string?

    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Tuesday, February 28, 2012 3:51 PM
  • It looks like you are using VBA so just change the line to rng.Text = mySignature & " " & Date$

    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, February 28, 2012 4:13 PM
  • yes, I want the DateTime.Now  to special format.

    How am I do it?

    • Edited by hila_d Tuesday, February 28, 2012 4:35 PM
    Tuesday, February 28, 2012 4:26 PM
  • How to parse date and time in VBA

    Sub WhatsNow()
        Dim Today, nDay, nMonth, nYear, nHour, nMin, nSec
        Today = Now
        nDay = Day(Today)
        nMonth = Month(Today)
        nYear = Year(Today)
        nHour = Hour(Today)
        nMin = Minute(Today)
        nSec = Second(Today)
    End Sub
    Hope this helps

    Kind Regards, Rich ... http://greatcirclelearning.com

    • Marked as answer by hila_d Wednesday, February 29, 2012 9:32 AM
    Tuesday, February 28, 2012 4:59 PM
  • I have another question on Bookmark:

    after I replace the text in bookmark, my marker has still in bookmark, and if I add free text, it enter to the bookmark area.

    how am I exit from bookmark area?

    and how am I prase date if I want at other DateLanguage?

    • Edited by hila_d Wednesday, February 29, 2012 9:54 AM
    Wednesday, February 29, 2012 9:36 AM
  • When working in a bookmark range and you want to move the insertion point out of the bookmarked area you can use the command: rng.Collapse Word.wdCollapseDirection.wdCollapseEnd. Or if you want to move to the end of the document and establish a new insertion point for the user you can reset the range, collapse it, and select.

    Example:

    Set rng = doc.Content

    rng.Collapse Word.wdCollapseDirection.wdCollaspeEnd

    rng.Select

    Regarding your question about language, I'm sorry I can't help you with that.  Maybe someone else can.


    Kind Regards, Rich ... http://greatcirclelearning.com

    Wednesday, February 29, 2012 1:04 PM
  • If the Signature can have one or more lines, and when I replace it, I am not want empty lines (If I replace long signature by short)  - how am I check how many lines the existing signature?

    Wednesday, February 29, 2012 3:14 PM