Author Topic: Format postal address copied from Outlook  (Read 1388 times)

E_RAMEL

  • Newbie
  • *
  • Posts: 37
  • 😊 VoiceAttack saves my wrists 👍🏻
    • Landscop design - Paysagistes
Format postal address copied from Outlook
« on: February 08, 2023, 10:51:12 AM »
Hello,

MS Outlook formats the postal address copied into a contact record by default as follows:
"Street Number" + CrLf
"Street name" + CrLf
"City "+" "+"ZIP code" + CrLf
"Country"

I would like the pasted text to look like this:
"Street Number" + "Street name" + "-" + ""ZIP code" + "City"

Do you know a routine to do this?

I look forward to your help. I am at your disposal for any further information.

Thanks in advance.

Edgar


E_RAMEL

  • Newbie
  • *
  • Posts: 37
  • 😊 VoiceAttack saves my wrists 👍🏻
    • Landscop design - Paysagistes
Re: Format postal address copied from Outlook
« Reply #1 on: February 08, 2023, 10:57:04 AM »
ChatGPT shows me a solution in VB that talks about this syntax:

string address = {CLIP};
string[] parts = address.Split('\n');
string result = parts[0] + " " + parts[1] + "-" + parts[3] + " " + parts[2];

But while I can see what it's trying to do, I can't see how to use it directly in Voiceattack.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4781
  • RTFM
Re: Format postal address copied from Outlook
« Reply #2 on: February 08, 2023, 02:44:04 PM »
If what you've posted is verbatim what you're getting from that chatbot, that "solution" is at the very least incomplete.
It's not Visual Basic, either, but it is mostly valid C#.


Do you have any programming experience?
What you currently have is going in the direction of what you'll need, but it's missing quite a bit:

Getting data from the clipboard can be done natively in C# as long as you're referencing System.Windows.Forms within your inline function, but if you want to use the "{CLIP}" token you'll need to parse the token using the appropriate proxy class (for which "VA" is an alias) method.

Splitting the string is what you'd want to to, but given that you're using Windows-style line endings, you'd want to split on that actual line ending, rather than just the newline character, which would leave the carriage return as part of the output.

Given that you want to reverse the "City" and "ZIP code" entries, you'll need to split that as well, but rather than using the Split() method which would also split multi-word city names, something like using LastIndexOf() and Substring() could be more appropriate.

E_RAMEL

  • Newbie
  • *
  • Posts: 37
  • 😊 VoiceAttack saves my wrists 👍🏻
    • Landscop design - Paysagistes
Re: Format postal address copied from Outlook
« Reply #3 on: February 09, 2023, 01:47:55 AM »
Hello Pfeil,

Thanks for your quick feedback.

Unfortunately, I'm not a programmer and my experience is to try to get inspiration from what I can see in different commands, and modify and fumble around to eventually come up with something that works 🤓.

I had a chance to try "Chat GPT", and I asked it a question ... and at first glance, the result is not usable as it is.

However, there is a way to approach the question with the identification of the different parts of the address as copied in Outlook.

The fields are separated by line breaks, and the city and postcode by a double space...

I don't know if Voiceattack would know how to create variables with these elements, and then return them in the desired order with "Quick Input"?

My ideas for solutions are a bit tinkered, and certainly not very academic from a programming point of view, but those are my limitations 😥

If this additional question can advance a solution track?

Thank you in advance.

Sincerely.

Edgar

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4781
  • RTFM
Re: Format postal address copied from Outlook
« Reply #4 on: February 09, 2023, 02:30:32 AM »
VoiceAttack does not currently have a native method to split text directly.

An inline function that does what you appear to be describing could look like this:
Code: [Select]
using System;
using System.Windows.Forms;

public class VAInline
{
public void main()
{
string input = Clipboard.GetText();
if (String.IsNullOrEmpty(input))
{
VA.WriteToLog("Clipboard did not contain any text", "red");
return;
}

string[] substrings = input.Split(new string[] {Environment.NewLine}, StringSplitOptions.None);
if (substrings.Length != 4)
{
VA.WriteToLog("Clipboard input does not consist of four lines", "red");
return;
}

int ZIPCodeSeparatorPosition = substrings[2].LastIndexOf(' ');
if (ZIPCodeSeparatorPosition == -1)
{
VA.WriteToLog("The third line does not contain a space character separating city name and ZIP code", "red");
return;
}

string cityName = substrings[2].Substring(0, ZIPCodeSeparatorPosition);
string ZIPCode = substrings[2].Substring(ZIPCodeSeparatorPosition + 1);

string output = substrings[0] + " " + substrings[1] + " - " + ZIPCode + " " + cityName;

Clipboard.SetText(output);
VA.WriteToLog("Clipboard set to \"" + output + "\"", "green");
}
}

E_RAMEL

  • Newbie
  • *
  • Posts: 37
  • 😊 VoiceAttack saves my wrists 👍🏻
    • Landscop design - Paysagistes
Re: Format postal address copied from Outlook
« Reply #5 on: February 10, 2023, 01:23:56 AM »
Hello Pfeil,

Thank you for your code.

I tried it as is, but I get this error message:

9:11:40.891 Clipboard input does not consist of four lines

It seems that Outlook does not always format the address in the same way depending on whether or not the "Country" or "Region" fields are filled in.

I will continue to look for an inline solution in C#.

Thanks again.

Sincerely.

Edgar

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4781
  • RTFM
Re: Format postal address copied from Outlook
« Reply #6 on: February 10, 2023, 01:28:14 AM »
You haven't mentioned a "Region" field previously, but as long as that's also after the lines you're actually using you could just check for three lines or more, E.G.
Code: [Select]
if (substrings.Length < 3)
The checks aren't necessary for the inline function to work, but if the input is incorrect it's good practice to handle that gracefully rather than letting it throw an exception.

E_RAMEL

  • Newbie
  • *
  • Posts: 37
  • 😊 VoiceAttack saves my wrists 👍🏻
    • Landscop design - Paysagistes
Re: Format postal address copied from Outlook
« Reply #7 on: February 24, 2023, 01:38:52 AM »
Hello,
Small follow-up on my search for a solution.
I found how to make a macro directly in Outlook to format the address copy as I wanted.
I don't know if it can be implemented directly in VoiceAttack or if it's of any interest, but here's the macro if anyone is interested:

Sub ExtractAddress()
Dim objContact As ContactItem
Set objContact = Application.ActiveExplorer.Selection.Item(1)
Dim strAddress As String
strAddress = Replace(objContact.MailingAddressStreet, vbCrLf, " ") & " - " & objContact.MailingAddressPostalCode & " " & objContact.MailingAddressCity
Dim DataObj As New MSForms.DataObject
DataObj.SetText strAddress
DataObj.PutInClipboard
End Sub

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4781
  • RTFM
Re: Format postal address copied from Outlook
« Reply #8 on: February 24, 2023, 08:45:53 AM »
Did you find that the inline function wasn't suitable even after making the proposed modification?

E_RAMEL

  • Newbie
  • *
  • Posts: 37
  • 😊 VoiceAttack saves my wrists 👍🏻
    • Landscop design - Paysagistes
Re: Format postal address copied from Outlook
« Reply #9 on: February 24, 2023, 12:01:08 PM »
Hello Pfeil,
I was struggling a bit with the code for the inline functions.
In addition, Outlook was formatting the copied addresses differently depending on the contact records (if I understood correctly, a question of forms and where the contact record was created (e.g. from an iPhone or not ...).
I tried to solve the problem on the base of Outlook.
The inline functions look very advanced, one day I will have to find time to learn code development.
Have a nice weekend.
Edgar